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;
}