declaration/calling of function with an array

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.

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
  Put the code you need help with here.
#include <iostream> 
using namespace 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;
}
you have to delcare the function before main so it knows it exists.

so:

void CalcAvg(int tests[], int numTests/*must be>0*/, float& avg);

main...
closed account (j3Rz8vqX)
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);

Try to resolve those for now.
Edited.
Last edited on
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.
 
CalcAvg(tests, 6, avg); 


I did what AbstractionAnon suggested. However, the function call does not execute when I run the program. Does this look right?

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
#include <iostream> 
using namespace std;
void CalcAvg(int[], int, float &);

int main()
{
	int tests[6]; // array declaration 
	int sum = 0;
	float 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, 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;
}
closed account (j3Rz8vqX)
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;


Have a good day.
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/

Last edited on
Topic archived. No new replies allowed.