Reclaim Reclaim - 21 days ago 7
C++ Question

Unable to add the columns and rows of a 2D array

I am currently having some difficulty adding the total columns in a data file I was given, shown below:

46.70 83.20 39.40 18.00 42.40 63.60
29.10 12.40 9.50 74.80 43.40 55.70
13.90 96.70 11.70 71.20 39.90 46.20
22.50 84.30 76.00 27.40 50.60 7.10
52.90 56.40 17.60 62.60 59.50 10.30
46.00 67.90 47.00 58.70 75.20 64.30
25.70 19.70 90.10 65.50 67.90 76.00
11.10 97.70 64.10 75.00 65.20 38.80
96.30 48.60 19.20 48.40 69.70 66.60
17.50 74.10 46.10 23.00 6.10 5.40
88.20 43.90 94.90 17.00 47.30 23.20
35.00 40.60 86.10 11.80 18.20 74.60
58.30 14.40 88.10 87.60 62.90 90.50
68.90 82.40 65.30 55.50 63.30 97.80
76.80 87.80 33.30 40.70 81.10 60.10
80.40 84.70 16.40 24.20 93.30 27.70
49.70 61.90 10.40 73.50 57.40 36.80
82.70 9.50 20.20 69.10 39.60 5.50
10.60 23.30 31.60 70.70 15.20 20.70
64.20 95.80 42.80 40.20 64.80 10.70
14.60 71.50 62.80 26.80 81.60 25.10
95.20 61.80 20.60 36.20 86.60 26.20
90.30 93.40 2.20 28.30 74.00 93.80
10.40 27.50 53.70 17.90 60.70 23.80
52.80 93.40 53.70 63.90 33.70 59.30
60.50 62.20 41.00 9.70 18.40 50.10
76.90 31.00 62.60 14.10 18.80 87.50
27.30 71.60 46.90 28.30 39.50 13.00
82.70 27.40 63.90 53.30 12.60 81.20
30.30 74.90 44.50 40.50 23.10 82.20
38.30 65.70 57.20 71.00 25.80 84.80
22.30 75.30 27.20 15.30 40.20 20.70
80.40 38.90 89.20 72.40 55.30 70.20
95.60 5.00 75.40 96.50 8.40 72.70
25.70 8.40 90.60 40.20 43.90 61.10
11.50 65.90 55.00 20.80 48.60 84.70
86.60 13.50 79.20 26.60 95.60 92.80
10.50 84.10 30.00 61.30 37.60 63.40
39.50 62.70 13.90 62.90 23.10 81.40
27.00 67.50 94.20 55.20 49.40 34.30
8.10 25.10 74.60 89.20 54.60 35.60
97.00 95.30 77.10 12.40 8.60 63.10
11.50 16.50 70.70 5.70 38.20 40.40
45.50 73.40 55.20 74.50 95.10 14.80
71.90 49.70 84.30 92.10 52.40 24.90
21.20 46.00 63.70 88.90 5.40 10.70
71.00 64.60 84.00 98.00 43.50 1.40
46.70 32.60 24.40 36.50 10.60 14.60
18.60 67.40 92.00 62.40 36.10 71.30
39.60 74.10 89.60 15.00 73.30 89.30


I've currently been able to add the individual rows, attaining the proper maximum value of the sum of the rows (and the index where it occurs). However, I've been unable to add the correct number of columns (it somehow counts to 50 columns when there are only 6, and isn't adding the values of each column, instead it adds some values I have no idea where they came from). Here's my current code:

const int NUM_ROWS = 50;
const int NUM_COLS = 6;
double scores[NUM_ROWS][NUM_COLS];
double sumRows = 0, sumColumns = 0;
int colIndex, rowIndex;
double highestRow = 0, highestCol = 0;

fstream fin("scores.txt");

while (! fin.eof())
{
for (int i = 0; i < NUM_ROWS; i++)
{
for (int j = 0; j < NUM_COLS; j++)
{
fin >> scores[i][j];
}
}
}

fin.close();

for(int i = 0; i < NUM_ROWS; i++)
{
for(int j = 0; j < NUM_COLS; j++)
{
sumColumns += scores[j][i];
}

cout << "The sum of " << i+1 << " Column is " << sumColumns << endl;

if (sumColumns > highestCol)
{
highestCol = sumColumns;
colIndex = i+1;
}

sumColumns = 0;
}

cout << endl;

for(int i = 0; i < NUM_ROWS; i++)
{
for(int j = 0; j < NUM_COLS; j++)
{
sumRows += scores[i][j];
}

cout << "The sum of " << i+1 << " Row is " << sumRows << endl;

if (sumRows > highestRow)
{
highestRow = sumRows;
rowIndex = i+1;
}

sumRows = 0;
}

cout << "The total of the column with the highest total is: " << highestCol << " and occurs at Column #" << colIndex << endl;
cout << "The total of the row with the highest total is: " << highestRow << " and occurs at Row #" << rowIndex << endl;


Thanks in advance!

Answer

The problem is here:

for(int i = 0; i < NUM_ROWS; i++)
{
    for(int j = 0; j < NUM_COLS; j++)
    {
        sumColumns += scores[j][i];
    }

    cout << "The sum of " << i+1 << " Column is " << sumColumns << endl;

    if (sumColumns > highestCol)
    {
        highestCol = sumColumns;
        colIndex = i+1;
    }

    sumColumns = 0;
}

As you can see you iterate through the rows in the outer loop and through the columns in the inner, but the sumColumns isn't adding the correct values.

Replace this:

 for(int i = 0; i < NUM_ROWS; i++)
 {
    for(int j = 0; j < NUM_COLS; j++)
    {
        sumColumns += scores[j][i];
    }

with this:

for(int j = 0; j < NUM_COLS; j++)
{
    for(int i = 0; i < NUM_ROWS; i++)
    {
        sumColumns += scores[i][j];
    }