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.
#include <iostream>
usingnamespace 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'
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.
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.
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!
#include <iostream>
usingnamespace std;
double averagearray (double numbers[], int count);
int main (void)
{
constint 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;
}