Beginning C++ Student That Needs Help With A Median Function Programing Challenge :-(

Here is the problem:

In statistics, when a set of values is sorted in ascending or descending order, its median is the middle value. If the set contains an even number of values, the median is the mean, or average, of the two middle values. Write a function that accepts as arguments the following:
A) An array of integers
B) An integer that indicates the number of elements in the array

The function should determine the median of the array. This value should be returned as a double.

Demonstrate your pointer prowess by using pointer notation instead of array notation in this function.

The code I have is below. I am not receiving any errors, but I am also not receiving any feedback in regards to my median function. My professor wanted us to incorporate selection sorting and that is the only output I'm receiving. Please help!


#include <iostream>

using namespace std;

double arrayMedian(int* [], int);
void selectionSort (int [], int);
void showArray (const int [], int);

int main()

{

const int SIZE =9;
int values [SIZE] ={5,1,3,2,9,4,6,7,8};

cout << "The unsorted vales are\n";

showArray (values, SIZE);

selectionSort (values,SIZE);

cout << "The sorted values are\n";
showArray (values, SIZE);

double arrayMedian( int *array[], int num );

return 0;

}

void selectionSort (int array [], int size)
{
int startScan, minIndex, minValue;
for (startScan = 0; startScan < (size -1); startScan++)
{
minIndex = startScan;
minValue = array [startScan];
for (int index = startScan +1; index < size; index ++)
{
if (array [index] < minValue)
{
minValue = array [index];
minIndex = index;
}
}
array [minIndex] = array [startScan];
array [startScan] = minValue;
}
}

void showArray (const int array [], int size)
{
for (int count = 0; count < size; count++)
cout << array [count] << " ";
cout << endl;
}


double arrayMedian (int *array[], int size)

{

int middlenumber;

int average, median;

middlenumber = size / 2;



if (size / 2)

{
median = *array[middlenumber];

cout << "The median of the array will be: " << median << endl;

}



else
{
average = (*array[middlenumber] + *array[middlenumber + 1]) / 2;

cout << "The median of the array will be: " << average << endl;

}

return median;

}
1
2
3
4
5
6
7
8
9
10
int main()
{
   const int SIZE =9;
   int values [SIZE] ={5,1,3,2,9,4,6,7,8};

   // ...

   double arrayMedian( int *array[], int num );  // this declares the function
   arrayMedian( values, SIZE ) ; // this calls the function (this is what you want to do)
}

Ok I made the change, now I get this error:

In function 'int main()':
26:30: error: cannot convert 'int*' to 'int**' for argument '1' to 'double arrayMedian(int**, int)'

I was wondering if it was because of the "[]" I have attached to the int *array, but I believe I need them there.
You got these right: void selectionSort (int [], int); and void showArray (const int [], int);

The median function is similar: double arrayMedian ( const int array[], int size ) ;
Like in showArray, we do not need to modify any of the numbers in the median function; ergo the const qualifier.

Note: These two are equivalent; in both, the type of the parameter array is 'pointer to const int'

double arrayMedian ( const int array[], int size ) ;

double arrayMedian ( const int* array, int size ) ;
So, I made all of the changes and here is my new code:

#include <iostream>

using namespace std;

double arrayMedian(const int array [], int size);
void selectionSort (int [], int);
void showArray (const int [], int);

int main()

{

const int SIZE =9;
int values [SIZE] ={5,1,3,2,9,4,6,7,8};

cout << "The unsorted vales are\n";

showArray (values, SIZE);

selectionSort (values,SIZE);

cout << "The sorted values are\n";
showArray (values, SIZE);

double arrayMedian( int* array[], int SIZE );
arrayMedian (values, SIZE);

return 0;

}

void selectionSort (int array [], int size)
{
int startScan, minIndex, minValue;
for (startScan = 0; startScan < (size -1); startScan++)
{
minIndex = startScan;
minValue = array [startScan];
for (int index = startScan +1; index < size; index ++)
{
if (array [index] < minValue)
{
minValue = array [index];
minIndex = index;
}
}
array [minIndex] = array [startScan];
array [startScan] = minValue;
}
}

void showArray (const int array [], int size)
{
for (int count = 0; count < size; count++)
cout << array [count] << " ";
cout << endl;
}


double arrayMedian (int* array[], int size)

{

int middlenumber;

int average, median;

middlenumber = size / 2;



if (size / 2)

{
median = *array[middlenumber];

cout << "The median of the array will be: " << median << endl;

}



else
{
average = (*array[middlenumber] + *array[middlenumber + 1]) / 2;

cout << "The median of the array will be: " << average << endl;

}

return median;

}

However, I'm still getting this error:

In function 'int main()':
26:30: error: cannot convert 'int*' to 'int**' for argument '1' to 'double arrayMedian(int**, int)'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <algorithm>
// #include <cmath>
// #include <cassert>

double median( const int array[], int size ) ;
void print( const int array[], int size ) ; // invariant: array is sorted

int main()
{
    const int SIZE = 9 ;
    int array[SIZE] = { 5, 1, 3, 2, 9, 4, 6, 7, 8 };
    std::sort( array, array+SIZE ) ; // sort the array
                                       // in your program, use your sort function
    {
        std::cout << "the median of array " ;
        print( array, SIZE ) ;
        const double med = median( array, SIZE ) ;
        std::cout << " is " << med << '\n' ;
    }

    {
        std::cout << "the median of array " ;
        print( array, SIZE-1 ) ;
        const double med = median( array, SIZE-1 ) ;
        std::cout << " is " << med << '\n' ;
    }
}

void print( const int array[], int size )
{
    std::cout << "[ " ;
    for( int i = 0 ; i < size ; ++i ) std::cout << array[i] << ' ' ;
    std::cout << "] (size==" << size << ')' ;
}

double median( const int array[], int size ) // invariant: array is sorted
{
    /*////////////// you may want to ignore these for now ///////////////////

    if( array == nullptr || size < 1 ) return NAN ; // NAN: not a number

    assert( std::is_sorted( array, array+size ) ) ; // assert the invariant: array is sorted

    ///////////////////////////////////////////////////////////////////??/*/

    // note: if size is odd, both size/2 and (size-1)/2 yield the position of the middle element
    //       eg. size == 23  size/2 == 11 (size-1)/2 == 11
    //
    // if size is even, size/2 and (size-1)/2 yield the positions of the two middle elements
    //       eg. size == 24  size/2 == 12 (size-1)/2 == 11
    return ( array[ size/2 ] + array[ (size-1) / 2 ] ) / 2.0 ; // 2.0: avoid integer division
}

http://coliru.stacked-crooked.com/a/391d8b9afefda1f8
Thank you very much for a proper code. Now I'm just trying to figure out how to use your code to fix mine lol
Topic archived. No new replies allowed.