I'm suppose to create a function that finds the median in an array. I can assume that the array is sorted, so when I test my function I am entering my data in ascending order. In my main function it asks the user for how many numbers will be in the array, asks the user to input data, and then calls the calcMedian function. The calcMedian function then finds the median(the purpose of my assignment).
Here is my code:
#include<iostream>
using namespace std;
void calcMedian(double [], int);
int main()
{
int arraySize;
cout << "Please enter how many numbers you are going to enter: ";
cin >> arraySize;
double numbers[arraySize];
for(int j = 0; j < arraySize; j++)
{
cout << "Please enter a positive number: ";
cin >> numbers[j];
}
calcMedian(numbers, arraySize);
system("Pause");
}
void calcMedian(double array[], int size)
{
int middle;
double median;
if ((size % 2) != 0)
{
middle = ((size - 1) / 2);
median = array[middle];
}
else
{
middle = size / 2;
median = ((array[middle] + array[middle - 1]) / 2);
}
cout << "The median is: " << median << endl;
}
Everything works fine, the median is found and displayed properly, but I do not understand where I can use pointer notation or how it works. I am so incredibly lost in this chapter and do not know where to begin. Any help would be much appreciated.