Calculate the average

How would I calculate the average of the numbers from the input file. I would like to put the average right after the last number of each class. Also each test has different weights.

Test #1 - 10%
Test #2 - 15%
Test #3 - 20%
Test #4 - 25%
Test #5 - 30%

Example:
CS2308
50.9
55.2
60.5
65.8
45.7
average

Input File:
Nancy Peterson
22
510 Tumble Dr., San Gabriel, TX 57981
3
(666) 759 - 2249
492-35-5984
CS1428
80.9
90.2
98.5
89.8
97.7
CS2308
50.9
55.2
60.5
65.8
45.7
CS2308
60.9
55.2
60.5
65.8
60.7
Joe Doe
19
951 Moore St., San Marcos, Tx 77688
4
(555) 759 - 2249
555-35-5984
CS1428
80.9
90.2
98.5
89.8
97.7
CS2308
50.9
55.2
60.5
65.8
45.7
CS2308
60.9
55.2
60.5
65.8
60.7
John Doe
19
951 Moore St., San Marcos, Tx 77688
4
(555) 759 - 2249
555-35-5984
CS1428
80.9
90.2
98.5
89.8
97.7
CS2308
50.9
55.2
60.5
65.8
45.7
CS2308
60.9
55.2
60.5
65.8
60.7

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
  #include <iostream>

#include <fstream>

#include <string>

#include<iomanip>

using namespace std;



int main()

{

    string name, social, address, telephone, course, header;

    int n, age, years;

    double test;

    double average;

    const int NUMBER_OF_COURSES = 3;

    const int NUMBER_OF_SCORES = 5;

    int min_students = 1,

        max_students = 100;

    //number of students

    cout << "Enter how many students do you want to see:" << endl;

    cin >> n;

    // Validate the input while loop

    while (n < min_students || n > max_students )

    {

        cout << "You should have at least "<< min_students << " student"
        << " and more than " << max_students << " students" << endl;

        //get number of students again

        cout << "Enter how many students do you want to see:";

        cin >> n;

    }

    //Open Input and Output file

    ifstream fin;

    fin.open("Input.txt");

    if (!fin) {

        cout << "could not open file" << endl;
fin.ignore();
        return -1;

    }

    ofstream fout;

    fout.open("Output.txt");

    if (!fout) {

        cout << "could not open file" << endl;

        return -1;

    }


    // Ask for some info

    for (int i = 0; i < n; ++i) {

        getline(fin,name);

        fin >> age;

        fin.ignore();

        getline(fin,address);

        fin >> years;

        fin.ignore();

        getline(fin,telephone);

        getline(fin,social);

        fout << name << endl;
        fout << age << endl;
        fout << address << endl;
        fout << years << endl;
        fout << telephone << endl;
        fout << social << endl;


        for (int j = 0; j < NUMBER_OF_COURSES; ++j) {
            fin.ignore();

            getline(fin,course);

            fout << course << endl;


            for (int k = 0; k < NUMBER_OF_SCORES; k++) {

                fin >> test;

                fin.ignore();

                 fout << test << endl;


            }

        }

    }

    cout << "Data written to output file." << endl;

    fin.close();
    fout.close();


    return 0;



        }
Last edited on
Keep a running total, like
total += test;

Then output
average = total / NUMBER_OF_SCORES
So, you have a list of five test weights:

    { 10, 15, 20, 25, 30 }

(which conveniently sum to 100).

And you have lists of test scores for each test. Here is one of the lists:

    { 80.9, 90.2, 98.5, 89.8, 97.7 }

(which conveniently is also five elements long).

Frankly, I don’t think Nancy has much to worry about for her grade. But I suppose the professor must give her an official final grade anyway.


Test grades rate at 100 points. What you are being asked to do is twist each of Nancy’s grades to only be worth, say, 10 points.

So, for her first test, Nancy earned 80.9 * 0.1 = 8.09 points. Nancy takes this home and her parents say, “stop chewing bubble gum and start studying!”

But Nancy says, “No, that’s 8 points out of 10. I got a B.”

So her parents take her out for ice cream.


The next exam is adjusted to be only worth 15 points. So again we take Nancy’s 100-point test and scale it: 90.2 * 0.15 = 13.53 points.

Again, that’s 13.53 points out of 15, an A, so Nancy’s parents take her out for ice cream again. Nancy likes the blue sherbet with gelato. She wonders if that cute guy two seats over would like to take her out to get some ice cream.


We keep going. Each grade has a weight applied to it:

    weight      10    15     20    25     30
    unweighted  80.9  90.2   98.5  89.8   97.7
    weighted    8.09  13.53  19.7  22.45  29.31

Now all we need to do is sum all the weighted grades to get Nancy’s final course grade:

    8.09 + 13.53 + 19.7 + 22.45 + 29.31
  = 93.08

A solid A! Nancy can’t stand ice cream now, but that cute guy has introduced her to power shakes and track and field. Nancy feels much better now, and is considering how to introduce him to her parents, because he has a nose ring.



The salient features are that for each set of grades in a course, you have two same-sized arrays: the weights array and the grades array.

This works just like your standard average:
    • before summing a grade into the running total, multiply it by the weight
    • when you are done, you must divide by 100 (since the weights are * 100)


The way to figure this out is to write a separate, small program that does nothing more than find the average of a bunch of numbers input by the user. Here is some help:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

int main()
{
  int n;
  cout << "n? ";
  cin >> n;

  double sum = 0.0;

  for (int i = 0; i < n; i++)
  {
    double grade;
    cout << "grade? ";
    cin >> grade;

    sum = /* what do I do here? */
  }

  double average = /* what do I do here? */

  cout << "The average grade is: " << average << "\n";
}

Once you get that working, fix it to apply a weight on every loop. You might as well set n = 5 and use the same weights as in your homework assignment.

And once you have done that, you will see exactly what you need to do to your loop on lines 119..128 to get a weighted final grade.


Hope this helps.

[edit] Fixed some formatting
Last edited on
Topic archived. No new replies allowed.