Hello everyone, I'm having a hard time figuring out what is wrong with my codes.
I have a set of arrays and I have to sort them in order to find the median.
I made the function to find the median and I tried recalling the function but it keeps saying that median is undeclared. With the error keep popping I'm not even sure if my 'getMedian' function is correct so any help and advice would be appreciated.

thank you.

#include "stdafx.h"
#include <iostream>
#include <iomanip>

using namespace std;

void getMedian(int data[], int numberOfElements, float median)
{
		int middle = 0;
		middle = numberOfElements / 2;
		median = (float)(data[middle] + data[middle + 1])/ 2;
								
}
int _tmain(int argc, _TCHAR* argv[])
{

	const int arraySize = 10;
	int data[ arraySize ] = { 34, 56, 4, 10, 77, 51, 93, 30, 5, 52 };
	int numberOfElements;
	int insert;
	
	numberOfElements = sizeof(data)/sizeof(int);

	cout << "Unsorted arrays:\n";

	for ( int i = 0; i < arraySize; i++ )
		cout << setw( 4 ) << data[ i ];

	for ( int next = 1; next < arraySize; next++ )
	{
		insert = data[ next ];

		int moveItem = next;

		while ( ( moveItem > 0 ) && ( data[moveItem - 1 ] > insert ) )
		{
			data[ moveItem ] = data[ moveItem - 1 ];
			moveItem--;
		}

		data[ moveItem ] = insert;
	}
	cout << "\nSorted arrays:\n";

	for ( int i = 0; i < arraySize; i++ )
		cout << setw( 4 ) << data[ i ];

	getMedian(numberOfElements,	median);
	cout << "The Median is: " << median << endl;

	cout << endl;

	return 0;
}

Recommended Answers

All 4 Replies

I'm not seeing median declared in your main function at all. also the median is average value of all the values in the array. to calculate that you need to loop through the array and add all the values together. After that you then dived that sum by the number of elements to get the average. Also you need t pass the value median into your function by either a pointer or a reference so that what you do to it inside the function is reflected in main. Passing by value as you are doing now will not change the value of median in main().

Ok, so I used the 'get average' instead but now i get this error
error C2664: 'getMedian' : cannot convert parameter 3 from 'float **' to 'float *'
to be honest I'am somewhat lost at this point...been sitting for 5hrs+ trying to figure this thing out

void getMedian(int data[10], int numberOfElements, float *median)
{
	float sum = 0;
	for (int s = 0; s < numberOfElements; ++s)
	sum += data[s];
	*median = (float)(sum/numberOfElements);
 
}
int _tmain(int argc, _TCHAR* argv[], float *median)
{

	const int arraySize = 10;
	int data[ arraySize ] = { 34, 56, 4, 10, 77, 51, 93, 30, 5, 52 };
	int numberOfElements;
	int insert;
	
	numberOfElements = sizeof(data)/sizeof(int);

	cout << "Unsorted arrays:\n";

	for ( int i = 0; i < arraySize; i++ )
		cout << setw( 4 ) << data[ i ];

	for ( int next = 1; next < arraySize; next++ )
	{
		insert = data[ next ];

		int moveItem = next;

		while ( ( moveItem > 0 ) && ( data[moveItem - 1 ] > insert ) )
		{
			data[ moveItem ] = data[ moveItem - 1 ];
			moveItem--;
		}

		data[ moveItem ] = insert;
	}
	cout << "\nSorted arrays:\n";

	for ( int i = 0; i < arraySize; i++ )
		cout << setw( 4 ) << data[ i ];

	getMedian(data, 10, &median);
	cout << "The median is: " << *median << endl;
	
	cout << endl;

	return 0;
}

why do you have median in your main paramaters? medain should be defined in main

int main()
{
    float meadian = 0;
    // rest of your code
}
commented: great help +0

OMG, thank you so much that did the trick.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.