I asked this question and was given this answer (below) but I have another quest
ID: 3649218 • Letter: I
Question
I asked this question and was given this answer (below) but I have another question. I compiled this program, but if there is more than one number as mode, it only returns one number (i.e. 13, 13, 13, 14,14,14, 15, 16, 17) it will only return 13. I don't know how to fix this.Movie Statistic
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.
Formulas Needed:
Median is the middle number in a range of ordered values. If the number of values is even, the median is the average of the middle two values.
examples:
(1,2,3,4,5) Median = 3
(1,2,3,4,5,6) Median = 3.5
Mode is a number that is repeated most often. There can be more than one mode value.
examples:
(13, 13, 13, 13, 14, 14, 15, 16, 17) Mode = 13
(13, 13, 13, 14, 14, 14, 15, 16, 17) Mode = 13 and 14
#include <iostream>
using namespace std;
void getinfo(int*, int);
void sort(int *, int);
double average(int*, int);
double median(int*, int);
int get_mode(int*, int);
int main()
{ int*array;
int num;
int mode,i,range;
float avg;
cout<<"enter number of students? ";
cin>>num;
while(num<=0)
{cout<<"Invalid Entry ";
cout<<"enter number of students? ";
cin>>num;
}
array=new int[num];
getinfo(array, num);
cout<<" The array is: ";
for(i=0;i<num;i++)
cout<<"student "<<i+1<<" saw "<<*(array+i)<<" movies. ";
sort(array, num);
cout<<"the median is "<<median(array, num) << endl;
cout<<"the average is "<<average(array, num) << endl;
mode=get_mode(array, num);
if(mode<0)
cout<<"There is no mode. ";
else
cout<<"The mode is " << mode << endl;
delete [] array;
system("pause");
return 0;
}
void getinfo(int *a, int n)
{int i;
for(i=0;i<n;i++)
{cout<<"How many movies did student "<<(i+1)<< " see? ";
cin >> *(a+i);
while(*(a+i)<0)
{cout<<"Invalid entry, must be >= 0 ";
cout<<"How many movies did student "<<(i+1)<< " see? ";
cin>>*(a+i);
}
}
}
double average(int *a, int num)
{ int tot=0,i;
double avg;
for(i=0;i<num; i++)
tot+=*(a+i);
avg=(double)tot/num;
return avg;
}
void sort(int *a, int n)
{ int i,j,t;
for(i=0;i<n-1;i++)
for(j=i;j<n;j++)
if(*(a+i)>*(a+j))
{t=*(a+i);
*(a+i)=*(a+j);
*(a+j)=t;
}
}
double median(int* a, int n)
{ int m1,m2;
if (n%2==0)
{m1=n/2;
m2=(n/2)-1;
return((*(a+m1)+*(a+m2))/2.);
}
else
return *(a+(n/2));
}
int get_mode(int* a, int n)
{
int* count,most,index,i,j;
count= new int[n];
for (i= 0;i< n;i++)
count[i] = 0;
for(i=0;i<n;i++)
{for(j=0;j<n;j++)
{if(*(a+j)==*(a +i))
(*(count+i))++;
}
}
most=*count;
index=0;
for (i=1;i<n;i++)
{if (*(count+i) >most)
{most=*(count+i);
index=i;
}
}
if (most == 1)
return -1;
else
return *(a+index);
}
Explanation / Answer
please rate - thanks
how is this?
#include <iostream>
using namespace std;
void getinfo(int*, int);
void sort(int *, int);
double average(int*, int);
double median(int*, int);
int get_mode(int*, int,int*);
int main()
{ int* array;
int* mode;
int num,last=-1;
int i,range,count;
float avg;
cout<<"enter number of students? ";
cin>>num;
while(num<=0)
{cout<<"Invalid Entry ";
cout<<"enter number of students? ";
cin>>num;
}
array=new int[num];
mode=new int[num];
getinfo(array, num);
cout<<" The array is: ";
for(i=0;i<num;i++)
cout<<"student "<<i+1<<" saw "<<*(array+i)<<" movies. ";
sort(array, num);
cout<<"the median is "<<median(array, num) << endl;
cout<<"the average is "<<average(array, num) << endl;
count=get_mode(array, num,mode);
if(count<0)
cout<<"There is no mode. ";
else
{cout<<"The mode(s): ";
for(int i=0;i<num;i++)
if(*(mode+i)==count&&*(array+i)!=last)
{cout<<*(array+i)<<" ";
last=*(array+i);
}
}
cout<<endl;
delete [] array;
delete []mode;
system("pause");
return 0;
}
void getinfo(int *a, int n)
{int i;
for(i=0;i<n;i++)
{cout<<"How many movies did student "<<(i+1)<< " see? ";
cin >> *(a+i);
while(*(a+i)<0)
{cout<<"Invalid entry, must be >= 0 ";
cout<<"How many movies did student "<<(i+1)<< " see? ";
cin>>*(a+i);
}
}
}
double average(int *a, int num)
{ int tot=0,i;
double avg;
for(i=0;i<num; i++)
tot+=*(a+i);
avg=(double)tot/num;
return avg;
}
void sort(int *a, int n)
{ int i,j,t;
for(i=0;i<n-1;i++)
for(j=i;j<n;j++)
if(*(a+i)>*(a+j))
{t=*(a+i);
*(a+i)=*(a+j);
*(a+j)=t;
}
}
double median(int* a, int n)
{ int m1,m2;
if (n%2==0)
{m1=n/2;
m2=(n/2)-1;
return((*(a+m1)+*(a+m2))/2.);
}
else
return *(a+(n/2));
}
int get_mode(int* a, int n,int* count)
{
int most,index,i,j;
for (i= 0;i< n;i++)
count[i] = 0;
for(i=0;i<n;i++)
{for(j=0;j<n;j++)
{if(*(a+j)==*(a +i))
(*(count+i))++;
}
}
most=*count;
index=0;
for (i=1;i<n;i++)
{if (*(count+i) >most)
{most=*(count+i);
index=i;
}
}
if (most == 1)
return -1;
else
return most;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.