Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Need help with finding Array Median and Mode of values?

Posted on 2006-11-06
5
Medium Priority
?
1,261 Views
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;
      system("pause");
      return 0;
}
0
Comment
Question by:manz2
  • 2
  • 2
5 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 17886500
the mode is the value that occurs the most often
0
 
LVL 2

Expert Comment

by:RNMcLean
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.
0
 
LVL 2

Assisted Solution

by:RNMcLean
RNMcLean earned 1000 total points
ID: 17886652
  Agh!
   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.
0
 

Author Comment

by:manz2
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];
    }

    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;
}
0
 
LVL 85

Accepted Solution

by:
ozo earned 1000 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
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Introduction to Processes

916 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