This program should accept array of integers and integers that indicate the number of elements in the array. the function should then determine the median of the array. Using pointer notation. I did research to see if someone had similar problem like me, but their was only other one, it was from a person that was way more advanced and was doing a lot more with her median function and other calculations, to confusing for my knowledge.
This is what i have created.
#include <iostream>
using namespace std;
//Function Prototypes.
void showArrayPtr(double*, int);
void showMedian(double, int);
void arrSelectSort(double*, int);
int main()
{
double *NUMBERS; //To dynamically allocate an array.
int numValues;
//To find out how many spaces to set aside.
cout << "How many numbers would you like to enter? ";
cin >> numValues;
//To dynamically allocate an array large enough to hold that many numbers.
NUMBERS = new double[numValues];
if( NUMBERS == NULL)
return 0;
//To get users input for numbers.
cout << "Enter the numbers below.\n";
for (int count = 0; count < numValues; count++)
{
cout << "Enter value #" << (count + 1) << ": ";
cin >> NUMBERS[count];
//Validate the input.
while (NUMBERS[count] <= 0)
{
cout << "zero or negative numbers not allowed.\n";
cout << "Enter value #" << ( count + 1 ) << ": ";
cin >> NUMBERS[count];
}
}
//Sort the numbers in the array pointer
arrSelectSort (NUMBERS, numValues);
//Will display them in sorted order.
cout << "The numbers in ascending order are: \n";
showArrayPtr(NUMBERS, numValues);
return 0;
}
void arrSelectSort(double *array, int size)
{
int startScan, minIndex;
double minElem;
for (startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
minElem = array[startScan];
for( int index = startScan + 1; index < size; index++)
{
if (array[index] < minElem)
{
minElem = array[index];
minIndex = index;
}
}
array[minIndex] = array[startScan];
array[startScan] = minElem;
}
}
void showArrayPtr(double *array, int size)
{
for( int count=0; count < size; count++)
cout << array[count] << " ";
cout << endl;
}
void showMedian(double *array, int size)
{
arrSelectSort(double *NUMBERS, int numValues);
int medIndex = 0;
double median = 0;
//If median is half way between.
if ((array % 2) !=0)
{
medIndex = ((numValues - 1) / 2);
median = array[medIndex];
}
else
{
medIndex = numValues / 2;
median = ((array[medIndex] + array[medIndex +1]) /2);
}
//Will display the median of the numbers.
cout << "The median of those numbers is: \n" << median << endl;
}
I think everything is ok, up until the void showMedian part or the last part that is where i get errors like double should be preseded by ), Function thoes not take 0 arguments, illegal left operator for %, syntax error ), and numValues undeclared identifier. Some honestly don't make sense to me because if i fix those syntax errors it will be for no reason, and the others, i thought i identified what every value is, apparently not. Could you guys assist as to pointing out what im doing wrong. Much appreciation.