Help using arrays

The program reads in the student id and their 2 test grades and 7 homework grades. Then it calculates their final grade. IT is supposed to read up to 30 students.

I wrote the program which works but does not do exactly what is wanted. Im lost and have no idea how to start changing it. Can someone lead me in the right direction? My teacher had these comments when i submitted.

works but the output miss the last two students; the program can't process 30 students, but only 9; the array of students should not be named as "length"; getinput function does not use for-loop to work with test/hw arrays, but repeats the code; process function does not use the second parameter size, not use for-loop, but repeat code 7 times; most variables are global;

Text file it reads from:

123456789 77.2 88.3 22 28 35 45 33 35 40
234567890 97.5 90 25 30 38 48 34 35 50
345678901 82.4 77.5 22.5 27 35.5 44 35 33 48
456789012 65.5 67 20 25 28 40 27 25 35
567890123 79.5 82 25 30 32 47 30 33 46
678901234 90 86.5 25 30 40 46 34 35 50
789012345 77 82.2 25 30 36 46 33 32.5 47
890123456 67.7 72.5 23 28 35 44 30 30 44
901234567 76.5 83.4 25 29 38 49 35 35 49


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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#include <iostream>
#include <conio.h>
#include <fstream>
#include <iomanip>
using namespace std;

struct SStudent
{
	int id;
	double test1, test2, hw1, hw2, hw3, hw4, hw5, hw6, hw7;
};

int getInput(SStudent [], int);
void process(SStudent [], int);
void display(const SStudent [], int);

double testAvg1, testAvg2, testAvg3, testAvg4, testAvg5, testAvg6, testAvg7,
		   hwAvg1, hwAvg2, hwAvg3, hwAvg4, hwAvg5, hwAvg6, hwAvg7,
		   finalScore1, finalScore2, finalScore3, finalScore4, finalScore5, finalScore6, finalScore7;

char grade1, grade2, grade3, grade4, grade5, grade6, grade7;

int countA, countB, countC, countD, countE;

int main()
{
	SStudent length[9];

	getInput(length, 9);
	process(length, 9);
	display(length, 9);
	_getch();
}

int getInput(SStudent students[], int size)
{
	ifstream inFile;
	inFile.open("scores.txt");
	int count = 0;
	if(!inFile)
	{
		cout << "Can't open input file\n";
		_getch();
		exit(1);
	}

	for(int i = 0; i < size; ++i)
	{
		inFile >> students[i].id >> students[i].test1 >> students[i].test2 
			>> students[i].hw1 >> students[i].hw2 >> students[i].hw3 
			>> students[i].hw4 >> students[i].hw5 >> students[i].hw6
			>> students[i].hw7;

		count = count + 1;
	}
	return count;
}

void process(SStudent students[], int size)
{	
	testAvg1 = ((students[0].test1 + students[0].test2) / 200) * 100;
	testAvg2 = ((students[1].test1 + students[1].test2) / 200) * 100;
	testAvg3 = ((students[2].test1 + students[2].test2) / 200) * 100;
	testAvg4 = ((students[3].test1 + students[3].test2) / 200) * 100;
	testAvg5 = ((students[4].test1 + students[4].test2) / 200) * 100;
	testAvg6 = ((students[5].test1 + students[5].test2) / 200) * 100;
	testAvg7 = ((students[6].test1 + students[6].test2) / 200) * 100;

	hwAvg1 = ((students[0].hw1 + students[0].hw2 + students[0].hw3 + students[0].hw4 + students[0].hw5 + students[0].hw6 + students[0].hw7) / 265) * 100;
	hwAvg2 = ((students[1].hw1 + students[1].hw2 + students[1].hw3 + students[1].hw4 + students[1].hw5 + students[1].hw6 + students[1].hw7) / 265) * 100;
	hwAvg3 = ((students[2].hw1 + students[2].hw2 + students[2].hw3 + students[2].hw4 + students[2].hw5 + students[2].hw6 + students[2].hw7) / 265) * 100;
	hwAvg4 = ((students[3].hw1 + students[3].hw2 + students[3].hw3 + students[3].hw4 + students[3].hw5 + students[3].hw6 + students[3].hw7) / 265) * 100;
	hwAvg5 = ((students[4].hw1 + students[4].hw2 + students[4].hw3 + students[4].hw4 + students[4].hw5 + students[4].hw6 + students[4].hw7) / 265) * 100;
	hwAvg6 = ((students[5].hw1 + students[5].hw2 + students[5].hw3 + students[5].hw4 + students[5].hw5 + students[5].hw6 + students[5].hw7) / 265) * 100;
	hwAvg7 = ((students[6].hw1 + students[6].hw2 + students[6].hw3 + students[6].hw4 + students[6].hw5 + students[6].hw6 + students[6].hw7) / 265) * 100;

	finalScore1 = (testAvg1 * .60) + (hwAvg1 * .40);
	finalScore2 = (testAvg2 * .60) + (hwAvg2 * .40);
	finalScore3 = (testAvg3 * .60) + (hwAvg3 * .40);
	finalScore4 = (testAvg4 * .60) + (hwAvg4 * .40);
	finalScore5 = (testAvg5 * .60) + (hwAvg5 * .40);
	finalScore6 = (testAvg6 * .60) + (hwAvg6 * .40);
	finalScore7 = (testAvg7 * .60) + (hwAvg7 * .40);

	if (finalScore1 < 60)
	{
		grade1 = 'E';
		countE += 1;
	}
	else if (finalScore1 > 60 && finalScore1 <= 69.99)
	{
		grade1 = 'D';
		countD += 1;
	}
	else if (finalScore1 > 69.99 && finalScore1 <= 79.99)
	{
		grade1 = 'C';
		countC += 1;
	}
	else if (finalScore1 > 79.99 && finalScore1 <= 89.99)
	{
		grade1 = 'B';
		countB += 1;
	}
	else
	{
		grade1 = 'A';
		countA += 1;
	}

//grade2
	if (finalScore2 < 60)
	{
		grade2 = 'E';
		countE += 1;
	}
	else if (finalScore2 > 60 && finalScore2 <= 69.99)
	{
		grade2 = 'D';
		countD += 1;
	}
	else if (finalScore2 > 69.99 && finalScore2 <= 79.99)
	{
		grade2 = 'C';
		countC += 1;
	}
	else if (finalScore2 > 79.99 && finalScore2 <= 89.99)
	{
		grade2 = 'B';
		countB += 1;
	}
	else
	{
		grade2 = 'A';
		countA += 1;
	}

//grade3
	if (finalScore3 < 60)
	{
		grade3 = 'E';
		countE += 1;
	}
	else if (finalScore3 > 60 && finalScore3 <= 69.99)
	{
		grade3 = 'D';
		countD += 1;
	}
	else if (finalScore3 > 69.99 && finalScore3 <= 79.99)
	{
		grade3 = 'C';
		countC += 1;
	}
	else if (finalScore3 > 79.99 && finalScore3 <= 89.99)
	{
		grade3 = 'B';
		countB += 1;
	}
	else
	{
		grade3 = 'A';
		countA += 1;
	}

//grade 4
	if (finalScore4 < 60)
	{
		grade4 = 'E';
		countE += 1;
	}
	else if (finalScore4 > 60 && finalScore4 <= 69.99)
	{
		grade4 = 'D';
		countD += 1;
	}
	else if (finalScore4 > 69.99 && finalScore4 <= 79.99)
	{
		grade4 = 'C';
		countC += 1;
	}
	else if (finalScore4 > 79.99 && finalScore4 <= 89.99)
	{
		grade4 = 'B';
		countB += 1;
	}
	else
	{
		grade4 = 'A';
		countA += 1;
	}

//grade 5
	if (finalScore5 < 60)
	{
		grade5 = 'E';
		countE += 1;
	}
	else if (finalScore5 > 60 && finalScore5 <= 69.99)
	{
		grade5 = 'D';
		countD += 1;
	}
	else if (finalScore5 > 69.99 && finalScore5 <= 79.99)
	{
		grade5 = 'C';
		countC += 1;
	}
	else if (finalScore5 > 79.99 && finalScore5 <= 89.99)
	{
		grade5 = 'B';
		countB += 1;
	}
	else
	{
		grade5 = 'A';
		countA += 1;
	}

//grade 6
	if (finalScore6 < 60)
	{
		grade6 = 'E';
		countE += 1;
	}
	else if (finalScore6 > 60 && finalScore6 <= 69.99)
	{
		grade6 = 'D';
		countD += 1;
	}
	else if (finalScore6 > 69.99 && finalScore6 <= 79.99)
	{
		grade6 = 'C';
		countC += 1;
	}
	else if (finalScore6 > 79.99 && finalScore6 <= 89.99)
	{
		grade6 = 'B';
		countB += 1;
	}
	else
	{
		grade6 = 'A';
		countA += 1;
	}

//grade 7
	if (finalScore7 < 60)
	{
		grade7 = 'E';
		countE += 1;
	}
	else if (finalScore7 > 60 && finalScore7 <= 69.99)
	{
		grade7 = 'D';
		countD += 1;
	}
	else if (finalScore7 > 69.99 && finalScore7 <= 79.99)
	{
		grade7 = 'C';
		countC += 1;
	}
	else if (finalScore7 > 79.99 && finalScore7 <= 89.99)
	{
		grade7 = 'B';
		countB += 1;
	}
	else
	{
		grade7 = 'A';
		countA += 1;
	}
}

void display(const SStudent students[], int size)
{
	ofstream outFile;
	outFile.open("grades.txt");
	outFile << fixed << showpoint;  
    outFile << setprecision(1);

    outFile << "STUDENT ID\tHW AVE\t\tTEST AVE\tFINAL SCORE\tGRADE" << endl;
	outFile << students[0].id << "\t" << hwAvg1 << "\t\t" << testAvg1 << "\t\t" << finalScore1 << "\t\t" << grade1 << endl;
	outFile << students[1].id << "\t" << hwAvg2 << "\t\t" << testAvg2 << "\t\t" << finalScore2 << "\t\t" << grade2 << endl;
	outFile << students[2].id << "\t" << hwAvg3 << "\t\t" << testAvg3 << "\t\t" << finalScore3 << "\t\t" << grade3 << endl;
	outFile << students[3].id << "\t" << hwAvg4 << "\t\t" << testAvg4 << "\t\t" << finalScore4 << "\t\t" << grade4 << endl;
	outFile << students[4].id << "\t" << hwAvg5 << "\t\t" << testAvg5 << "\t\t" << finalScore5 << "\t\t" << grade5 << endl;
	outFile << students[5].id << "\t" << hwAvg6 << "\t\t" << testAvg6 << "\t\t" << finalScore6 << "\t\t" << grade6 << endl;
	outFile << students[6].id << "\t" << hwAvg7 << "\t\t" << testAvg7 << "\t\t" << finalScore7 << "\t\t" << grade7 << endl;

	outFile << "\nNumber of A's: " << countA;
	outFile << "\nNumber of B's: " << countB;
	outFile << "\nNumber of C's: " << countC;
	outFile << "\nNumber of D's: " << countD;
	outFile << "\nNumber of E's: " << countE;
	cout << "Output file created.";
}
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
#include <fstream>

const unsigned testScores = 2 ;
const unsigned hwScores = 7 ;

struct SStudent
{
	int id;
    double testScore[testScores] ;
    double hwScore[hwScores] ;
};

unsigned getInput(SStudent [], unsigned);
// ...

int main()
{
    const unsigned capacity = 30 ;
    SStudent students[capacity] ;

    unsigned nStudents = getInput(students, capacity) ;
    // ...
}


unsigned getInput(SStudent students[], unsigned maxStudents )
{
    std::ifstream in("scores.txt") ;

    unsigned count = 0 ;

    while ( count < maxStudents && (in >> students[count].id) )
    {
        for ( unsigned i=0; i<testScores; ++i )
            in >> students[count].testScore[i] ;

        for ( unsigned i=0; i<hwScores; ++i )
            in >> students[count].hwScore[i] ;

        ++count ;
    }

    return count ;
}
Topic archived. No new replies allowed.