Passing Arrays to functions

Hi all. I am just learning about arrays, and am having trouble passing them to functions. To be honest, I am quite lost. Below is the code I am currently working on.

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
#include <iostream>

using namespace std;


int main ()

{
	double numbers[10];
	double averagearray( double numbers[], int count);
	double average;
	int count;

	cout<< "Welcome! This program will allow you to enter 10 numbers, then find the average of those numbers."<< endl;

	average = averagearray(numbers, count);

	cout<< "The average of the numbers that were entered is: " << average << endl;

	return (0);

}

/******************************************************************/
double averagearray (double tobesummed [], int count)

{
	double sum;
	double avg;
	double numbers[10];
	int count;

	for (count = 0; count < 10; count++)
	{
		cout << "Enter # " << (count + 1) << endl;
		
		cin>> numbers[count];
	}

	avg = sum/10;
	
	for (count = 0; count < 10; count++);
	{
		cout<< "You entered.........." << (count + 1) << endl;
	}

	return avg;

}




I only receive 1 error, and I am not quite sure how to fix it. I have messed around and tried to fix it but to no avail:

error C2082: redefinition of formal parameter 'count'

Last edited on
closed account (3hM2Nwbp)
The problem is that you have a parameter with the name 'count' in your 'averagearray' method (line 25), and then inside of your 'averagearray' method, you declare another variable with the name 'count' (line 31). The compiler doesn't know which identifier is which (there can be only one!), so it's complaining. To resolve this, change either of the names.

Good luck!
If I change it up, it tells me it isn't declared, which is why I declared it on there.
closed account (3hM2Nwbp)
1
2
3
4
5
6
7
8
9
10
11
double averagearray (double numbers[], int count)

{
        // You'll need to make a small change if you're compiling C instead of C++
	for (int i = 0; i < count; i++)
	{
		cout << "Enter # " << (i + 1) << endl;
		cin >> numbers[i];
	}

}


Also, you're not initializing the 'count' variable in your main method. If I might suggest making the count a constant.

1
2
const int COUNT = 10;
double numbers[COUNT];
Last edited on
I am very appreciative of your help. It seems I may also be calling the function the wrong way. Do you have any input there?
Last edited on
closed account (3hM2Nwbp)
You'll want to move the averagearray method declaration above the main method (not inside it).
Should I change anything in the parenthesis on the function call segment? That is where I keep getting the error with count.
closed account (3hM2Nwbp)
1
2
3
4
5
6
7
8
9
10
11
12
/// Method Declaration
double averagearray( double numbers[], int count);

int main(void)
{
    const int COUNT = 10;
    double numbers[COUNT];
    double average = averagearray(numbers, COUNT);
    return 0;
}

Last edited on
Thank you so much for all of your help. I sure hope you get paid well for your knowledge. Here is the code I am using now and it works fine. I adjusted it a bit to get it to look how I wanted on the output screens. All in all I owe you one man!

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
#include <iostream>

using namespace std;

double averagearray (double numbers[], int count);

int main (void)

{
	const int count = 10;
	double numbers[count];
	double average;
	

	cout<< "Welcome! This program will allow you to enter 10 numbers, then find the average of those numbers."<< endl;

	average = averagearray(numbers, count);

	cout<< "The average of the numbers that were entered is: " << average << endl;

	return (0);

}

/******************************************************************/
double averagearray (double numbers[], int count)

{
	double sum;
	double avg;

	sum=0;
	avg=0;

	for (count = 0; count < 10; count++)
	{
		cout << "Enter # " << (count + 1) << endl;
		
		cin>> numbers[count];

		sum = sum + numbers[count];
	}

	avg = sum/10;

	return avg;

}
Topic archived. No new replies allowed.