int Mean(int numbers[],int sizeOfArray)
{
float sum = 0;
float average = 0;
for(int i = 0; i < sizeOfArray; i ++)
{
sum += numbers[i];
}
average = sum/sizeOfArray;
return average;
}
int main()
{
int array[10]; // can not define size of array by variable, It must be a const value or enum may work.
cout << "Enter integers('x' to stop)\n";
size_t size = 0;
while ((size < 10) && (cin >> array[size]))
{
size++;
}
cout << "average: " << Mean(array, size); // just array name will work no need to say array[]
getch();
return 1;
}
#include <iostream>
usingnamespace std;
//prototyypes
float Mean(int arr[], int size); //make sure you declare prototypes correctly
int main() //all functions must have a return type
{
const size_t max_size = 100;
int arr[max_size]; //you CAN define an array with a const variable
//you can also define an array with a non-const variable but then you have to dynamically allocate memory
cout << "Enter integers('x' to stop)\n";
size_t size = 0;
while ((size < 100) && (cin >> arr[size]))
{
size++;
}
cout << "average: " << Mean(arr, size);
cin.ignore(); //to pause the program
cin.get();
return 0;
}
float Mean(int numbers[], int sizeOfArray) //mean must return float
{
float sum = 0;
for (int i = 0; i < sizeOfArray; i++)
{
sum += (numbers[i]);
}
return (float)sum / sizeOfArray; //must cast sum to float, otherwise an integer division would occur
}