Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Populate an array of size 100 with integers > = 0 and < = 1000. Initialize the a

ID: 3770299 • Letter: P

Question

Populate an array of size 100 with integers > = 0 and < = 1000. Initialize the array using a random number generator with “time” as the seed value., call it RandomNums. Further,

1. Print the elements of the array RandomNums after initialization.

2. Perform selection sort using function on array RandomNums, and sort the array in non-increasing order.

3. Print the elements of RandomNums after sorting from within the above defined function.

4. Create another array of size 50, call it TerminalSum

5. Initialize each element of TerminalSum by adding each pair of terminal elements from RandomNums. For Example: TerminalSum 1st element = RandomNums 1st element + RandomNums 100st element TerminalSum 2nd element = RandomNums 2nd element + RandomNums 99th element . . TerminalSum 50th element = RandomNums 50th element + RandomNums 51st element Print TerminalSum array after initialization.

6. Perform selection sort on TerminalSum, and sort the array in non-decreasing order using another function.

7. Print the elements of the array TerminalSum after sorting from within the above defined function.

Explanation / Answer

#include <iostream>

unsigned int PRNG()
{
    // our initial starting seed is 5323
    static unsigned int seed = 5323;

    // Take the current seed and generate a new value from it
    // Due to our use of large constants and overflow, it would be
    // very hard for someone to predict what the next number is
    // going to be from the previous one.
    seed = (8253729 * seed + 2396403);

    // Take the seed and return a value between 0 and 32767
    return seed % 32767;
}

int main()
{
    // Print 100 random numbers
    for (int count=0; count < 100; ++count)
    {
        std::cout << PRNG() << " ";

        // If we've printed 5 numbers, start a new column
        if ((count+1) % 5 == 0)
            std::cout << " ";
}
}