Decimal cutting off when calculating median of even numbers

When I try to find the median of an even amount of numbers, the decimal is being cut off of the answer. I'm sure I'm just overlooking something simple. Any help would be much appreciated.

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
101
102
103
104
105
106
107
108
109
110
111
 // Unit 9 modularized.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include<string>
#include <iomanip>
using namespace std;

// Declare functions.
int askStudents();
int* askMovies(int getStudents);
int* sortMovies(int* askMovies);
int getMedian(int getStudents, int* setMovies);


//Declare variables.
int getStudents;
int* setMovies;

int main()
{
	getStudents = askStudents();
	setMovies = askMovies(getStudents);
	sortMovies(setMovies);
	getMedian(getStudents, setMovies);

    return 0;
}

int askStudents()
{
	cout << "How many students were surveyed? ";
	cin >> getStudents;

	while (getStudents < 0)
	{
		cout << "Please enter a non-negative number: ";
		cin >> getStudents;
	}
	return getStudents;
}

int* askMovies(int getStudents)
{
	// Creating array "setMovies" with size of getStudents using pointer.
	int *setMovies;
	setMovies = new int[getStudents];

	// Storing the amount of movies each student watched into setMovies array.
	for (int i = 0; i < getStudents; i++)
	{
		cout << "How many movies did each student watch: ";
		cin >> setMovies[i];

		while (setMovies[i] < 0)
		{
			cout << "Please enter a non-negative number: ";
			cin >> setMovies[i];
		}
	}

	// Printint setMovies for test
	for (int i = 0; i < getStudents; i++)
		cout << setMovies[i];
	cout << "\n";

	return setMovies;
}

int* sortMovies(int* setMovies)
{
	for (int i = 0; i < getStudents; i++)
	{
		for (int j = i + 1; j <getStudents; j++)
		{
			if (setMovies[i]>setMovies[j])
			{
				int temp = setMovies[i];
				setMovies[i] = setMovies[j];
				setMovies[j] = temp;
			}
		}
	}

	for (int i = 0; i < getStudents; i++)
		cout << setMovies[i];
	cout << "\n";

	return setMovies;
}

int getMedian(int getStudents, int* setMovies)
{
	float median;
	if (getStudents % 2 == 0)
	{
		median = (setMovies[(getStudents / 2) - 1] + setMovies[(getStudents / 2)]) / 2;
		cout << "The median is: " << median << endl;

	}
	else
	{
		median = setMovies[(getStudents / 2)];
		cout << "The median is: " << median << endl;
	}

	return median;
}

closed account (E0p9LyTq)
Returning a float as an int in your getMedian() function.
So how do I return it as a float instead? Change getMedian to float?
closed account (E0p9LyTq)
Easy peasy:

float getMedian(int getStudents, int* setMovies)

You are still going to have possible decimal truncation problems with your integer math in the function, though.
Anyone have a real world example where you would want to do this? Because there is a real world example of why you shouldn't...

If you're trying to be mathematically rigid, returning a float is already blowing it because you're reporting precision that isn't in the original data.

Yes, watching half a movie is actually a thing.. for a variety of reasons. But the data is rounded to integers.

So why report the median with false precision? That's flawed math.
1
2
3
4
float getMedian(int getStudents, int* setMovies)
{
   return 0.5 * ( setMovies[getStudents/2]+setMovies[(getStudents-1)/2] );
}


Should work for odd or even numbers (and relies on the appropriate integer division).

A mean is routinely accepted as a float/double; why not then the median?
Rounding to int would introduce bias, which is one thing statistics tries to avoid.



Last edited on
Topic archived. No new replies allowed.