How do you add all the values of an array together?

In, void process(SStudent students[], int size) I want it to take all the scores in the test array for a student and add them all together and put them into the testAve for the student. I can't figure out how to write it.

This is the 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

#include <iostream>
#include <conio.h>
#include <fstream>
#include <iomanip>
using namespace std;

struct SStudent
{
	int id;
	double test[2], hw[7], hwAve, testAve, finalScore, grade;
};

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

int main()
{
	int totalStudents, i;
	SStudent student[30];

	totalStudents = getInput(student, 30);
	process(student, totalStudents);
	display(student, totalStudents);

	_getch();
}

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

	for(i = 0; i < size; ++i)
	{
		inFile >> students[i].id;

		for(int j = 0; j < 2; ++j)
			inFile >> students[i].test[j];

		for (int k = 0; k < 7; ++k)
			inFile >> students[i].hw[k];

		if(!inFile) break;
	}

	return i;
}
//WHERE IM HAVING TROUBLE//////////////////////////////////////////////////
void process(SStudent students[], int size)
{
	for(int i = 0; i < size; ++i)
	{
		for(int j = 0; j < 2; ++j)
			students[i].testAve =+ students[i].test[j];

		for(int k = 0; k < 7; ++k)
			students[i].hwAve =+ students[i].hw[k];

////////////////////////////////////////////////////////////////////////////
		
		//students[i].hwAve = students[i].hwAve / 265;
		//students[i].testAve = students[i].testAve / 200;
		//students[i].finalScore = (students[i].hwAve * 0.40) +(students[i].testAve * 0.60) * 100;

		if (students[i].finalScore < 60)
			students[i].grade = 'E';

		else if (students[i].finalScore > 60 && students[i].grade <= 69)
			students[i].grade = 'D';

		else if (students[i].finalScore > 69 && students[i].grade <= 79)
			students[i].grade = 'C';

		else if (students[i].finalScore > 79 && students[i].grade <= 89)
			students[i].grade = 'B';

		else
			students[i].grade = 'A';
	}
}

void display(const SStudent students[], int size)
{
    std::cerr << "In display, size is " << size << ".\n" ;

    ofstream outFile("grades.txt") ;
    outFile << fixed << showpoint << setprecision(1);
    outFile << "STUDENT ID\tHW AVE\t\tTEST AVE\tFINAL SCORE\tGRADE" << endl;
    for(int i = 0; i < size; ++i)
        outFile << students[i].id << "\t" << students[i].hwAve << "\t\t" << students[i].testAve << "\t\t" << students[i].finalScore << "\t\t" << students[i].grade << endl;

    if ( outFile )
        cout << "Output file created.\n";
    else
        cout << "Output file not created.\n" ;
}
60
61
62
63
64
65
66
	for(int i = 0; i < size; ++i)
	{
		for(int j = 0; j < 2; ++j)
			students[i].testAve =+ students[i].test[j];

		for(int k = 0; k < 7; ++k)
			students[i].hwAve =+ students[i].hw[k];

+=

The average would be the total divided by the number of individual parts. So the test average would be the sum of the test scores divided by the number of tests. The homework average would be the sum of the homework scores divided by the number of homework assignments.

As for the final score, I'm going to assume that there's no curve. In that case, it should be 40% of the total homework score added to 60% of the total test score. The sum should be divided by the total possible points of all the assignments together, then multiplied by 100.
You need to set testAve and hwAve to zero for each student before adding any values to them.

Also, the the correct operator on lines 63 and 66 is +=, not =+.

Also, note in struct SStudent, grade should be of type char.
Last edited on
Topic archived. No new replies allowed.