cant get average from arrays

I am trying to get the average scores for all of the students for each test. I have gotten my code to work but when the average scores are outputted its showing a crazy result when it should only show 5 numbers. can somebody check out my code.
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
  #include<iostream>
 #include<fstream>
 #include<string>
 #include<iomanip>
 using namespace std;


 const int MAX = 50;
 // a function called GetData to read and store data into two arrays,
 void GetData(ifstream& infile,string name[],int scores[][5],int& n)
 {
    n = 0;
    int i=0;
    int j=0;
    while(!infile.eof())
    {
    infile >> name[i];
    for(int j=0; j<5; j++)
    infile >> scores[i][j];
    i++;
    }
    n = i;
 }


 // a function called Average that is used to calculate the average test score and grade,
 void Average(int a[][5],double avg[],int no_of_students)
 {
    for(int i=0; i<no_of_students; i++)
    {
    double sum =0;
        for(int j=0; j<5; j++)
        sum+= a[i][j];
    avg[i] = sum/static_cast<double> (5);
    }
 }

 // function called PrintResults to output the results.
 void PrintResults(string name[],double avg[],int scores[][5],int n)
 {
 for(int i=0; i<n; i++){
 cout << left << setw(10)<< name[i];
    for(int k=0; k<5; k++)
        cout << right << setw(8) << scores[i][k];
    cout << endl;
 }
 cout << setw(8) <<"Average ";
 for(int i=0; i<n; i++)
     cout << setw(5) << avg[i];
     cout << endl;
 }

 int main()
 {
 // a one-dimensional array to store the students� names,
 string name[MAX];
 // a (parallel) two-dimensional array to store the test scores,
 int scores[MAX][5];
 // a parallel one-dimensional array to store grades.

 int no_of_students;
 double avg[MAX];

 ifstream infile("_mp.txt");

 if(!infile)
 {
    cout <<"unable to open file.so exiting from program" << endl;
    return 0;
 }

 GetData(infile, name, scores, no_of_students);
 infile.close();
 Average(scores,  avg, no_of_students);
 PrintResults(name,avg,scores,no_of_students);


 return 0;
 }
When you're asking for help, please reduce your code to a minimal compilable snippet (preferably with a small data sample) that reproduces the issue you are having. Often, boiling the code down to the essential elements will reveal the flaw. Also, instead of say "I get crazy results" provide what you expect to get and how it differs from what you actually get.


I am trying to get the average scores for all of the students for each test.

It looks more like you're calculating the averages test scores for each student rather than the scores of students for each test. The latter would result in 5 values, the former would result in the same number of values as the number of students.
Last edited on
One think which might be relevant. It's not a good idea to use eof() in the condition of the while loop (line 15). Don't do it.

I'd suggest replacing this code:
15
16
17
18
19
20
21
    while (!infile.eof())
    {
        infile >> name[i];
        for(int j=0; j<5; j++)
            infile >> scores[i][j];
        i++;
    } 


With something like this:
15
16
17
18
19
20
21
    while (infile >> name[i])
    {
        for (int j=0; j<5; j++)
            infile >> scores[i][j];
        
        i++;
    }


Here the body of the while loop is entered only after successfully reading the student name. To be more robust, it should also check the file status after attempting to read the 5 scores, but this would be at least a step in the right direction.
Thanks I am new to this site and new to c++. the expected output i want is
 average: some number, another number, another number, another number, another number

but I am getting the same number for my averages.
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
void Average(int a[][5],double avg[],int no_of_students)
 {
    for(int i=0; i<no_of_students; i++)
    {
    double sum =0;
        for(int j=0; j<15; j++)
        sum+= a[j][0];
    avg[i] = sum/static_cast<double> (15);
    }
 }

 // function called PrintResults to output the results.
 void PrintResults(string name[],double avg[],int scores[][5],int n)
 {
 for(int i=0; i<n; i++){
 cout << left << setw(10)<< name[i];
    for(int k=0; k<5; k++)
        cout << right << setw(8) << scores[i][k];
    cout << endl;
 }
 cout << setw(8) <<"Average ";
 for(int i=0; i < 5; i++)
     cout << setw(5)<< setprecision(2) << avg[i];
     cout << endl;
 
The average of a set of numbers is going to be the same number no matter how many times you output it. Are you sure you want an output of the average or are you wanting some other output?
Last edited on
but I am getting the same number for my averages.

Yes. You're calculating the average for the first column over and over. If you have 50 students, you take the average of the first 15 students' first score 50 times. If you have than 10 students, you take the average of the first 15 students' first score 10 times.

Hint: The number 15 has no place in your Average function and one of those loops should be done based on the number of scores per student.
Last edited on
Topic archived. No new replies allowed.