Searching a 2D (15x15) array

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!
closed account (48T7M4Gy)
to find the largest number within this array.


You don't have to sort the array and you can start finding the maximum with associated indices as the array is being built.

Just set max = the first element once it has been generated and work from there recording the indices as appropriate.

By the time the array is full you have all the info you need. :)

PS You won't need more than 3 or 4 extra lines I reckon.
Last edited on
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
int main()
{
      int i, j;
      // Declare array variable along with array size of 15 x 15
      const int ROWS = 15;
      const int COLUMNS = 15;
  
      long long random_numbers[ROWS][COLUMNS];

      // Set seed reference to start with 1. 
      long long seed = 1;

      cout << "Generating & displaying the table : " << endl;

      for(i = 0; i < ROWS; ++i)
      {
            cout << endl << "  ";
            for(j = 0; j < COLUMNS; ++j)
            {
                  random_numbers[i][j] = PRNG(seed);
                  cout << random_numbers[i][j] << " ";
            }
      }

      // State the largest value in the array and its coordinate.
      int max_x = 0, max_y = 0; // Location of the largest value (column, row)
      long long num_max = random_numbers[0][0];

      for(i = 0; i < ROWS; ++i)
      for(j = 0; j < COLUMNS; ++j)
      {
            if(num_max < random_numbers[i][j])
            {
                  //  Update the largest number and its location 
                  num_max = random_numbers[i][j];
                  max_x = j;
                  max_y = i;
            }
      }

      cout << endl;
      cout << "Printing the largest value & its location : " << endl;

      cout << "    Number value : " << num_max << endl;
      cout << "    Location : (x = " << max_x + 1<< ", y = " << max_y + 1<< ")" << endl;

      return 0;
}
Last edited on
kemort, thanks for your assistance yet again! I looked at Half Life G Man's style and was able to make sense out of what you meant. I didn't think that setting the value to evaluate following numbers using an if statement would work in recognizing the largest number within the array, and given my results after running the program, it did it just fine!

Half Life G Man, thank you! Your code makes sense to me!
closed account (48T7M4Gy)
Just set max = the first element once it has been generated and work from there recording the indices as appropriate.

I was slightly out because you don't have to wait. Anyway, this is what I meant that it can be done with a single pass/loop.

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
50
51
#include <iostream>

long long PRNG(long long& seed)
{
    int MULTIPLIER = 2147483647;
    int INCREMENT = 1103515245;
    int MODULUS = 12345;
    
    seed = ( MULTIPLIER * seed + INCREMENT) % MODULUS;
    
    return seed % 1000;
}

int main()
{
    const int ROWS = 15;
    const int COLUMNS = 15;
    
    long long random_numbers[ROWS][COLUMNS];
    
    long long seed = 1;
    
    int row_max = 0;
    int col_max = 0;
    
    long long max = 0; // because all numbers generated >= 0
    
    for ( int row_number = 0; row_number < ROWS; row_number++)
    {
        for ( int column_number = 0; column_number < COLUMNS; column_number++)
        {
            random_numbers[row_number][column_number] = PRNG(seed);
            
            if (random_numbers[row_number][column_number] > max)
            {
                row_max = row_number;
                col_max = column_number;
                
                max = random_numbers[row_number][column_number];
            }
            
            std::cout << random_numbers[row_number][column_number] << '\t';
        }
        
        std::cout << '\n';
    }
    
    std::cout << "Maximum value = random_numbers[" << row_max << "][" << col_max << "] = " << max << '\n';
    
    return 0;
}


867	644	218	961	997	869	968	811	827	769	298	36	567	984	743	
721	122	959	333	421	467	974	113	826	272	464	143	396	372	459	
993	886	417	464	153	686	412	329	768	136	567	979	598	701	187	
244	553	346	882	424	278	766	487	899	538	566	477	274	788	736	
917	144	543	61	987	574	783	576	117	804	23	446	882	419	133	
746	552	184	413	491	892	724	953	21	787	579	938	231	92	734	
578	771	617	139	413	476	112	664	148	531	757	9	838	236	587	
899	533	421	457	339	73	956	842	214	993	881	927	444	218	971	
632	254	838	241	732	919	468	136	582	759	658	506	677	274	778	
446	877	274	113	811	492	59	338	906	342	889	238	891	567	974	
108	681	252	529	428	271	297	529	443	51	357	679	933	81	927	
434	583	931	417	479	933	91	217	819	798	361	667	974	103	191	
232	594	713	146	877	944	893	216	642	549	368	476	97	884	743	
726	612	979	268	481	592	49	43	376	452	179	273	616	977	944	
888	726	277	614	653	351	22	299	193	891	552	194	48	531	762	
Maximum value = random_numbers[0][4] = 997
Program ended with exit code: 0
Topic archived. No new replies allowed.