current code:

void showMedian(int *array, int size)
{
	double middle;
	double average;

	middle = size / 2;

	if ( middle % 2)
	{
		average = (*(array[middle]) + *(array[middle + 1])) / 2;
		cout << "The median is: " << average << endl;
	}
	else
		cout << "The median is: " << *(array[middle + .5]) << endl;
}

I know that % only works with int, and not doubles but what im trying to do is find the middle number of a set of numbers, if the set of numbers is even, then find the average of the two numbers.

im getting errors for average =

Where are you? I want to come rip the * key off your keyboard!!!!;)

The value middle must be an int - has to be in order to be an index into the array.
That will fix the % operator problem.

You don't want to test middle % 2 - that does not tell you if the array was odd or even sized. Test size % 2.

Get rid of the asterisks in the average computation line. You'll see that they give compiler errors when you've fixed the stuff above. And divide by 2.0 so you get a result of type double, which is what you're storing to.

I love you too baby =D what part of SD you live? i got family in soiux falls or something like that.

void showMedian(int *array, int size)
{
	double middle;
	double average;

	middle = size / 2.0;

	if (size % 2)
	{
		average = (array[middle] + array[middle + 1]) / 2.0;
		cout << "The median is: " << average << endl;
	}
	else
		cout << "The median is: " << *(array[middle + .5]) << endl;
}

getting the error "subscript is not of integral type" for the average =

now if im reading that right that means that there is no number. or is it not about to be divided?

sorry if it sounds restarded. im just trying to get an understanding of what the error means, and not just get it fixed and move on.

See my last post - "middle" must be an int type. You cannot use a double as an array index.

I'm in Rapid City - opposite side of the state from Sioux Falls. The better side side of the state.

Signing off for the night.

Val

I'm in Rapid City - opposite side of the state from Sioux Falls. The better side side of the state.

The Wall Drug side of the state? I hope that's not what makes it better!! :icon_lol:

The Wall Drug side of the state? I hope that's not what makes it better!! :icon_lol:

That's one aspect. With all the signs around the world telling you how far it is to Wall Drug, I always know how far from home I am.:D

But it's much, much more that makes this area great.

To find median, first of all, we have to sort the sequence of numbers, after that we can do average for odd and even size.
Hope it helps.

You can use qsort to sort the sequence.

//comparing function
int compare(const void *a,const void *b){
        return (*(int*)a-*(int*)b);
}
//median fuction after sorting sequence
void getMedian(int arr[],int size)
{
    int middle = size/2;
    float average;
    if (size%2==0) average = static_cast<float>(arr[middle-1]+arr[middle])/2;
    else average = static_cast<float>(arr[middle]);

    cout << "Median of array is: " << average << endl;
}
int main()
{
     int *arr;
     int size;
     cout << "input size: " << endl; cin >> size;
     arr = new int[size];
     for (int i=0;i<size;i++) cin >> arr[i];
     qsort(arr,size,sizeof(int),compare); // using qsort for sorting or u can see bubble sort, selection sort...
     getMedian(arr,size);
     return 0; 
}

Edited 3 Years Ago by Nick Evan: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.