are you learning C or C++? This is C, or nearly.
the function, calculate mean, is talking about myintarray, but there is no such variable. That variable is local to main, and if you want it in the function, you need to pass it in as a parameter.
there is nothing called bufferlen, perhaps you meant arraysize?
line 23 missing a ;
the format is
for(initialize; condition; action)
you have
for(initialize; condition action)
myint (27) does not exist.
30 you have a parameter to the function, which does not take one!
There were some other bugs as well. I think this one works and is close to what you wanted
and moves it toward c++:
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
|
#include <cstdio>
#include <cstdlib>
const int ARRAYSIZE{5}; //#defined items have no clean type, can be an issue in some code
float calculateMean(int *myIntArray) //Calculate the average of 5 integers
{
float result{}; //sets to zero. {42.5} sets to 42.5. this is preferred in c++ to initialize.
for(int i = 0; i < ARRAYSIZE; i++)
{
result += myIntArray[i]; //+= here, you added and discarded the result.
}
return result /(float)ARRAYSIZE; //cast to ensure floating point math.
}
int main()
{ //removed unused variable.
int myIntArray[5];
//float result;
for(int i = 0; i < ARRAYSIZE; i++) //c++ and C index from 0 not 1
{
cout << "Supply number " << i << endl; //printf("Supply number %d", i);
//scanf("%d", myInt);
cin >> myIntArray[i]; //removed useless variable and extra step
}
//result = calculateMean(myIntArray);
cout << "The average of the five numbers is: " << calculateMean(myIntArray) << endl;
//return 0; //not required
}
| |
a note on errors:
the compiler tells you what is wrong, and where. It can take a while to learn to read the messages, but it tries hard.
In function 'float calculateMean()':
11:20: error: 'myIntArray' was not declared in this scope
that is what it said, and what it means is you used a variable that does not exist at that location.
23:33: error: expected ';' before 'i'
Its hard to say that any plainer. Line 23 is missing a semi
and so on. read the message, look at the line it points to, try to fix it.
note that after the first error, subsequent errors CAN be nonsense, so if you can't make any sense of them, fix the top most error and recompile to see what is next and you may get a better message.