The code is supposed to use the pseudo random number generating algorithm, via an external function, to produce a random number from 0 999 and store it into an element of a 15x15 array, until all elements are filled. Obviously with random numbers being generated, there should be no noticeable order within the array, completely unsorted, so to say. This array is to be displayed in a table format. Easy enough and shown below.
Once this task is completed, the next task is for the program to find the largest number within this array. Furthermore, it is to report this value's location within the array. The largest number and its location is to be displayed following the table. This is my dilemma.
I was thinking after displaying the randomly ordered table, I'd use the selection sort method to order the array from least to greatest, then get the number at the end of this array, then refer back to the initial random array to locate the value within this array, and report its location. This, however, seems like a tedious task. I'm wondering if there's a way, without ordering the array, to simply search for and get the largest value and its location?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
#include <iostream>
using namespace std;
// Declare and establish external function for pseudo random number generator.
long long PRNG(long long& seed)
{
// Declare constant values for seed, multiplier, increment, and modulus.
int MULTIPLIER = 2147483647;
int INCREMENT = 1103515245;
int MODULUS = 12345;
// Declare variable to store random number.
// State pseudo random number generator equation.
seed = ( MULTIPLIER * seed + INCREMENT) % MODULUS;
// Return random number to be stored into element.
return seed % 1000;
}
int main()
{
// Declare array variable along with array size of 15 x 15.
const int ROWS = 15;
const int COLUMNS = 15;
int row_number;
int column_number;
long long random_numbers[ROWS][COLUMNS];
// Set seed reference to start with 1.
long long seed = 1;
// Call for external function PRNG to generate a random number.
// Repeat until all elements of the array are filled.
for ( row_number = 0; row_number < ROWS; row_number++)
{
for ( column_number = 0; column_number < COLUMNS; column_number++)
{
random_numbers[row_number][column_number] = PRNG(seed);
// Display array.
cout << random_numbers[row_number][column_number] << " ";
}
cout << endl;
}
// State the largest value in the array and its coordinate.
return 0;
}
|
I appreciate any feedback on the integrity of my code thus far, improvements and critiques alike!