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.
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[], constunsignedint size);
int main(){
constunsignedint 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[], constunsignedint 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;
}
}