For the following code: The mean function must loop through the values in the ar
ID: 3638692 • Letter: F
Question
For the following code:The mean function must loop through the values in the array, summing them together. The result of the function is the sum divided by the number of grades collected. The result must be returned from the mean function to the main function, where is it output in an appropriate manner (two digits after the decimal point).
#include <iostream>
using namespace std;
//functions prototypes
double funMean(int[], int);
double funMedian(int[], int);
//function main
int main()
{
//Variables Declaration
//Constant integer for maximum size
const int MAX=100;
int grade[MAX], size;
double mean,median;
//Request the number of grades from the user
cout<<"Enter the number of grades you want to enter : ";
//Read the number of grades from the user
do
{
cin>>size;
if(size<0||size>MAX)
{
cout<<"You have entered invalid input... ";
cout<<"Enter valid input below 100 : ";
}
//Validating the size
//Repeat until user enters valid input
//To read the grades from the user
}while(size<0||size>MAX);
for (int i=0; i<size; i++)
{
do
{
cout<<"Enter grade "<<i+1<<" : ";
cin>>grade[i];
if(grade[i]<0||grade[i]>100)
cout<<"Please enter a valid grade..."<<endl;
//Validating the user grade input
//Rrepeat until user enters valid input
}while(grade[i]<0||grade[i]>100);
}
//Display the reults
mean = funMean(grade, size);
cout<<" Mean of given grades is : "<<mean<<endl;
median = funMedian(grade,size);
cout<<" Median of given grades is : "<<median<<endl;
cout<<" The grades you entered are : "<<endl;
//Display the sorted grades
for(int k=0;k<size;k++)
{
cout<<grade[k]<<" ";
//To display 5 grades per line
if((k+1)%5==0)
{
cout<<endl;
}
}
cout<<endl;
//To halt the system
system("pause");
//end main
}
//Function to find mean
double funMean(int g[], int m)
{
double sum=0;
double avg;
for (int i=0; i<m;i++)
sum+=g[i];
avg=sum/m;
//return mean
return avg;
//end funMean
}
//Function to find Median
double funMedian(int g[], int m)
{
double sum=0;
double avg;
int temp;
//To sort the elements
for (int i = 0; i < m; i++)
for (int j = m-1; j > i; j--)
if (g[j-1] > g[j])
{
temp = g[j-1];
g[j-1]=g[j];
g[j]=temp;
}
//To find the median
if(m%2==0)
{
sum = g[m/2]+g[m/2-1];
avg = sum/2.0;
}
else
avg = g[m/2];
//return median
return avg;
//end median
}
Explanation / Answer
please rate - thanks
I made the same change for median since it's also a double
#include <iostream>
#include <iomanip>
using namespace std;
//functions prototypes
double funMean(int[], int);
double funMedian(int[], int);
//function main
int main()
{
//Variables Declaration
//Constant integer for maximum size
const int MAX=100;
int grade[MAX], size;
double mean,median;
//Request the number of grades from the user
cout<<"Enter the number of grades you want to enter : ";
//Read the number of grades from the user
do
{
cin>>size;
if(size<0||size>MAX)
{
cout<<"You have entered invalid input... ";
cout<<"Enter valid input below 100 : ";
}
//Validating the size
//Repeat until user enters valid input
//To read the grades from the user
}while(size<0||size>MAX);
for (int i=0; i<size; i++)
{
do
{
cout<<"Enter grade "<<i+1<<" : ";
cin>>grade[i];
if(grade[i]<0||grade[i]>100)
cout<<"Please enter a valid grade..."<<endl;
//Validating the user grade input
//Rrepeat until user enters valid input
}while(grade[i]<0||grade[i]>100);
}
//Display the reults
mean = funMean(grade, size);
cout<<" Mean of given grades is : "<<setprecision(2)<<fixed<<mean<<endl;
median = funMedian(grade,size);
cout<<" Median of given grades is : "<<setprecision(2)<<fixed<<median<<endl;
cout<<" The grades you entered are : "<<endl;
//Display the sorted grades
for(int k=0;k<size;k++)
{
cout<<grade[k]<<" ";
//To display 5 grades per line
if((k+1)%5==0)
{
cout<<endl;
}
}
cout<<endl;
//To halt the system
system("pause");
//end main
}
//Function to find mean
double funMean(int g[], int m)
{
double sum=0;
double avg;
for (int i=0; i<m;i++)
sum+=g[i];
avg=sum/m;
//return mean
return avg;
//end funMean
}
//Function to find Median
double funMedian(int g[], int m)
{
double sum=0;
double avg;
int temp;
//To sort the elements
for (int i = 0; i < m; i++)
for (int j = m-1; j > i; j--)
if (g[j-1] > g[j])
{
temp = g[j-1];
g[j-1]=g[j];
g[j]=temp;
}
//To find the median
if(m%2==0)
{
sum = g[m/2]+g[m/2-1];
avg = sum/2.0;
}
else
avg = g[m/2];
//return median
return avg;
//end median
}