Im finding the middle position of an array, but if it is an even set of arrays i must find the average of the two middle numbers. i have the variable initialized as a double; but it still wont work. I've tried setprecision and the correct values are being found.
void median (int *pScores, int numScores)
{
insertionSort(pScores, numScores);
int mid = 0;
int midRight = 0;
int midLeft = 0;
double midEven;
//if array is odd
if (numScores % 2 != 0)
{
mid = numScores / 2;
cout << "Middle Position: " << *(pScores + mid) << endl;
}
//if array is even
if (numScores % 2 == 0)
{
midRight = (numScores/2);
midLeft = (numScores/2) - 1;
cout << *(pScores + midRight) << endl;
cout << *(pScores + midLeft) << endl;
midEven = ( *(pScores + midRight) + *(pScores + midLeft) ) / 2;
cout << "Median: "<< setprecision(2) << midEven << endl;
}
}
I've tried initializing double midEven as double midEven = 0; and double midEven = 0.0; and I'm still not getting a decimal point.