Hi Experts,

I'm having trouble with two things:

This is what the book is asking of me "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 the values entered.

I have created the program but I am stuck with showing the results. As of right now it shows the average, and I think I might be able to do the median by continuing

looking at some book samples. But the part that get's me the most and have difficult finding is "and mode the values entered."

Thanks in advance for your help!

#include <iostream>

#include <iomanip>

using namespace std;

int main ()

{

double *movies, total = 0, average;

int numStudents;

int count;

cout << "How many students were surveyed? \n";

cin >> numStudents;

movies = new double[numStudents];

cout << "Enter the number of movies for each student. \n";

for (count =0; count < numStudents; count ++)

{

cout << "Student " << (count + 1) << ": ";

cin >> movies[count];

}

for (count = 0; count < numStudents; count++)

{

total += movies[count];

}

average = total / numStudents;

cout << setprecision(2);

cout << fixed << showpoint;

cout << "Average: " << average << endl;

delete [] movies;

system("pause");

return 0;

}

I'm having trouble with two things:

This is what the book is asking of me "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 the values entered.

I have created the program but I am stuck with showing the results. As of right now it shows the average, and I think I might be able to do the median by continuing

looking at some book samples. But the part that get's me the most and have difficult finding is "and mode the values entered."

Thanks in advance for your help!

#include <iostream>

#include <iomanip>

using namespace std;

int main ()

{

double *movies, total = 0, average;

int numStudents;

int count;

cout << "How many students were surveyed? \n";

cin >> numStudents;

movies = new double[numStudents];

cout << "Enter the number of movies for each student. \n";

for (count =0; count < numStudents; count ++)

{

cout << "Student " << (count + 1) << ": ";

cin >> movies[count];

}

for (count = 0; count < numStudents; count++)

{

total += movies[count];

}

average = total / numStudents;

cout << setprecision(2);

cout << fixed << showpoint;

cout << "Average: " << average << endl;

delete [] movies;

system("pause");

return 0;

}

With such an array, the mode is easily found. The median can most easily be found by sorting the array by some simple method, whereupon if N is odd the median value is VisitCount(n/2) and if even, the tradition is midway between the two middle values: (visitcount(n/2) + visitcount(n/2 + 1))/2

Sorting the array is more than is strictly necessary. There is a method of finding the median (due to C.A.R. Hoare) that only partially sorts the array.

I just realised that I misdescribed the mode for all to see, and I can't edit out my mistake... I described the maximum! As ozo says, the mode is the number of visits that more students made than any other visit count's collection of students. So, if student 1 visited 3 times, student 2 once, student 3 4,and student 4 once, the mode would be 1, because more students (two) visited that number of times more often than any other number of times, even though other students visited more often than once.

And now the skull sludge settles.

Also, could you show me a code sample using my program of how I would go about doing the MODE VALUE. Thank you so much, as I have been going nuts these past few hours trying to do this

and understand it :)

#include <iostream>

#include <iomanip>

using namespace std;

void readem(int * & number, int quant)

// OUT OUT

// This function reads the data and returns it in an array along with

// the quantity of elements read.

{

// Find out how many numbers there will be and allocate space

cout << "\nHow many students took the survey? ";

cin >> quant;

// Allocate appropriate amount of space

number = new int[quant];

// Read numbers and put them into list

for(int i=0; i<quant; i++)

{

cout << "\nNumber of movies for student # " << i+1 << "? ";

cin >> number[i];

}

return;

}

int sum(const int array[], int num)

// IN IN

// This function calculates the sum of the array with 'num' elements.

{

int total=0;

for(int i=0; i < num; i++)

total += array[i];

return total;

}

float median(const int array[], int num)

// IN IN

// This function calculates the average, or geometric mean of the array.

// The array contains 'num' elements. NOTE: no error checking is done.

// If the array has an odd number of elements the median is the element in

// the middle position; otherwise it is the average of the two elements

// closest to the middle.

{

float middle;

if ( (num % 2) == 1)

middle = float(array[num/2]);

else

middle = (array[num/2] + array[num/2 - 1]) / 2.0;

return middle;

}

int main()

{

int * number; //Pointer to array to hold values

int num = 0; //Quantity of values read

int total;

// Read data and store in array

readem(number, num);

// Call functions to get mean and median and display results.

total = sum(number, num);

cout << "\nSum = " << total << endl;

cout << "\nAverage = " << float(total)/num << endl;

cout << "\nMedian = " << median(number, num) << endl;

// Return space to free store

delete[] number;

return 0;

}

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.

median seems to be assuming that the Number of movies for student # values are entered monotonicly