### Need Help with Funciton to find Median- Using Array

Below is the code for the function to find the Median from an array. However, it is not working when there is an even number of numbers in the array. It works with an odd number in the array. Where is the error in the code? Thanks

// Median.cpp : Defines the entry point for the console application.
//

#include <iostream>
#include <algorithm>
using namespace std;
//sort() function to sort the array.
void sort(int* arrayPtr, int size)
{
int i, j;
//Start the nested for loop to sort.
for (i = 0;i<size;i++)
for (j = i + 1;j<size;j++)
//Comparison to sort.
if (*(arrayPtr + j)<*(arrayPtr + i))
{
int temp = *(arrayPtr + j);
*(arrayPtr + j) = *(arrayPtr + i);
*(arrayPtr + i) = temp;
}
}
//printMedian() function to print the sorted array.
void printMedian(int array[], int size)
{
for (int i = 0; i < size; i++)
cout << array[i] << ' ';
}
//findMedian() function to find the median.
int findMedian(int array[], int size)
{
int median, mid;
//Find the mid element.
mid = size / 2;
//Array size is even.
if (size % 2 == 0)
{
//Calculate the median.
median =(array[mid] + array[mid + 1]) / 2;
}
//Array size is odd.
else
{
//Calculate the median.
median = (array[mid]);
}
return median;
}
//main() function
int main()
{
//Initialize an array
int array[] = { 20, 2, 8, 0, 0, 50, 1, 2, 40, 20, 100, 10 };
//make the arrayPtr to an array.
int* arrayPtr = array;
//Initialize the size of the array
int size = 12;
//Declare the variable to store the median.
int median;
//Call the function sort() to sort the array.
sort(arrayPtr, size);

//Call the function printMedian() to print.
printMedian(array, size);
//Call the function findMedian() to find the median.
median = findMedian(array, size);
//Print the result.
cout << endl << "Median is:" << median << endl;
system("pause");
return 0;
}
Try looking at what the value mid is set to in each case (Print it out to the console). You are really close, but the value of mid in your even case is off.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869`` ``````#include #include using namespace std; //sort() function to sort the array. void sort(int* arrayPtr, int size) { int i, j; //Start the nested for loop to sort. for (i = 0;i
//Calculate the median.
median =(array[mid] + array[mid + 1]) / 2;

Do I not need to have the +1 added to the mid in the second part of the array? I tried typing it into the console but can't quite figure it out.
You are on the right track. There is a problem with that line of code.

Determine which two numbers are being added currently and which 2 numbers should be added and you will know how to adjust your values. My goal here is to help you learn how to debug your own code so when you get more difficult code, you are able to find and resolve the problems yourself.
Hmm I can't quite seem to figure it out using the console. Is there a problem using [mid + 1] As in this is adding +1 to the actual value and not the next number in the array?
This is the output I get when I run your current code

0 0 1 2 2 8 10 20 20 40 50 100
Median is:15

Which 2 values are added to create the erroneous value of 15?
median =(array[mid] + array[mid + 1]) / 2;
Hint: they add up to 30

Now which two values do you need to add to get the expected median of 9?
median =(array[???] + array[???]) / 2;
Hint: one of the values in you current code is correct and one is incorrect
So the two values needed are of course 8 and 10. So I'm guessing (array[mid]) = 8 and array[mid+1] /= 10. It instead equals 15. I'm still not sure how to implement the correct value to 10 instead of 15.
Ahhh. The second needs to be array[mid -1]. I figured it out. Thank you for taking the time to help me debug instead of simply telling me the answer!
Try printing out the values like this
 ``1234567891011121314151617181920212223`` ``````int findMedian(int array[], int size) { int median, mid; //Find the mid element. mid = size / 2; //COMMENT:print out the value of mid //Array size is even. if (size % 2 == 0) { //Calculate the median. std::cout << "array[mid] = " << array[mid] << "\n"; std::cout << "array[mid+1] = " << array[mid+1] << "\n"; median =(array[mid] + array[mid + 1]) / 2; //COMMENT: Which values are being added here. (not what you expect) } //Array size is odd. else { //Calculate the median. median = (array[mid]); } return median; }``````

The added print statements will show you what the values are for each. The value you are guessing for mid is incorrect.

Another thing to remember is that array index start from 0, not 1. That may have some effect on your calculations.
Great job. Glad I could help.
Topic archived. No new replies allowed.