Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that can be used to gather statistical dataabout the number of m

ID: 3618498 • Letter: W

Question

Write a program that can be used to gather statistical dataabout the number of movies college students see in a month. Theprogram should perform the following steps:

a)      Ask the user how many studentswere surveyed. An array of integers with this many elements shouldbe dynamically allocated.

b)      Allow the user to enter thenumber of movies each student saw into the array.

c)       Calculate and display theaverage, median, and mode of the values entered.

Input validation: Do not accept negative numbers forinput.

Explanation / Answer

#include<iostream.h>

int main()
{
int numStudents = 0;
int sumTotal=0;
cout<<"No of students surveyed? ";
cin>>numStudents;
int* numOfMovies;
cout<<" ";

for(int i=0;i<numStudents;i++)
{
  cout<<" How many movies student"<<(i+1)<< " watched in a month? ";
  int tmpinput = 0;
        cin>>tmpinput;
  if(tmpinput <0)
  {
   i--;
   cout<< "Negative numbers not allowed!!!please reenter the value. ";
   continue;
  }     
  numOfMovies[i] = tmpinput;
  sumTotal += tmpinput;  
}
    cout<<" ";
cout<<"Average no of movies a student waching ina month : "<< (int)(sumTotal/numStudents);
    cout<<" ";
if(numStudents%2 == 1)
{
  cout<<"Median of the no of movies a student waching in a month : "<< (int)numOfMovies[(numStudents/2)+1];
}
else
{
  int tempsum = (int)numOfMovies[(numStudents/2)] +(int)numOfMovies[(numStudents/2) +1];
        cout<<"Median ofthe no of movies a student waching in a month : "<<tempsum/2;
}
    cout<<" ";
int mode = numOfMovies[0];
    int prevSearchCount = 0;

for(intj=0;j<numStudents;j++)
{
  int searchCount = 1;
  for(int k = 0;k<numStudents;k++)
  {
   if(j!=k)
   {
    if( numOfMovies[j] == numOfMovies[k])
    {
     searchCount++;
    }
   }
  }
  if(searchCount > prevSearchCount)
  {
   mode = numOfMovies[j];
   prevSearchCount = searchCount;
  }
}
    if(prevSearchCount > 1)
  cout<<"Mode of the no of movies astudent waching in a month :"<<mode;
else
  cout<<"Can't find the mode";

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote