c++arraysrandomvariable-assignmentdouble-pointer

How to locate the highest value in a randomly filled two-dimensional array


I am making a program that inserts a two-dimensional array with random integers and locates the highest value to display it and its coordinates in the array.

I am using three files:

#include "utils.h"

void fillTable(int table[ROW_COL_SIZE][ROW_COL_SIZE]) {
    for(int i = 0; i < ROW_COL_SIZE; i++) { 
        for(int c = 0; c < ROW_COL_SIZE; c++) { 
            cout << rand() % table[i][c];
        }
    }
} 

void findLargest(int table[ROW_COL_SIZE][ROW_COL_SIZE], int& largestRow, 
                 int& largestCol) {
    largestRow = 0;
    largestCol = 0;

    for ( int i = 0; i < ROW_COL_SIZE; i++) {
        for ( int j = 0; j < ROW_COL_SIZE; j++) {
            if(table[i][j] > table[largestRow][largestCol]) {
                largestRow = i;
                largestCol = j;
            }
        }
    }
}

void displayTable(int table[ROW_COL_SIZE][ROW_COL_SIZE]) {
    for (int i = 0; i < 10; i++) {
        for ( int j = 0; j < 10; j++) {
            cout << table[i][j];
        }
    }
}

My program displays an array, but it is not correct:

55302337713078127332504421405961229072248303443307961481223132483391855019110600
92808812679236602328231529150663269913935376911094217591887215022974011255316512
71103276228950168692675422850260269511370054042617128509148242205517590190271332
93168530667935211606208729747118402681321223203422069312038223266476231187148148
05966618422064721159313592422312213211891498452701498229001417726265175102184575
4298481247015001631326472115171254718059341323252489617888241851323216308-858993
460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993
460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993
460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993
460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993
460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993
460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993
460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993
460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993
460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993
460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993
460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993
460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993
460-858993460-858993460-858993460The largest value located at [0] [0] is: -85899
3460
Press any key to continue . . .

The only thing I can change is utils.cpp.


Solution

  • Should fillTable fill table? Currently it does not - it only prints to cout. Therefore, table appears to remain uninitialized.

    So, in fillTable, instead of:

    cout << rand() % table[i][c];
    

    You probably want something like:

    table[i][c] = rand();