Find the highest and lowest value of a multidimensional array

Hi, I am currently working on an assignment that asks me to input test scores for 5 students for three tests using multi dimensional arrays and determined the average test score as well as the highest and lowest test score for each student. I was able to determine the average for each student however I am having trouble finding the highest and lowest score for each student. Could somebody please help me? I have included the question in the program. Thank you

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
//Ask the user to input the test scores of 5 students for 3 tests.

//Then for each student compute the average test score,

//as well as the student's highest and lowest test score

//and print them out.

#include <iostream>
using namespace std;

const int STUDENTS = 5;
const int TESTS = 3;

int main ()
{
    int score[STUDENTS][TESTS];
    int s; //Represents students
    int t; //Represents tests
    int highest;
    int lowest;
    float Average = 0;
    
    
    cout << "Input tests scores of 5 students for 3 tests: \n\n";
    
    for (s=0; s < STUDENTS; s++)
    {
        int Total = 0;
        
        for (t=0; t < TESTS; t++)
        {
            cout << "Student #" << s+1 << " - Test #" << t+1 << ": ";
            cin >> score[s][t];
            Total += score[s][t];
        }
        
        Average = float(Total) / TESTS;
        cout << "The average score is " << Average << endl << endl;
        
    }
        highest = score[0][0];
        lowest = score[0][0];
    
    cout << "===============================================================================\n\n";
    
        for (s=0; s < STUDENTS; s++)
        {
            for (t=0; t < TESTS; t++)
                {
                if(score[s][t] > highest)
                    highest = score[s][t];
                else
                    if (score[s][t] < lowest)
                    lowest = score[s][t];
                

                    }
            
            cout << "The highest score for student #" << s+1 << " is " << highest << endl;
            cout << "The lowest score for student #" << s+1 << " is " << lowest << endl << endl;
        }

    return 0;
}
Last edited on
Hey, the problem is that you're not restarting the variables for each student. You should do that per outter loop iteration to avoid testing the test scores of student 1 with student 2's scores for example. Add the following after your 2 cout statements on line 62:

1
2
highest = score[s][t];
lowest = score[s][t];
Works perfectly! Thank you so much for the help!
Topic archived. No new replies allowed.