The functions are getInputN()
, calculateMean()
& displayData()
.
So to be really clear, these are the requirements.
getInputN
function: Should accept the number of the value, N as an integer as argument and ask the user to enter the value of N number. Then, return the sum of the value as a double.calculateMean
function: Should accept the number of the value, N and sum of the value as arguments. Then return the mean as a double.displayData
function: Should accept the mean as argument. Then, display them in the appropriate message on screen. No return value required for this function.
If I run the code, it will display Average = inf
p/s: I'm really sorry for the confusing question at first. I'm really new to this website and this is my very first question. It took me some times to figure out things to ask properly in this platform. I hope you guys understand and once again, sorry for the inconvenience. Thank you for the helps too :)
Here is my code:
#include <iostream>
using namespace std;
int getInputN(int n);
float calculateMean (int n, float sum);
float displayData(double mean);
int i,n;
float sum = 0.0, num[50];
double mean;
int main()
{
getInputN(n);
calculateMean (n, sum);
displayData(mean);
return 0;
}
int getInputN(int n)
{
int i;
float num[50];
//User enter the number of value
cout << "Enter the numbers of data: ";
cin >> n;
//if user input more than 50 numbers
while (n > 50 || n <= 0)
{
cout << "Invalid! Enter the number in range of (1 to 50)." << endl;
cout << "Enter the number of data: ";
cin >> n;
}
for(i = 0; i < n; ++i)
{
cout << i + 1 << ". Enter number: ";
cin >> num[i];
sum += num[i];
}
return n;
}
//function to calculate the mean
float calculateMean (int n, float sum)
{
mean = sum/n;
return mean;
}
//function to display the mean
float displayData (double mean)
{
cout << "Average = " << mean;
}