Help writing a code to find the median

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
  #include <iostream>
#include <iomanip>
using namespace 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 = new int[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


http://www.cplusplus.com/reference/algorithm/sort/

http://www.cplusplus.com/reference/iterator/begin/
http://www.cplusplus.com/reference/iterator/end/
You have to sort the array before you calculate median.
http://www.cplusplus.com/articles/NhA0RXSz/
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;  
}
Last edited on
Let's say, you have four numbers: 1, 2, 3, 4.

 
middle = num_students / 2.0; // pointless 2.0, as middle is an int 

This will make middle = 2, due to integer truncation.

median = nums[middle];
This will access the element at index 2, which is 3.

 
median = (nums[middle] + nums[middle]) / 2.0;

Redundant?
1
2
3
4
5
6
7
median = (nums[middle] + nums[middle]) / 2.0;

// which is
median = 2 * nums[middle] / 2.0;

// which is
median = nums[middle];

which is the same as your even elements case.


1,2,3,4 it gave me 3 instead of 2.5

Why did you expect the answer to be 2.5 instead of 3?
How did you go about working this out?
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.
Topic archived. No new replies allowed.