Need help with finding Array Median and Mode of values?

Posted on 2006-11-06
Last Modified: 2012-06-21
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;
      return 0;
Question by:manz2
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 84

Expert Comment

ID: 17886500
the mode is the value that occurs the most often

Expert Comment

ID: 17886547
  The mode is the index that has the largest count. For your case, this is the number of the student, for instance that student 5 visited the most movies. You could determine this by keeping two numbers as the data are acquired, the largest number of visits so far recorded, and the student's number corresponding to it. However, to find the median value (the number of movie visits such that half the students saw fewer, and half more than that number) you will have to use an array, say VisitCount, because the median can only be found  after all the data are acquired.
   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.

Assisted Solution

RNMcLean earned 250 total points
ID: 17886652
   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.

Author Comment

ID: 17887373
Hi and thanks for the help. I had to make changes to the program to put functions in place and descriptions, so I won't forget. I'm getting an error when I compile and when I run the program I don't get the correct results.
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];


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]);
        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;
LVL 84

Accepted Solution

ozo earned 250 total points
ID: 17887432
void readem(int * & number, int &quant)

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question