Find max, average values in file

Greetings everyone,

My class is the "review basic code" phase, as it has just started up.
The goal is to find the highest value from a file, average, and output names accordingly (see in comments).

My trouble has been with finding the max and average values within the data.
I have seen this done with vectors and arrays, but my teacher says there is an
even SIMPLER way to do this WITHOUT any of those. I have spent a considerable amount of time searching for an answer, and maybe I'm just over thinking it.
If anyone knows the simplest way to find these, I would love to know.
Any help would be GREATLY appreciated. Thanks!

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
//Goal - read in file containing names and grades
//       calculate highest grade, average grade, output names LOWER than //average, output names EQUAL to highest

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <cmath>

using namespace std;

int main()
{
    ifstream inFile;//read in file
    inFile.open("students.dat");//open file

    if (!inFile) {//if file failed, display error
        cerr << "Can not open file!" << endl;
        system("pause");
        return 1;
    }

    string fName, lName;//declare first and last names
    double grade;//declare grades

    while(inFile >> fName >> lName >> grade) {//assign variables to data
        cout << fName << " " << lName << " " << grade << endl;//display data
    }

    system("pause");
    return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int cnt = 0 ;
double highest_seen_so_far = 0 ;
double total = 0 ;

each time through the loop:
     read names, and a grade
     if( grade >  highest_seen_so_far) highest_seen_so_far = grade ;
     total += grade ; // add grade to total
     ++cnt ; // increment cnt

after we are done with the loop:
      highest grade is the highest_seen_so_far (we have seen all)
      number of grades read is cnt
      total of all the grades is total

compute average grade
print out highest grade, average grade
Thank you for the reply.

I understand what is SUPPOSED to happen here. But after messing with it for a while, the code you suggest only gathers up the highest grade. Total and cnt do not work. I'm in the process of tinkering... for now, here is the outcome.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int cnt = 0;
    double highest_seen_so_far = 0;
    double total = 0;

    if (grade > highest_seen_so_far) {
        highest_seen_so_far = grade;
        total += grade;
        ++cnt;
    }

    double average = total/cnt;//total = highest grade, cnt = 1... thus 95/1

    cout << endl;
    cout << "Number of grades read: " << cnt << endl;//only reads 1 grade
    cout << "Highest grade: " << highest_seen_so_far << endl;//works, prints highest grade
    cout << "Average grade: " << average << endl;//results from only 1 grade, prints highest grade 



Ichabod Crane 89.9
Harry Potter 90.2
Frodo Baggins 78.2
Alex Cross 95

Number of grades read: 1
Highest grade: 95
Average grade: 95
Press any key to continue . . .
You don't show your loop. Nevertheless, having the "increase count and add to sum" inside conditional "do this only for entries that have higher grade than highest_seen_so_far" is clearly not what you want.
Correct. I don't want the total and cnt to be conditional.
I've changed it to a FOR loop shown below...

1
2
3
4
5
6
7
8
9
10
11
12
 for (grade = "???" > highest_seen_so_far; cnt++) {//if grade = 100 then it will only cout one grade...100, if 0, then undefined
        highest_seen_so_far = grade;
    }

    total += grade;

    double average = total/cnt;//total = highest grade, cnt = 1... thus 95/1

    cout << endl;
    cout << "Number of grades read: " << cnt << endl;//only reads 1 grade
    cout << "Highest grade: " << highest_seen_so_far << endl;//works, prints highest grade
    cout << "Average grade: " << average << endl;//results from only 1 grade, prints highest grade 
In the pseudo-code by JLBorges, the lines 6-9 are within a loop. Your for loop does not read anything. Your while loop did.
I've got the loop down. I understand now.
However, this method does not work since the grades are not in ascending order.
Therefore, when it loops and looks for "grades higher than itself", it ignores the grades lower than itself. If the grades in the file are not in ascending order, it ignores one (shown below-Frodo Baggins).

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
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>

using namespace std;

int main()
{
    ifstream inFile;
    inFile.open("./students.dat");

    string fName;
    string lName;
    double grade;
    double cnt = 0;
    double cntGrade = 0;
    double total;
    double average;

    while(inFile >> fName >> lName >> grade) {
        cout << fName << " " << lName << " " << grade << endl;
        if(grade > cntGrade) {
            cntGrade = grade;
            cout << "--cntGrade: " << cntGrade << endl;
            total += cntGrade;
            cout << "--total: " << total << endl;
            cnt++;
            cout << "--cnt: " << cnt << endl;
            average = total / cnt;
            cout << "--average: " << average << endl;
        }
    }
    inFile.close();
    system("pause");
    return 0;
}



Ichabod Crane 89.9
--cntGrade: 89.9
--total: 89.9
--cnt: 1
--average: 89.9
Harry Potter 90.2
--cntGrade: 90.2
--total: 180.1
--cnt: 2
--average: 90.05
Frodo Baggins 78.2
Alex Cross 95
--cntGrade: 95
--total: 275.1
--cnt: 3
--average: 91.7
Press any key to continue . . .
Problem has been solved!
Below is the code and output if anyone is curious.
Thank you all for helping me out.

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
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <iomanip>

using namespace std;

int main()
{
    ifstream inFile;
    inFile.open("./students.dat");

    string fName;
    string lName;
    string lessName;
    string moreName;
    double grade;
    double cnt = 0;
    double cntGrade = 0;
    double total;
    double average;
    double lessGrade;
    double moreGrade;

    while(inFile >> fName >> lName >> grade) {
        cout << fName << " " << lName << " " << grade << endl;
        if(grade > 0) {
            cntGrade = grade;
            //cout << "--cntGrade: " << cntGrade << endl;
            cnt++;
            //cout << "--cnt: " << cnt << endl;
        }
        total += cntGrade;
        average = total / cnt;
        if(grade < average) {
            lessGrade = grade;
            lessName = fName + " " + lName;
        }
        if(grade = cntGrade) {
            moreGrade = grade;
            moreName = fName + " " + lName;
        }
    }

    cout << endl;
    cout << "Highest grade: " << cntGrade << endl;
    cout << "Average grade: " << average << endl;
    cout << "Grades meeting highest: " << endl << setw(20) << moreGrade << " " << moreName << endl;
    cout << "Grades below average: " << endl << setw(20) << lessGrade << " " << lessName << endl;

    inFile.close();
    cout << endl;
    system("pause");
    return 0;
}



Ichabod Crane 89.9
Harry Potter 90.2
Frodo Baggins 78.2
Alex Cross 95

Highest grade: 95
Average grade: 88.325
Grades meeting highest:
                  95 Alex Cross
Grades below average:
                78.2 Frodo Baggins

Press any key to continue . . .
Topic archived. No new replies allowed.