I offered to make my girlfriend a grade calculator for some c++ practice. I've got this far to find out that it outputs all my module scores as 0 and the overall score to be a ridiculously small number... Any assistance would be appreciated to calm my nerves before my exam on Monday. I wouldn't be surprised if there are a number of stupid mistakes, I've been awake for 36 hours :(
Thanks,
Jake
The overall function takes a pointer as argument. yes it looks like an array but arr1 is actually a pointer. Just so that you understand what is going on I will rewrite the overall function using pointer syntax. This shows exactly what is going on. I don't mean you should change your code, just look at it.
1 2 3 4 5 6 7 8 9
double overall(double* arr1)
{
double score = 0;
for (int i = 0; i < 5; i++)
score = score + *(arr1 + i);
return score;
}
The problem is when you call the function and pass &scores[5] as argument. scores[5] is the sixth element in the scores array, but the array only has 5 elements so this is out of bounds. So the function will actually access scores[5...9] which are all out of bounds it will probably just read what happens to be stored after the array in memory.
What you should do instead is to pass a pointer to the first element in the array: overall(&scores[0]); or overall(scores); will also work because the array will automatically decay to a pointer to the first element when it's passed to the function.
Another problem with your code is the two for loops in the main function because scores[i] is out of bounds when i=5.