Write a program that can be used to gather statistical data about the number of
ID: 3620039 • Letter: W
Question
Write a program that can be used to gather statistical data about the number of movies college students see in a month. The program should ask the user how many students were surveyed and dynamically allocate an array of that size. The program should then allow the user to enter the number of movies each student has seen. The program should then calculate the average, median, and mode of the values entered#include <iostream>
using namespace std;
double average(int[],int);
void print(int[],int);
void sort (int[],int);
double median (int [], int );
int mode(int [], int );
int main()
{int num,i;
cout<<"how many students are there? ";
cin>>num;
int movies[num];
for(i=0;i<num;i++)
{do
{cout<<"How many moviesdid student "<<i+1<<" see? ";
cin>>movies[i];
if(movies[i]<0)
cout<<"You can't see <0 movies! ";
}while(movies[i]<0);
}
print(movies,num);
cout<<"each student saw an average of"<<average(movies,num)<<" movies ";
sort(movies,num);
cout<<"The median is "<<median(movies,num)<<endl;
cout<<"The mode is "<<mode(movies,num)<<endl;
system("pause");
return 0;
}
int mode(int a[], int num)
{int b[num],c,most,emost;
int bcount=0,i=0,j,done,count,tot[num];
for(i=0;i<num;i++)
{c=a[i];
done=0;
for(j=0;j<bcount;j++)
if(c==b[j])
done=1; //letter already done
if(done==0)
{b[bcount]=c;
count=1;
for(j=i+1;j<num;j++)
if(a[j]==c)
count++;
tot[bcount]=count;
bcount++;
}
}
most=tot[0];
emost=0;
for(i=1;i<bcount;i++)
if(tot[i]>most)
{ most=tot[i];
emost=i;
}
return b[emost];
}
void sort (int a[], int num)
{int i,j,t;
for(i=0;i<num-1;i++)
for(j=i+1;j<num;j++)
if(a[i]>a[j])
{t=a[i];
a[i]=a[j];
a[j]=t;
}
}
double median (int a[], int num)
{double m;
if(num%2==0)
return((float)(a[num/2-1]+a[num/2])/2.);
else
return a[num/2];
}
void print(int a[],int num)
{int i;
cout<<"Your values: ";
for(i=0;i<num;i++)
cout<<a[i]<<" ";
cout<<endl;
}
double average(int a[],int num)
{int i,sum=0;
for(i=0;i<num;i++)
sum+=a[i];
return (double)sum/num;
}
Explanation / Answer
please rate - thanks all but the mode appeares to work. this includes a different version of mode -- much more completeRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.