I am trying to write a program that finds the mean and median for a set of numbers, based on how many movies a number a students see a month. I have no included the mean function, as it is working great. I notice with the median function though, it is giving the median based on the order of input the user gives and isn't sorting from smallest to biggest. I am not sure how to do that, can someone please help me?
#include <iostream>
#include <iomanip>
usingnamespace std;
// Function prototypes
double calculateMean(int *, int);
double calculateMedian(int *, int);
int main()
{
int *nums; // To dynamically allocate an array
int num_students; // To hold the number of students
char repeat = ' '; // Does the user want to go again?
do
{
// Prompt the user to enter in how many students were surveyed.
cout << "Enter in how many students were surveyed: ";
cin >> num_students;
// Determine input validation.
while (num_students < 0)
{
cout << "Invalid number of students!\n";
cout << "Enter in how many students were surveyed: ";
cin >> num_students;
}
// Dynamically allocate an array large enough to hold
// that many numbers of students.
nums = newint[num_students];
// Get the number of movies for each student;
for (int count = 0; count < num_students; count++)
{
cout << "Number of movies say by Person #" << (count + 1) << ": ";
cin >> nums[count];
// Determine input validation.
while (nums[count] < 0)
{
cout << "Invalid number. Please enter in a positive number: ";
cout << "\nNumber of movies say by Person #" << (count + 1) << ": ";
cin >> nums[count];
}
}
// Set output formatting.
cout << fixed << showpoint << setprecision(1);
// Display the mean.
cout << "\nThe mean is: ";
cout << calculateMean(nums, num_students) << endl;
// Display the median.
cout << "\nThe median is: ";
cout << calculateMedian(nums, num_students) << endl;
// Free dynamically allocated memory.
delete[] nums;
nums = 0; // Make nums point to null.
// Ask the user if he/she wants to go again.
cout << "Do you want to go again? (Enter Y if yes and N if no) ";
cin >> repeat;
} while (repeat == 'Y' || repeat == 'y');
cout << "Program ending.\n";
return 0;
}
// Definition of calculateMedian. The nums parameter is a pointer.
// The num_students parameter holds the number of students. The
// function calculates and returns the median back to the main function
double calculateMedian(int *nums, int num_students)
{
double median = 0.0;
cout << fixed << showpoint << setprecision(1);
// int answer = num_students % 2;
if (num_students % 2 == 0)
{
median = (nums[num_students / 2] + nums[(num_students / 2) + 1]) / 2.0;
// median = even_median;
}
else
median = nums[num_students / 2];
return median;
}
I notice with the median function though, it is giving the median based on the order of input the user gives and isn't sorting from smallest to biggest
Okay, so I have sorted the number and now my median works great for when there is an odd number. It's a lot easier to find i guess since it's the actual number from the array. When there is an even number it is looking through I get 1 or .5 more than it should be. When I had it looking for the median of 1,2,3,4 it gave me 3 instead of 2.5. How can I fix this problem?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
double calculateMedian(int *nums, int num_students)
{
int middle;
double median;
middle = num_students / 2.0;
if (num_students % 2 == 0 )
{
median = nums[middle];
}
else
{
median = (nums[middle] + nums[middle]) / 2.0;
}
return median;
}
In some schools, given an even number of values, the median is defined to be the mean of the two central values. Which in many ways is a bit rubbish, as one of the key features of a median that makes it actually useful is that it's definitely represented in the set. This is statisticians missing the point, in my opinion, but it is what it is.