158,174 views
39 votes
39 votes
For this assignment, you will use pointers to 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. Be sure to include comments throughout your code where appropriate.

Complete the C++ code

User Donotalo
by
3.1k points

1 Answer

10 votes
10 votes

Answer:

Following are the program to the given question:

#include <iostream>//header file

#include <bits/stdc++.h>//header file

using namespace std;

int main()//main method

{

int x,i,sum=0;//defining integer variable

double average, median;//defining double variable

cout<<"How many students were surveyed? ";//print message

cin>>x;//input value

int *a = new int[x]; //dynamically created array

for(i = 0;i<x;i++)//defining for loop to input value in array

{

cout<<"Enter the number of movies seen by student number "<<i+1<<": ";//print message

cin>>a[i];//input value

}

for (int i = 0; i < x; i++)//defining loop for add value

sum += a[i]; //use sum to add array value

average = (double)sum/x;//defining average variable to calculate average

cout<<"Average is "<<average<<endl;//print average value with message

sort(a, a+x); //use sort method to sort array to find the median

if (x % 2 != 0)//use if to check the even value

median = (double)a[x/2];//calculating the median value

else//esle block

median = (double)(a[(x-1)/2] + a[x/2])/2.0;//calculating median value

cout<<"Median is "<<median<<endl;//print median value with message

int m_count=1 ,mode= a[0], c_count = 0;//defining integer variable

for( i=1;i<x;i++)//defining for loop arrenge value

{

if(a[i]==a[i-1])//use if to check array value

{

c_count++;//increment c_count variable value

}

else//else block

{

if(c_count>m_count)//use if to check c_count greater than m_count

{

m_count = c_count;//holding value in m_count

mode = a[i-1];//holding mode value

}

c_count = 1;//holding value integer value in c_count

}

}

cout<<"Mode is "<<mode;//print mode value with message

return 0;

}

Output:

Please find the attachment file.

Step-by-step explanation:

  • Including header file.
  • Defining the main method.
  • Declaring integer and double type variables.
  • Declaring integer array.
  • Use the loop to the input value.
  • After input, the value uses double variable "average, the median" that uses the for loop and conditional statement to check the value and hold in its variable.
  • In the next step, another integer variable "m_count, mode, and c_count" is defined that uses the loop and conditional statement to check the median and mode value and hold its value into their variable.
For this assignment, you will use pointers to write a program that can be used to-example-1
User FrankTheTank
by
2.7k points