Calculate weighted average in Array

Im trying to find the weighted average of the five test grades for each course and store it in the same array. Currently I have the averages without any weight and it prints to the console. How would I store the weighted average of the 5 test grades for each course in the same array? And How would I print it to the Output file right under the 5th test grade?

Different weights
Test 1 = .10
Test 2 = .15
Test 3 = .20
Test 4 = .25
Test 5 = .30



Input File:
Student Grade Sheet, Texas State University
Nancy Peterson
A00591342
510 Tumble Dr., San Gabriel, TX 57981
(666) 759 - 2249
492-35-5984
22
3
CS2307
98.9
98.2
98.5
89.8
97.7
ART2308
50.9
55.2
60.5
65.8
45.7
HS2309
60.9
55.2
60.5
65.8
60.7
Student Grade Sheet, Texas State University
Jonah Peterson
A00591342
510 Tumble Dr., San Gabriel, TX 57981
(555) 759 - 2249
555-35-5984
20
2
CS2307
98.9
98.2
98.5
89.8
97.7
ART2308
50.9
55.2
60.5
65.8
45.7
HS2309
60.9
55.2
60.5
65.8
60.7
Student Grade Sheet, Texas State University
James Peterson
A00591342
510 Tumble Dr., San Gabriel, TX 57981
(555) 759 - 2249
555-35-5984
19
2
MT2306
98.9
98.2
98.5
89.8
97.7
CS2307
98.9
98.2
98.5
89.8
97.7
ART2308
50.9
55.2
60.5
65.8
45.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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
  #include <iostream>

#include <fstream>

#include <string>

#include<iomanip>

using namespace std;

int main()
{
    double average;
    double total;
    
    string name, social, address, telephone, course1, course2, course3, header, studentID, comment1, comment2;
    
    int n = 3;//, age, years; // n = number of students
    
    const int NUMBER_OF_nonNumerical = 9;
    
    const int NUMBER_OF_COURSES = 3;
    
    const int NUMBER_OF_SCORES = 5;
    const int THREE = 3;
    const int TWO =2;
    
    string student_nonNumeric1[n][NUMBER_OF_nonNumerical]; //store student string information
    
    int student_numeric1 [THREE][TWO]; // store student age and years at Texas State
    
    double courses_Numeric2 [n][NUMBER_OF_COURSES][NUMBER_OF_SCORES]; // store three students, three courses and five exams
    
    char letterGrade [THREE][NUMBER_OF_COURSES]; // store letter grade of three students and three courses
    
    
    //number of students
    
    cout << "Enter how many students do you want to see:" << endl;
    
    cin >> n;
    
    // Validate the input while loop
    
    while (n > 3 || n < 3 )
    {
        
        cout << "You should have exactly 3 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;
        
        return -1;
        
    }
    //fin >> courses_Numeric2[i][j][k]; //scores
    ofstream fout;
    
    fout.open("Output.txt");
    
    if (!fout) {
        
        cout << "could not open file" << endl;
        
        return -1;
        
    }
    
    
    for (int i = 0; i < n; i++)
    {
        getline(fin, student_nonNumeric1[i][0]); //headergetline(fin, student_nonNumeric1[i][3]);
        getline(fin, student_nonNumeric1[i][1]); //name
        getline(fin, student_nonNumeric1[i][2]);    // Id number
        getline(fin, student_nonNumeric1[i][3]);    //address
        //   fin.ignore();
        getline(fin, student_nonNumeric1[i][4]);    //phone number
        getline(fin, student_nonNumeric1[i][5]);    //social security
        
        fin >> student_numeric1[i][0];  // age
        fin >> student_numeric1[i][1];  //years at texas state
        
        fin.ignore();
        
        for (int j = 0, z = 6; j < NUMBER_OF_COURSES; j++, z++)
        {
            
            getline(fin, student_nonNumeric1[i][z]);
            total = 0;
            
            for (int k = 0; k < NUMBER_OF_SCORES; k++)
            {
                
                fin >> courses_Numeric2[i][j][k]; //scores
                
                total += courses_Numeric2[i][j][k];
                //  fout<<courses_Numeric2[i][j][k]; //scores
                //average =+ courses_Numeric2[i][j][k] /5;
                //average = (courses_Numeric2[0][0][0] + courses_Numeric2[0][0][1] + courses_Numeric2[0][0][2] + courses_Numeric2[0][0][3] + courses_Numeric2[0][0][4]) / 5;
            }
            average = total / 5;
            courses_Numeric2[i][j][NUMBER_OF_SCORES] = average;
            cout << average << endl;
            
            fin.ignore();
        }
        
        //getline(fin, student_nonNumeric1[i][3]);
        
    }
    
    
    
    //       fin >> courses_Numeric2[i][j][0]; //course 1
    //            fin >> courses_Numeric2[i][j][1]; //course 2
    //            fin >> courses_Numeric2[i][j][2];   //course 3
    //for(i =0; i < NUMBER_OF_SCORES; i++)
    
    
    
    
    fout << student_nonNumeric1[0][0] << endl;
    fout << student_nonNumeric1[0][1] << endl;
    fout << student_nonNumeric1[0][2] << endl;
    fout << student_nonNumeric1[0][3] << endl;
    fout << student_nonNumeric1[0][4] << endl;
    fout << student_nonNumeric1[0][5] << endl;
    
    fout << student_numeric1[0][0] << endl;
    fout << student_numeric1[0][1] << endl;
    
    fout << student_nonNumeric1[0][6] << endl;
    
    for (int k = 0; k < NUMBER_OF_SCORES; k++)
    {
        fout << courses_Numeric2[0][0][k] << endl;
    }
    fout << student_nonNumeric1[0][7] << endl;
    
    for (int k = 0; k < NUMBER_OF_SCORES; k++)
    {
        fout << courses_Numeric2[0][1][k]<< endl;
    }
    
    fout <<  student_nonNumeric1[0][8] << endl;
    
    for (int k = 0; k < NUMBER_OF_SCORES; k++)
    {
        fout << courses_Numeric2[0][2][k]<< endl;
    }
    
    //fout << courses_Numeric2[0][0][NUMBER_OF_SCORES];//print average
  
    
    
    fout << student_nonNumeric1[1][0] << endl;
    fout << student_nonNumeric1[1][1] << endl;
    fout << student_nonNumeric1[1][2] << endl;
    fout << student_nonNumeric1[1][3] << endl;
    fout << student_nonNumeric1[1][4] << endl;
    fout << student_nonNumeric1[1][5] << endl;
    
    fout << student_numeric1[1][0] << endl;
    fout << student_numeric1[1][1] << endl;
    
    fout << student_nonNumeric1[1][6] << endl;
    
    for (int k = 0; k < NUMBER_OF_SCORES; k++)
    {
        fout << courses_Numeric2[1][1][k] << endl;
    }
    fout << student_nonNumeric1[1][7] << endl;
    
    for (int k = 0; k < NUMBER_OF_SCORES; k++)
    {
        fout << courses_Numeric2[1][1][k]<< endl;
    }
    
    fout <<  student_nonNumeric1[1][8] << endl;
    
    for (int k = 0; k < NUMBER_OF_SCORES; k++)
    {
        fout << courses_Numeric2[1][2][k]<< endl;
    }
    
    
    
    fout << student_nonNumeric1[2][0] << endl;
    fout << student_nonNumeric1[2][1] << endl;
    fout << student_nonNumeric1[2][2] << endl;
    fout << student_nonNumeric1[2][3] << endl;
    fout << student_nonNumeric1[2][4] << endl;
    fout << student_nonNumeric1[2][5] << endl;
    
    fout << student_numeric1[2][0] << endl;
    fout << student_numeric1[2][1] << endl;
    
    fout << student_nonNumeric1[2][6] << endl;
    
    for (int k = 0; k < NUMBER_OF_SCORES; k++)
    {
        fout << courses_Numeric2[2][2][k] << endl;
    }
    fout << student_nonNumeric1[2][7] << endl;
    
    for (int k = 0; k < NUMBER_OF_SCORES; k++)
    {
        fout << courses_Numeric2[02][1][k]<< endl;
    }
    
    fout <<  student_nonNumeric1[2][8] << endl;
    
    for (int k = 0; k < NUMBER_OF_SCORES; k++)
    {
        fout << courses_Numeric2[2][2][k]<< endl;
    }
    return 0;
}
Last edited on
How would I store the weighted average of the 5 test grades for each course in the same array?
It depends on what makes sense in your opinion. According you code you have the choice of string student_nonNumeric, int student_numeric1, double courses_Numeric2, and char letterGrade.
Then, are the weights for all courses the same?

And How would I print it to the Output file right under the 5th test grade?
How about a 6th test grade with the average of the five before?
@MikeStgt

Different weights
Test 1 = .10
Test 2 = .15
Test 3 = .20
Test 4 = .25
Test 5 = .30

Yes, the weights for each courses are the same.

Should I makes [NUMBER_OF_SCORES] six instead of five?

Im unsure of how to calculate the weighted grade for each test in the loop.
> Im unsure of how to calculate the weighted grade for each test in the loop.

Something along these lines, perhaps:

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

// invariant: non-negative weights, sum of weights > 0
double weighted_average( const double weights[], const double scores[], std::size_t n )
{
    // see: https://en.wikipedia.org/wiki/Weighted_arithmetic_mean#Mathematical_definition

    // compute the sum of weights
    double sum_of_weights = 0 ;
    for( std::size_t i = 0 ; i < n ; ++i ) sum_of_weights += weights[i] ;

    // compute the total of each score multiplied by its weight
    double weighted_total = 0 ;
    for( std::size_t i = 0 ; i < n ; ++i ) weighted_total += scores[i] * weights[i] ;

    // divide the weighted_total by the sum_of_weights to get the weighted average
    return weighted_total / sum_of_weights ;
}

int main()
{
    const std::size_t NSCORES = 5 ;
    const std::size_t NSTUDENTS = 3 ;

    const std::string names[NSTUDENTS] { "Stroustrup", "Ritchie", "Koenig" } ;
    const double weights[NSCORES] { 0.10, 0.15, 0.20, 0.25, 0.30 } ;
    const double scores[NSTUDENTS][NSCORES]
    {
        { 98.9, 98.2, 98.5, 89.8, 97.7 },
        { 50.9, 55.2, 60.5, 65.8, 45.7 },
        { 60.9, 55.2, 60.5, 65.8, 45.7 }
    };

    // compute the weighted average scores
    double weighted_average_scores[NSTUDENTS] ;

    for( std::size_t i = 0 ; i < NSTUDENTS ; ++i )
        weighted_average_scores[i] = weighted_average( weights, scores[i], NSCORES ) ;

    // print results
    for( std::size_t i = 0 ; i < NSTUDENTS ; ++i )
    {
        // print name and test scores
        std::cout << std::setw(10) << names[i] << "  -  scores: " ;
        for( std::size_t j = 0 ; j < NSCORES ; ++j ) std::cout << scores[i][j] << ' ' ;

        // print the weighted average score
        std::cout << "   weighted average: " << weighted_average_scores[i] << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/88c48fba7676159d
Im unsure of how to calculate the weighted grade for each test in the loop.
Do you have a sly procedure to calculate it with a pointed pencil on a sheet of paper? A clear plan bulletproof in all circumstances, with work arounds for almost all ifs and buts? This is your program you have to translate to code. Still no idea? Slice it to smaller chunks, suitable to swallow without choking, until you have some code for every single step. Concatenate them to an algorithm, compile it, debug it, compare results with your manual procedure, consider the validity limits, test them, and then hope it will work also in the wild.

It's like a voyage to the moon, it begins with with fundamental steps taken in the stone age.
Your code is very difficult to read. Have you learned about structures? The code would be easier to write and read if you used them. If you postively must use arrays then I suggest creating const int variables that name the indices where things go. E.g.:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Non-numeric fields
const int HEADER=0, NAME=1, ID=2, ADDRESS=3, PHONE=4, SSN=5;

// Numeric fields
const int AGE=0, YEARS=1;
...
        getline(fin, student_nonNumeric1[i][HEADER]);
        getline(fin, student_nonNumeric1[i][NAME]);
        getline(fin, student_nonNumeric1[i][ID]);
        getline(fin, student_nonNumeric1[i][ADDRESS]);
        //   fin.ignore();
        getline(fin, student_nonNumeric1[i][PHONE]);
        getline(fin, student_nonNumeric1[i][SSN]);
        
        fin >> student_numeric1[i][AGE];
        fin >> student_numeric1[i][YEARS];


courses_Numeric2[i][j][NUMBER_OF_SCORES] = average; This is an access out of bounds. Since the last dimension is declared with NUMBER_OF_SCORES elements, that means the valid indices are 0 to NUMBER_OF_SCORES-1.

Lines 39-56: If 3 is the only valid value for n then why even prompt for it?
1
2
    const int THREE = 3;
    const int TWO =2;



This is ugly. Review where you use THREE and TWO. What do those numbers actually mean? You'll find that there are other numbers that reflect what you want.

1
2
3
    int n = 3;//, age, years; // n = number of students
...
    string student_nonNumeric1[n][NUMBER_OF_nonNumerical];

I'm almost certain that this isn't legal C++, although some compilers support it. The size of an array must be a compile-time constant. They only reason I'm not certain in this case is because n is guaranteed to be 3 at this point.

Ask yourself this: do you really NEED to store the data for each student? Why not read one student's info, write that info out, and then move to the next student. Done this way, you know have to store one student's info at a time and you can handle any number of students.

Please

don't

double

space

your

code

it

makes

it

very

hard

to

read.
Topic archived. No new replies allowed.