We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

# Need help with finding Array Median and Mode of values?

on
Medium Priority
1,280 Views
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."

#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;
}
Comment
Watch Question

## View Solutions Only

CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
the mode is the value that occurs the most often

Commented:
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.
Commented:
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.

Not the solution you were looking for? Getting a personalized solution is easy.

Commented:
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];

}

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

// 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;
}
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015
Commented:
void readem(int * & number, int &quant)

median seems to be assuming that the Number of movies for student # values are entered monotonicly
##### Thanks for using Experts Exchange.

• View three pieces of content (articles, solutions, posts, and videos)
• Ask the experts questions (counted toward content limit)
• Customize your dashboard and profile