My problem lies with the declaration of the function and calling of the function. I've tried so many combinations but can't figure it out. Any help is appreciated.
Thanks.
Put the code you need help with here.
#include <iostream>
usingnamespace std;
int main()
{
int tests[6]; // array declaration
int sum = 0;
float avg;
float CalcAvg(tests[6], 6, avg);
//input test scores
cout << " Enter " << 6 << " test scores: " << endl;
for (int i = 0; i < 6; i++)
{
cout << "Enter Test " << i + 1 << ": ";
cin >> tests[i];
}
cout << "first test score is: " << tests[0] << endl; //print first test
cout << "last test score is: " << tests[5] << endl; //print last score
//print all test scores
for (int i = 0; i<6; i++)
{
cout << tests[i] << " ";
}
// call funtion
CalcAvg(tests[i], 6, &avg);
return 0;
}
//function
void CalcAvg(int tests[], int numTests/*must be>0*/, float& avg)
{
int sum = 0;
for (int i = 0; i < numTests; i++)
{
sum = sum + tests[i];
}
avg = (float)sum / numTests;
}
First off, functions have to be declared before the main function is called, unless you declared function prototypes above your main.
So add:void CalcAvg(int tests[], int numTests/*must be>0*/, float& avg);
above your main.
Edited: Or before it is used; which you've done, but your missing the data types.
Second, you're calling the function with a single value from your array, whereas your array was expecting an array at argument #1.
Also, your third argument should be avg, not the address of avg.CalcAvg(tests[i], 6, avg);
Line 10: Move your forward declaration outside of main. The signature of the forward declaration must exactly match the implementation at line 42 (argument names are optional).
void CalcAvg(int[], int, float &);
Line 35: Lose the subscript on the array and the & on avg.
The sum variable inside your function and from your main function are completely different; they are in different scopes, which makes them different objects/variables.
Either pass the sum to the function or return the sum from the function?
For a simple idea:
Change the void in your functions prototype and its header to int.
then insert return sum; at the end of your function.
Then in your main you can see if the sum was accumulated by inserting the function into a output prompt.
cout<<"The total is: "<<CalcAvg(tests, 6, avg)<<endl;
If this is your intentions then there is no need for the sum in your main function.
Else you can also do this:
1 2
sum = CalcAvg(tests, 6, avg);
cout<<"The total is: "<<sum<<endl;
the function call does not execute when I run the program.
The function call does indeed execute. You can easily step into the function with a debugger to show that it executes.
The problem is that you calculate avg inside the function, but don't do anything with it. either inside the function or after you return from the function. You might want to add at line 38:
1 2
cout << "The average is: " << avg << endl;
Note: The console window will close immediate after displaying that, so you might not see it. See this thread regarding the console window closing: http://www.cplusplus.com/forum/beginner/1988/