2D Arrays, and how to pull the Highest number, Lowest Number, and Average from one Column within them?

Hello! This is my first time posting, so please excuse any missteps in etiquette.

Today I come to you with a problem that I just can't seem to wrap my head around! I've been tasked with creating a simple grading program, that accepts the input of 3 tests each for 5 students, and then displays the Average, Highest Grade, and Lowest Grade for each test.

I've managed to successfully input all of the test values I need into the array, but a few strange things happen.

Firstly, it displays a "garbage" value for the average, no matter what values I put in, how I change the array, etc.

Secondly, the program always reports 0 as the lowest number, even if I do not input zero as one of the testing numbers.

Thirdly, while on occasion the highest number for a column is displayed correctly, it is more often than not a different number from within the 2D array, although I have seen it display "garbage" values such as the average displays on occasion.

These are just the currently most vexing problems I'm facing, before even trying to validate that the user input is correct.

Any insight that can be offered based on my code would be much appreciated!

Thank you very much,

Regressive
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
          //Student Grading Statistics Extra Credit Assignment

        #include<iostream>

        using namespace std;

        const int TOTAL_STUDENTS=5;
        const int TOTAL_TESTS=3;
        void readStuGrades(int stuGrades[][TOTAL_TESTS]);
        void printStuGrades(int stuGrades[][TOTAL_TESTS]);
        void getExamAvgMinMaxGrade(int stuGrades[][TOTAL_TESTS], int exam, float&avg, int&low, int&high);
        void printExamStatistics (int exam, float avg, int low, int high);

        //Global variables (TOTAL_STUDENTS and TOTAL_TESTS) were declared as constant integers since their values cannot be exceeded during program execution.
        //Function prototype declarations
        //readStuGrades is declared as a two dimensional array, and will be accepting grade values from the user for a total of 5 tests.
        //printStuGrades is also declared as a two dimensional array and will be used to display the grade values inputted by the user in a table format.
        //getExamAvgMinMaxGrade will be accepting the values from the two dimensional array, stugrades. The exam variable is used to keep track of the average, high and low values for each specific exam.
        //It will be used to do all of the calculations for the average, high and low values.
        //The avg, low and high values are being called by reference, since we would like to have the most updated values at all times during program execution.
        //printExamStatistics will be used to display the grade report for the students in their respective exams.

        int main ()

        {

        float avg;
        int low;
        int high;
        int exam;
        int stugrades[TOTAL_STUDENTS][TOTAL_TESTS];

        readStuGrades(stugrades);

        //Function call to readStuGrades, a two dimensional array which will accept user input and perform validation.

        cout <<endl;
        cout <<"Student Grades\n\n";
        cout <<"---------------\n\n";

        cout <<"Student\t\t" <<"Exam 1\t\t" <<"Exam 2\t\t" <<"Exam 3\n\n";

        //Basic heading setup for the table that will be generated by the printStuGrades.

        printStuGrades(stugrades);

        //Function call to printStuGrades.

        cout <<endl;
        cout <<endl;

        cout <<"Grade Report\n\n";
        cout <<"---------------\n\n";

         cout <<"Exam\t\t" <<"AVG\t\t" <<"LOW\t\t" <<"HIGH\n\n";

        //Basic heading structure setup for displaying the grade report section of the program.

        getExamAvgMinMaxGrade(stugrades,1,avg,low,high);

        cout <<endl;
        cout <<endl;


        //Function call to getExamAvgMinMaxGrade.
        return 0;

        }

        void readStuGrades(int stuGrades[][TOTAL_TESTS]) {

            for(int i=1; i <=TOTAL_STUDENTS; i++)
        {
        cout <<"Enter the 3 grades for student #" <<i <<" (separate by a space): ";

        for(int j=1; j<=TOTAL_TESTS; j++) {

        cin >> stuGrades[i][j];

        do {
            if (stuGrades[i][j] < 0 || stuGrades[i][j] > 100) {

                cout <<"\nGrade values must not be less than zero or greater than 100.\n";
                cout <<"Please re-enter grade. ";
                cin >> stuGrades[i][j];
            }
            else
            {
            break;
            }

        }while(true);
        }
    }
        }

        //This function allows the user to input a total of three grades for 5 students.
        //Following this is a do while loop that validates that the grade values entered by the user are within the range of being greater than 0 and less than 100.
        //If the user inputs a value that is not within the specified range, the program will display an error message to the user and request new values for the array.
        //Else, the program breaks out of the do-while loop and continues to the next sequential code block.
        //The for loops are used to ensure that the values are stored properly within the 2D array (row then column).

         void printStuGrades(int stuGrades[][TOTAL_TESTS]) {

        for(int i=1; i <=TOTAL_STUDENTS; i++)
        {
            cout <<i <<"\t\t";

        for(int j=1; j<=TOTAL_TESTS; j++) {

            cout <<stuGrades[i][j] <<"\t\t";

         }
         cout <<endl;
         cout <<endl;
         }
}

         //This function simply displays the values that are stored in the 2D array.
         //The first for loop iterates through the rows in the array.
         //The second for loop iterates through the columns in the array.


    void getExamAvgMinMaxGrade(int stuGrades[][TOTAL_TESTS], int exam, float&avg, int&low, int&high){

    double total=0;

    for(int grades=0; grades<TOTAL_STUDENTS; grades++)
    {

        for(int a=0; a<exam; a++) {

        total+=stuGrades[grades][TOTAL_STUDENTS];
    }

    }

    cout.setf(ios::showpoint);
    cout.setf(ios::fixed);
    cout.precision(2);

    avg= total/TOTAL_STUDENTS;

    high=0;

    for(int x=0; x<TOTAL_STUDENTS; x++) {

        if(stuGrades[TOTAL_TESTS][TOTAL_STUDENTS] > high) {

            high=stuGrades[x][TOTAL_STUDENTS];
        }
    }

    low=0;

    for(int y=0; y<TOTAL_STUDENTS; y++){

        if(stuGrades[TOTAL_TESTS][TOTAL_STUDENTS] <high ) {

            low=stuGrades[y][TOTAL_STUDENTS];
        }
    }

    printExamStatistics(exam,avg,low,high);
}

    void printExamStatistics (int exam, float avg, int low, int high){


    for(int i=1; i <= exam; i++)

        {
            cout <<i;
        }
            for(int a=1; a<=exam; a++) {
            cout <<"\t\t" <<avg <<"\t\t" <<low <<"\t\t" <<high;

            }
            cout <<endl;
            cout <<endl;

    for(int i=2 ; i <= exam+1; i++)

        {
            cout <<i;
        }

        cout <<endl <<endl;

    for(int i=3 ; i <= exam+2; i++)

        {
            cout <<i;
        }


    }



    //This function is used to display the result of the calculations from the getExamAvgMinMaxGrade function, in a table format.


Line 72 for(int i=1; i <=TOTAL_STUDENTS; i++)
Line 76 for(int j=1; j<=TOTAL_TESTS; j++)
Indexes start with 0 and end with size-1

Line 133 looks wrong total+=stuGrades[grades][TOTAL_STUDENTS];
You only add the last column to total.
Last edited on
Thanks for the reply, Thomas!

I've changed my code to 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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
        //Student Grading Statistics Extra Credit Assignment

        #include<iostream>

        using namespace std;

        const int TOTAL_STUDENTS=5;
        const int TOTAL_TESTS=3;
        void readStuGrades(int stuGrades[][TOTAL_TESTS]);
        void printStuGrades(int stuGrades[][TOTAL_TESTS]);
        void getExamAvgMinMaxGrade(int stuGrades[][TOTAL_TESTS], int exam, float&avg, int&low, int&high);
        void printExamStatistics (int exam, float avg, int low, int high);

        //Global variables (TOTAL_STUDENTS and TOTAL_TESTS) were declared as constant integers since their values cannot be exceeded during program execution.
        //Function prototype declarations
        //readStuGrades is declared as a two dimensional array, and will be accepting grade values from the user for a total of 5 tests.
        //printStuGrades is also declared as a two dimensional array and will be used to display the grade values inputted by the user in a table format.
        //getExamAvgMinMaxGrade will be accepting the values from the two dimensional array, stugrades. The exam variable is used to keep track of the average, high and low values for each specific exam.
        //It will be used to do all of the calculations for the average, high and low values.
        //The avg, low and high values are being called by reference, since we would like to have the most updated values at all times during program execution.
        //printExamStatistics will be used to display the grade report for the students in their respective exams.

        int main ()

        {

        float avg;
        int low;
        int high;
        int exam;
        int stugrades[TOTAL_STUDENTS][TOTAL_TESTS];

        readStuGrades(stugrades);

        //Function call to readStuGrades, a two dimensional array which will accept user input and perform validation.

        cout <<endl;
        cout <<"Student Grades\n\n";
        cout <<"---------------\n\n";

        cout <<"Student\t\t" <<"Exam 1\t\t" <<"Exam 2\t\t" <<"Exam 3\n\n";

        //Basic heading setup for the table that will be generated by the printStuGrades.

        printStuGrades(stugrades);

        //Function call to printStuGrades.

        cout <<endl;
        cout <<endl;

        cout <<"Grade Report\n\n";
        cout <<"---------------\n\n";

         cout <<"Exam\t\t" <<"AVG\t\t" <<"LOW\t\t" <<"HIGH\n\n";

        //Basic heading structure setup for displaying the grade report section of the program.

        getExamAvgMinMaxGrade(stugrades,1,avg,low,high);

        cout <<endl;
        cout <<endl;


        //Function call to getExamAvgMinMaxGrade.
        return 0;

        }

        void readStuGrades(int stuGrades[][TOTAL_TESTS]) {

            for(int i=0; i <=TOTAL_STUDENTS - 1; i++)
        {
        cout <<"Enter the 3 grades for student #" << (i+ 1) <<" (separate by a space): ";

        for(int j=0; j<=TOTAL_TESTS -1; j++) {

        cin >> stuGrades[i][j];

        do {
            if (stuGrades[i][j] < 0 || stuGrades[i][j] > 100) {

                cout <<"\nGrade values must not be less than zero or greater than 100.\n";
                cout <<"Please re-enter grade. ";
                cin >> stuGrades[i][j];
            }
            else
            {
            break;
            }

        }while(true);
        }
    }
        }

        //This function allows the user to input a total of three grades for 5 students.
        //Following this is a do while loop that validates that the grade values entered by the user are within the range of being greater than 0 and less than 100.
        //If the user inputs a value that is not within the specified range, the program will display an error message to the user and request new values for the array.
        //Else, the program breaks out of the do-while loop and continues to the next sequential code block.
        //The for loops are used to ensure that the values are stored properly within the 2D array (row then column).

         void printStuGrades(int stuGrades[][TOTAL_TESTS]) {

        for(int i=0; i <=TOTAL_STUDENTS-1; i++)
        {
            cout <<(i+1) <<"\t\t";

        for(int j=0; j<=TOTAL_TESTS-1; j++) {

            cout <<stuGrades[i][j] <<"\t\t";

         }
         cout <<endl;
         cout <<endl;
         }
}

         //This function simply displays the values that are stored in the 2D array.
         //The first for loop iterates through the rows in the array.
         //The second for loop iterates through the columns in the array.


    void getExamAvgMinMaxGrade(int stuGrades[][TOTAL_TESTS], int exam, float&avg, int&low, int&high){

    double total=0;

    for(int grades=0; grades<=TOTAL_STUDENTS-1; grades++)
    {


        total+=stuGrades[exam][grades];


    }

    cout.setf(ios::showpoint);
    cout.setf(ios::fixed);
    cout.precision(2);

    avg= total/TOTAL_STUDENTS;

    high=0;

    for(int x=0; x<TOTAL_STUDENTS; x++) {

        if(stuGrades[x][exam] > high) {

            high=stuGrades[x][exam];
        }
    }

    low = high;

    for(int y=0; y<TOTAL_STUDENTS; y++){

        if(stuGrades[y][exam] < low ) {

            low=stuGrades[y][exam];
        }
    }

    printExamStatistics(exam,avg,low,high);
}

    void printExamStatistics (int exam, float avg, int low, int high){


    for(int i=0; i < exam; i++)

        {
            cout <<(i + 1);
        }
            for(int a=1; a<=exam; a++) {
            cout <<"\t\t" <<avg <<"\t\t" <<low <<"\t\t" <<high;

            }
            cout <<endl;
            cout <<endl;

    for(int i=1 ; i < exam; i++)

        {
            cout <<(i+1);
        }

        cout <<endl <<endl;

    for(int i=2 ; i < exam+1; i++)

        {
            cout <<(i+1);
        }


    }



    //This function is used to display the result of the calculations from the getExamAvgMinMaxGrade function, in a table format. 


Are there any other things I've missed?
Your getExamAvgMinMaxGrade doesn't look right. In readStuGrades and printExamStatistics you got your nested loops right. You should apply the same principle to getExamAvgMinMaxGrade as well.
As a general rule loop if you have a 2D array you need 2 nested loops to process all elements.
Topic archived. No new replies allowed.