Using C++, create a program to input a list of positive numbers, find the mean (average) of the numbers, and output the result. Use a subprogram to input the numbers, a function to find the mean, and a subprogram to output the result.
C++ code:
#include <iostream>
using namespace std;
float Mean(int m[],int n)//function to find mean
{
int s=0;//initializing s which is the sum as 0
for(int i=0;i<n;i++)//looping to find the sum
s+=m[i];//finding sum
return(s/n);//dividing sum by n to obtain mean and returning
it
}
int main()//main function
{
cout<<"Enter number of positive numbers: ";//asking user to
enter count of numbers
int n;//variable n to store count of numbers
cin>>n;//accpting n
int num[n];//initializing an array to store numbers
for(int i=0;i<n;++i){//loop for accepting values
cin>>num[i];//reading values to num array
}
cout<<"Mean: "<<Mean(num,n)<<endl;//printing
mean
return 0;
}
Screenshot of code:
Input and output:
Get Answers For Free
Most questions answered within 1 hours.