0

I am making a program that inserts a two dimensional array with random integers and locates the highest value to display it and it's coordinates in the array. I am using three files. utils.h, utils.cpp, and main.cpp. My program displays an array but it is not correct and I cannot figure out why despite all my research. Any help would be appreciated. This is a college assignment and I know that my professor wants main and utils.h left as is so the only thing I can change is utils.cpp. Thank you for your time.

#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];
        }
    }
}

This is my output I am getting.

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 . . .
sschrass
  • 7,014
  • 6
  • 43
  • 62
Kayla Bianchi
  • 67
  • 3
  • 5
  • 11

3 Answers3

3

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();
Reunanen
  • 7,921
  • 2
  • 35
  • 57
0

In fillTable you should use something like:

table[i][c] = rand() % MAX_VALUE; // MAX_VALUE is the largest value +1 you want to generate
cout << table[i][c] << " ";

(Note the whitespace to separate numbers, you might also want to insert a cout << endl; after each row. But I'm not sure if fillTable should also print out the values?)

Also the displayTable function only prints the first ten lines/columns (but this might be intentional?)

MartinStettner
  • 28,719
  • 15
  • 79
  • 106
0

You are just making the random number output and not inserting in it. do it like this

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

PLus you are getting a garbage value when you find the largest value because there is no actual value stored in the array.

Coding Mash
  • 3,338
  • 5
  • 24
  • 45