Help me find a median, median grade Lab

OK so I got this program almost done my only problem now is that it keeps rounding when it finds median, any help would be 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
//Zachary Marbooti
#include <iostream>
using namespace std;

int numGrd();

void getGrd(double grd[], int sze);

void sortArray(double grd[], int sze);

double median(double grd[], int sze);

void displayGrd(double grd[], int sze); //ignore this, I placed this function to check on my bubble sort, and it is working.

int main()
{
	double grd[20], sze; //Set Array
	
	sze = numGrd();

	getGrd(grd, sze); // Gets the Grades

	sortArray(grd, sze); // Heres my bubble sort

	median(grd, sze); //The call to median (this is where my problem is I am sure its my math!!!)
	
    return 0;


}


int numGrd()
{
	int sze;
	cout << " How many grades? " << endl;
	cin >> sze;
	return sze;
}

void getGrd(double grd[], int sze)
{
	int i;
	cout << " Enter Grades " << endl;
	for (i = 0; i < sze; i++)
	{
		cin >> grd[i];
	}
}

void sortArray(double grd[], int sze)
{
	for (int i = 0; i < sze; i++)
	{
		for (int j = 0; j < sze - i - 1; j++)
		{
			if (grd[j] > grd[j + 1])
			{
				int t = grd[j];
				grd[j] = grd[j + 1];
				grd[j + 1] = t;
			}
		}
	}
}


double median(double grd[], int sze)
{
	double med;
	if (sze % 2 == 0) // If Even
	{
		int mid1 = sze / 2;
		int mid2 = (sze / 2) - 1;
		med = (grd[mid1] + grd[mid2]) / 2;
		
	}
	else
	{                     // Else odd
		int mid = sze / 2;
		med = grd[mid];
	}
	cout << "The median grade is " << med << '\n' << endl;  //Display median here 
	return med;
	
}

void displayGrd(double grd[], int sze)
{
	for (int i = 0; i < sze; i++)
		cout << grd[i] << ' ' << endl;
}
Last edited on
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/general/192612/
Topic archived. No new replies allowed.