how to add arrays?

I have a homework assignment where I have to write a program that asks the user how many students to process, then reads in the scores for two items (an exam score and a lab average) for that many students, then calculates and displays a table of the students' grade information.

The part of the program that I'm having trouble with is where I have a function that receives two arrays of scores and then adds them together (calculatePointGrades). I think maybe the problem is that I can't figure out how to get the arrays from the functions getExamScores and getLabScores to the function calculatePointGrades. The values dont carry over like i want them too.

Anyways, Here is my code:
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
#include <iostream>
using namespace std;
int getStudentCount(int& number);
void getExamScores(int& escore, int& number);
void getLabScores(double& lscore, int& number);
void calculatePointGrades(int number);

void main()
{
	/*Code to read in all data*/
	int number=0, escore=0;
	double lscore=0;
	cout << "Please enter the number of students.";
	getStudentCount(number);
	getExamScores(escore, number);
	getLabScores(lscore, number);
	/*Code to process the data*/
	calculatePointGrades(number);
}
/*Code to read in all data*/
int getStudentCount(int& number)
{
cin >> number;
return number;
}

void getExamScores(int& escore, int& number)
{

	int i;
	double totale=0;
	double escores[100];

	for(i = 0; i < number; i++)
		{
		cout << "Enter Student # " << i+1 << " Exam Scores:"; 
		cin >> escores[i];
		totale += escores[i]; //total = total + escores[i];
		}
}

void getLabScores(double& lscore, int& number)
{

	int j;
	double totall=0;
	double lscores[100];
	
	for(j = 0; j < number; j++)
		{
		cout << "Enter Student # " << j+1 << " Lab Scores:"; 
		cin >> lscores[j];
		totall += lscores[j]; //total = total + lscores[i];
		}
}
	/*Code to process the data*/
void calculatePointGrades(int number)
{
	
	int tscores[100];
	int escores[100];
	int lscores[100];
	int m=0;
	
	for(m=0; m<number; m++)
	{
		tscores[m] = escores[m] + lscores[m];
		cout << tscores[m] << endl;
	}
}


I added line 68 to test the code, and always get a random value like -1717986920. Any ideas on what I'm doing wrong?

I havent started on the table, but I'm pretty confident that I can figure that part out on my own.


Thanks!
I added line 68 to test the code, and always get a random value like -1717986920. Any ideas on what I'm doing wrong?


Because you haven't declared anything in any of the arrays you are just getting garbage. Check out this link for more info on arrays: http://www.cplusplus.com/doc/tutorial/arrays/
Last edited on
I dont understand...

The user enters the values that the function manipulates.
Doesnt the code below declare a value into the two arrays?

for(i = 0; i < number; i++)
{
cout << "Enter Student # " << i+1 << " Exam Scores:";
cin >> escores[i];
}
for(j = 0; j < number; j++)
{
cout << "Enter Student # " << j+1 << " Lab Scores:";
cin >> lscores[j];
}

Do i have to declare the arrays in the function heading?
void calculatePointGrades(double escores[], double lscores[], int number)
{}
Or do i have to declare the arrays outside the function, in the main?
double escores[100], lscores[100];
void calculatePointGrades(int number)
{}

Or what?
Last edited on
In your Calculation function you reinitialize the arrays back to 100 cells of 0s. Take them out there and in the other functions and just put them in the top, so they are constants.

The other guy i right too, I just thought it be best to make as little change to your program as possible.

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
#include <iostream>
using namespace std;
int getStudentCount(int& number);
void getExamScores(int& escore, int& number);
void getLabScores(double& lscore, int& number);
void calculatePointGrades(int number);

int escores[100];
int lscores[100];

int main()
{


/*Code to read in all data*/
int number=0, escore=0;
double lscore=0;
cout << "Please enter the number of students.";
getStudentCount(number);
getExamScores(escore, number);
getLabScores(lscore, number);
/*Code to process the data*/
calculatePointGrades(number);
system ("PAUSE");
return 0;
}
/*Code to read in all data*/
int getStudentCount(int& number)
{
cin >> number;
return number;
}

void getExamScores(int& escore, int& number)
{

int i;
double totale=0;

for(i = 0; i < number; i++)
{
cout << "Enter Student # " << i+1 << " Exam Scores:"; 
cin >> escores[i];
totale += escores[i]; //total = total + escores[i];
}
}

void getLabScores(double& lscore, int& number)
{

int j;
double totall=0;

for(j = 0; j < number; j++)
{
cout << "Enter Student # " << j+1 << " Lab Scores:"; 
cin >> lscores[j];
totall += lscores[j]; //total = total + lscores[i];
}
}
/*Code to process the data*/
void calculatePointGrades(int number)
{
system("cls");
int tscores[100];
int m=0;

for(m=0; m<number; m++)
{
tscores[m] = (escores[m] + lscores[m]) / 2;
cout <<"Grade average for Student #"<< m+1<<": "<< tscores[m] << endl;
}
}

That makes sense. My code works now! Thanks for all the help!

Last edited on
Topic archived. No new replies allowed.