/*Program to compute the mean and medians of the given grades and display the re
ID: 3630042 • Letter: #
Question
/*Program to compute the mean and medians of the given grades and
display the results*/
//header files
#include <iostream>//needed for I/O operations
using namespace std;
//functions prototypes
double funMean(int[], int);
double funMedian(int[], int);
//function main
int main()
{
//variables declaration
const int MAX=100;//constant integer for maximum size
int grade[MAX], size;
double mean,median;
//read the number of grades from the user
cout<<"Enter the number of grades you want to enter : ";
do
{
cin>>size;
if(size<0||size>MAX)
{
cout<<"You have entered invalid input... ";
cout<<"Enter valid input below 100 : ";
}
}while(size<0||size>MAX);//validating the size
//repeat until user enters valid input
//to read the grades from the user
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
//repeat 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
Dear Friend i fixed the error u r having trouble with and now u can enter a float point number without getting into an infinite loop PLEASE RATE #include //needed for I/O operations using namespace std; //functions prototypes double funMean(double[], int); double funMedian(double[], int); //function main int main() { //variables declaration const int MAX=100;//constant integer for maximum size double grade[MAX]; int size; double mean,median; //read the number of grades from the user coutsize; if(sizeMAX) { coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.