Getting large numbers as output

I'm trying to finish up my program, but I'm getting very large numbers as the output for the averages after running the program. This program takes data from the user and stores it in a 3x3 array and then does an average on the rows, columns and entire array. The program is big and ugly and probably could be wrote better but its the best I could do:

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <iostream>
#include <cmath>
using namespace std;

const int ROWS = 3, COLUMNS = 3;

// Function:    dataManipulation
// Purpose:     To get data from user and
//              get the average and maximum
// Variables:   A lot

int dataManipulation()
{
    int sum = 0, row_sum = 0, column_sum = 0, row_average, column_average,
    average, average_firstrow, average_secondrow, average_thirdrow,
    average_firstcolumn, average_secondcolumn, average_thirdcolumn,
    highest_row = 0, highest_column = 0, highest_total = 0,
    max_firstrow, max_secondrow, max_thirdrow,
    data[ROWS][COLUMNS];

    for (int x = 0; x < ROWS; x++)
    {
        for (int y = 0; y < COLUMNS; y++)
        {
            if (y == 0) // Reset the variables if we are at the start of the loop and display set
            {
                row_sum = 0;
                column_sum = 0;
                row_average = 0;
                column_average = 0;
                cout << "SENSOR SET " << (x + 1) << endl;
                cout << "------------\n";
            }

            cout << "Sensor " << (y + 1) << ": ";
            cin >> data[x][y];
            cin.ignore();

            sum += data[x][y]; // Running total that does not reset
            row_sum += data[x][y]; // Running total of the rows
            column_sum += data[y][x]; // Running total of the columns

            if (y == 2) // Only do this the last time the loop goes through
            {
                row_average = row_sum / COLUMNS;
                column_average = column_sum / COLUMNS;
            }

            if (data[x][y] > highest_row) // Find the highest number in the row
            {
                highest_row = data[x][y];
            }
        }

        if (x == 0)
        {
            average_firstrow = row_average;
            average_firstcolumn = column_average;
            max_firstrow = highest_row;
        }

        else if (x == 1)
        {
            average_secondrow = row_average;
            average_secondcolumn = column_average;
            max_secondrow = highest_row;
        }

        else if (x == 2)
        {
            average = sum / pow(COLUMNS, 2);
            average_thirdrow = row_average;
            average_thirdcolumn = column_average;
            max_thirdrow = highest_row;
        }

        cout << endl;
    }

    cout << "----------------------------------------------\n\n";
    cout << "Average for sensor set 1: " << average_firstrow << endl;
    cout << "Average for sensor set 2: " << average_secondrow << endl;
    cout << "Average for sensor set 3: " << average_thirdrow << endl << endl;
    cout << "Average for first sensor in every set: " << average_firstcolumn << endl;
    cout << "Average for second sensor in every set: " << average_secondcolumn << endl;
    cout << "Average for third sensor in every set: " << average_thirdcolumn << endl << endl;
    cout << "The maximum number for the first set of sensors is " << max_firstrow << endl;
    cout << "The maximum number for the second set of sensors is " << max_secondrow << endl;
    cout << "The maximum number for the third set of sensors is " << max_thirdrow << endl << endl;
    cout << "The average sensor reading is " << average << endl;
    cout << "The maximum sensor reading is " << highest_row << endl;
}

int main()
{
    cout << "Geological sensor data\n";
    cout << "----------------------\n\n";
    cout << "Enter the following data\n\n";

    dataManipulation();

    cin.get();
    return 0;
}


If the program is ran, it gives large numbers for the average for the columns 1 and 2 and the correct answer for column 3 for some reason. I think variables are not being initialized somewhere, but I don't know where. Any help would be appreciated.
1. Get and store input.
2. Manipulate data.

Not:
1. Get 1 input.
2. Attempt to manipulate data not yet input.
3. repeat.

1
2
3
            sum += data[x][y]; // Running total that does not reset
            row_sum += data[x][y]; // Running total of the rows
            column_sum += data[y][x]; // Running total of t 



column_sum... when do you think the row index is also a valid column index?
Are you saying that I should move all of the "if" statements out of the loops and it will work?

What I'm thinking is since I need to get the average of the columns, the column_sum would do that because it would look like this:

1st time through: column_sum [0][0]
2nd time through: column_sum [1][0]
3rd time through: column_sum [2][0]

See, that is what I need it to add. After adding all those up I will divide by the total.

I haven't worked with arrays in C++ very much so I apologize if I can't yet understand what you're talking about.

EDIT: I don't see why the average for row_sum is working while the column_sum is not... If none of them worked, it might be a little easier to see.
Last edited on
Column_sum doesn't work, because you're getting input by row. So when you get the sum of the first column, after you've gotten input for the first row, only one of the 3 values is valid.

When you get the sum of the first column after you've gotten input for the 2nd row, only two of those three values are valid.

When you get the sum of the first column after you've gotten input for the 3rd row, all three values are valid.
I tried to put the column_sum outside of the first loop like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
            cout << "Sensor " << (y + 1) << ": ";
            cin >> data[x][y];
            cin.ignore();

            sum += data[x][y]; // Running total that does not reset
            row_sum += data[x][y]; // Running total of the rows

            if (y == 2) // Only do this the last time the loop goes through
            {
                row_average = row_sum / COLUMNS;
            }

            if (data[x][y] > highest_row) // Find the highest number in the row
            {
                highest_row = data[x][y];
            }
        }

        column_sum += data[y][x]; // Running total of the columns
        column_average = column_sum / COLUMNS;

        if (x == 0)
        {
            average_firstrow = row_average;


But it won't compile. What does the error "name lookup of 'y' changed for ISO 'for' scoping"?
I ended up adding another loop to the program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    for (y = 0; y < COLUMNS; y++)
    {
        column_sum = 0;

        for (x = 0; x < ROWS; x++)
        {
            column_sum += data[x][y];
        }

        if (y == 0)
        {
            average_firstcolumn = column_sum / ROWS;
        }

        if (y == 1)
        {
            average_secondcolumn = column_sum / ROWS;
        }

        if (y == 2)
        {
            average_thirdcolumn = column_sum / ROWS;
        }
    }


Now the columns average properly.

But my question is.. is there anyway to do this average in the other loop? I'm trying to do this program with only one loop (nested loops don't count as additional loops).
I re-wrote the program to make it a little bit smaller:

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <cmath>
using namespace std;

const int ROWS = 3, COLUMNS = 3;

// Function:    dataManipulation
// Purpose:     To get data from user and
//              get the average and maximum
// Inputs:      Integers to hold various data

int dataManipulation()
{
    int i, x, y, sum = 0, row_sum = 0, column_sum = 0, highest = 0,
    average, row_average, column_average,
    data[ROWS][COLUMNS], row_array[ROWS], column_array[ROWS], maximum[ROWS];

    for (x = 0; x < ROWS; x++)
    {
        row_sum = 0;
        cout << "SENSOR SET " << (x + 1) << endl;
        cout << "------------\n";

        for (y = 0; y < COLUMNS; y++)
        {
            cout << "Sensor " << (y + 1) << ": ";
            cin >> data[x][y];
            cin.ignore();

            sum += data[x][y]; // Running total that does not reset
            row_sum += data[x][y]; // Running total of the rows

            if (y == 2) // Only do this the last time the loop goes through
            {
                row_average = row_sum / COLUMNS;
            }

            if (data[x][y] > highest) // Find the highest number in the row
            {
                highest = data[x][y];
            }
        }

        average = sum / pow(ROWS, 2);

        row_array[x] = row_average;
        maximum[x] = highest;

        cout << endl;
    }

    for (i = 0; i < ROWS; i++)
    {
        cout << "The average for SENSOR SET " << (i + 1) << " is " << row_array[i] << endl;
    }

    cout << endl;

    for (i = 0; i < ROWS; i++)
    {
        cout << "The maximum number in SENSOR SET " << (i + 1) << " is " << maximum[i] << endl;
    }

    cout << "\nThe overall average is " << average << endl;
    cout << "The maximum overall number is " << highest << endl;
}

int main()
{
    cout << "Geological sensor data\n";
    cout << "----------------------\n\n";
    cout << "Enter the following data\n\n";

    dataManipulation();

    cin.get();
    return 0;
}


I'm still not sure where to compute the column_sum and column_average values. Is it possible to average the columns in the same loop as the rows or should I just go with a separate loop?
I wouldn't average the rows in the loop you're averaging the rows.

Consider the following:

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
void getInput( int data[][COLUMNS], unsigned rows )
{
    for (unsigned row=0; row < rows ; ++row)
    {
        std::cout << "Sensor Set " << row+1 << "\n------------\n" ;
        for ( unsigned col=0; col < COLUMNS; ++col )
        {
            std::cout << "Sensor " << col+1 << ':' ;
            std::cin >> data[row][col] ;
        }
    }
}

void output( char const* label, int const * data, unsigned size )
{
    for ( unsigned i=0; i<size; ++i )
        std::cout << '\t' << label << ' ' << i+1 << ": " << data[i] << '\n' ;
}

void manipulateData()
{
    int data[ROWS][COLUMNS] ;

    getInput(data, ROWS) ;

    int sum = 0 ;
    int max = data[0][0] ;    
    int row_average[ROWS] = {} ;
    int row_max[ROWS] = {} ;
    int column_average[COLUMNS] = {} ;
    int column_max[COLUMNS] = { data[0][0], data[0][1], data[0][2] } ;

    for ( unsigned row=0; row < ROWS; ++row )
    {
        row_max[row] = data[row][0] ;

        for ( unsigned col=0; col < COLUMNS; ++col )
        {
            int reading = data[row][col] ;

            sum += reading ;

            if ( reading > max )
                max = reading ;

            if ( reading > row_max[row] )
                row_max[row] = reading ;

            if ( reading > column_max[col] )
                column_max[col] = reading ;

            column_average[col] += reading ;
            row_average[row] += reading;
        }

        row_average[row] /= COLUMNS ;
    }

    for ( unsigned col=0; col < COLUMNS; ++col )
        column_average[col] /= ROWS ;

    std::cout << "Average for\n" ;
    output( "Sensor Set", row_average, ROWS ) ;
    output( "Column", column_average, COLUMNS) ;

    std::cout << "Highest reading for\n" ;
    output( "Sensor Set", row_max, ROWS) ;
    output( "Column", column_max, COLUMNS) ;

    std::cout << "\nAverage reading: " << sum / (ROWS*COLUMNS)
              << "\nHighest reading: " << max << '\n' ;
}
Topic archived. No new replies allowed.