adding 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!
The lscores[100] array and the escores[100] arrays are not initialized. You define all your arrays in your functions, after the function has run they go out of scope. The result is, your data is lost after the function ends.
When you call calculatePointGrades you create three new arrays tscores, escores and lscores. The for loop just adds the non initialized escores[m] and the non initialized lscores[m]. The result is undefined. When the function is left by the program all your vectors go out of scope and all changes to the data is lost.

A better solution is to define them in main and pass them to your functions as arguments, together with the size of the array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void doStuff(int myArray[], const unsigned int size);

int main(){
const unsigned int size = 100;
int myArray[size]; // if you want this initilized to all zero write: myArray[size] = {0};

  doStuff(myArray, size);
  return 0;
}

void doStuff(int myArray[], const unsigned int size){
  // declaring the function as void doStuff(int *myArray, int size) would be the same
  for(int i = 0; i < size; i++){
    myArray[i] = i*2;
    std::cout << myArray[i] << endl;
  }
}
Topic archived. No new replies allowed.