Solved

Need help with finding Array Median and Mode of values?

Posted on 2006-11-06
5
1,246 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 84

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 250 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 84

Accepted Solution

by:
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
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Exchange and Third Party application. 8 68
how to open Waze.com/livemap from address saved in DB? 26 178
Specific format 21 184
Arduino EDI - Programming Language - Voice Recorder 4 70
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 …
This is an explanation of a simple data model to help parse a JSON feed
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

863 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now