Trying to calculate median of sorted array (I'm close)

I am trying to figure out the median of a sorted array. So, what I know is that I need to find the middle element of the array and print it.

I feel like I can't just do arr[size / 2] because if it is odd, there will be a decimal and there will be no matching element in the array. So, I think I mainly need help finding the middle element without doing any mathematical operation on the size of the array.

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;
}

If 'size' is an integer, arr[size / 2] is the middle element, because size / 2 does integer division. By the way, it's not possible to use a fractional number to index an array. arr[1.5] will give a compiler error.
Right so how do I handle the decimal occurrences? If I can't have a decimal for the array element, what do I do to handle this?
You don't have to. size / 2 is an integer, always. For integer values of a and b, a / b = c is the integer farthest from zero such that abs(c * b) <= abs(a).
Topic archived. No new replies allowed.