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"
#include "stdafx.h"
#include <iostream>
#include <iomanip>
usingnamespace std;
void bubbleSort(double v[], int n);
double calcMedian(double v[], int n);
int main()
{
constint 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;
}
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.
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