This is the working code of something I've been working on while learning C++.
How could I modify this so that ArraySortToMedian() uses pointer notation instead of array notation to handle the array?
All my attempts so far haven't worked so there is something in the logical relation or the syntax that I am missing. Thank you in advance.
#include <iostream>
#include <fstream>
double ArraySortToMedian(int [], int );
using namespace std;
int main()
{
ifstream infile;
infile.open("numbers.txt");
const int SIZE = 6;
int array[SIZE];
int i = 0;
double median;
if(!infile)
{
cout << "couldn't find 'numbers.txt'";
return 1;
}
while(i < SIZE && infile >> array[i])
i++;
infile.close();
for(i = 0; i < SIZE; i++)
cout << *(array + i) << "!\n";
median=ArraySortToMedian(array, SIZE);
cout<< "\n" << median << "\n";
return 0;
}
double ArraySortToMedian(int (x[]), int numElem)
{
bool swap;
int temp, i;
double m;
do
{
swap = false;
for(i = 0;i < (numElem - 1); i++)
{
if( x[i] > x[i + 1] )
{
temp = x[i];
x[i] = x[i + 1];
x[i + 1] = temp;
swap = true;
}
}
}
while (swap);
cout << "\n";
for(i = 0; i < numElem; i++)
cout << x[i] << "\n";
m = (x[numElem/2] + x[numElem/2]-1)/(double)2;
return(m);
}