Trouble with double

Hi again guys. I just posted a few days ago since I am a noob and needed help with an array. Well now I got the array working correctly except for one thing, Its not taking my values of grades as a double, only as Integers. Please help. If you need an example of how the program is suppose to work here is one:

"Please enter the number of grades: " 6 (user enters 6)
(user enters 6 different grades)
85.2
92.3
78.0
51.5
91.6
87.0
"The median grade is 86.1"



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
 #include "stdafx.h"
#include <iostream>
#include <iomanip>


using namespace std;

void bubbleSort(double v[], int n);
double calcMedian(double v[], int n);

int main()
{
	const int SIZE = 20;
	int numOfGrades;
	double arrayGrades[SIZE] = { 0 };
	int i;


	
	//enters number of grades
	cout << "Please enter the number of grades. . .  ";
	cin >> numOfGrades;

	for (i = 0; i < numOfGrades; i++)

	{
		//grades for array
		cout << " Please enter grade " << (i + 1) << " : ";
		cin >> arrayGrades[i];
	}

	//call function to sort the array
	bubbleSort(arrayGrades, numOfGrades);
	cout << "\nThe sorted array is : " << endl;
	for (i = 0; i < numOfGrades; i++)
	{

		cout << arrayGrades[i] << '\t';
	}

	//call function to calculate median
	double median = calcMedian(arrayGrades, numOfGrades);
	cout << "\n\nThe median of all the elements is : " << setprecision(2) << median << endl;

	system("Pause");

	return 0;
}


//function for bubble sort
void bubbleSort(double v[], int n)

{

	for (int i = 0; i<n; i++)
	{
		for (int j = 0; j < n - i - 1; j++)
		{
			if (v[j] > v[j + 1])
			{
				double temp = v[j];
				v[j] = v[j + 1];
				v[j + 1] = temp;
			}
		}
	}
}

double calcMedian(int v[], int n)
{
	return 0.0;

}
	//function to calculate the median of the array
	double calcMedian(double v[], int n)
	{
		double median;

		//check if array size is even
		if (n % 2 == 0)
		{
			int middle = n / 2;
			median = v[middle];
		}
		//else if array size is odd
		{

			int middle1 = n / 2;
			int middle2 = (n / 2) - 1;
			//find average of the two middle elements
			median = (v[middle1] + v[middle2]) / 2;
		}
		return median;
	}

Firstly,your calcMedian function is incorrect.

If 'n' is even, then there are two middle numbers: n/2 and n/2+1 (yes, +1, not -1). And if n i s odd, the middle number is n/2+1.

Secondly, take a look at your parameter list for the calcMedian function. You are accepting the array as an int array. Change it to double.

Thirdly, whats up with line 72 of your code? Why are you returning 0.0? Was that a typo?
Last edited on
yeah line 72 was a typo sorry about that.
Alright so I tried what you said and this is what I have now. I hope I did it correctly:

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
 double calMedian(double v[], int n)
{
	return 0;
}

	//function to calculate the median of the array
	double calcMedian(double v[], int n)
	{
		double median;

		//check if array size is even
		if (n % 2 == 0)
		{
			int middle1 = n / 2;
			int middle2 = (n / 2) + 1;
			//find average of the two middle elements
			median = (v[middle1] + v[middle2]) / 2;

		}
		else
		//else if array size is odd
		{

			int middle = (n / 2)+1;
			median = v[middle];
		}
		return median;
	}


I still get my median as an integer rather than a double. Do I have to set n as a double aswell? because I get a conversion error with that. Also now my median is giving me weird values. Example would be the median of the example is suppose to be 86.1 but I get an 89 instead.
Last edited on
Also now my median is giving me weird values. Example would be the median of the example is suppose to be 86.1 but I get an 89 instead.


That was my mistake. Classic off-by-one error.
For n=odd, dont add 1, just do n/2.
for n=even, the two middle elements are n/2 and n/2-1.

Again, sorry for that mistake, it was my fault.

As for why you are getting an integer value, I just realized that when you are printing out the median in main, you have set the precision to 2. Set it to 3 (or 4 if you want 2 decimal places) and it should give you the decimal part.

Here is the output after making the above modifications:


Please enter the number of grades. . .  5
 Please enter grade 1 : 90.5
 Please enter grade 2 : 91.5
 Please enter grade 3 : 92.5
 Please enter grade 4 : 93.5
 Please enter grade 5 : 94.5

The sorted array is : 
90.5	91.5	92.5	93.5	94.5	

The median of all the elements is : 92.5
Last edited on
Its alright, no worries we all make mistakes. . . that why I am on this forum lol.
Also thank you so much. The program works perfectly now!!
Topic archived. No new replies allowed.