In: Computer Science
write a function mean_value that accepts up to four integers and returns their possibly floating point mean_value. the function should be able to work with any number of inputs from one to four. ( it just returns the input if there is only one)
#include<iostream>
using namespace std;
float mean_value(int nums[],int size)
{
int sum=0;
float mean;
if(size==1)
{
return nums[0];
}
else
{
for(int i=0;i<size;i++)
{
sum=sum+nums[i];
}
mean=(float)sum/size;
return mean;
}
}
int main()
{
int *nums;
int size;
cout<<"Enter number of integers : ";
cin>>size;
if(size<1 || size>4)
{
cout<<"\nInvalid number of integers either enter 1 or 2 or 3
or 4 not other number. \n";
}
else
{
cout<<endl;
nums=new int[size];
for(int i=0;i<size;i++)
{
cout<<"Enter Number "<<i+1<<" : ";
cin>>nums[i];
}
float mean=mean_value(nums,size);
cout<<"\nMean :
"<<mean<<endl<<endl;
}
system("pause");
return 0;
}