Search code examples
c++arrayspointersfunction-pointersselection-sort

(C++) Trying to finish up a quick program but I'm not sure where I went wrong?


The program description is below. When I try to compile, I get errors saying 'cannot convert 'double' to 'double**' for argument.

Write a program that dynamically allocates an array large enough to hold a user-defined number of test scores. Once all the scores are entered, the array should be passed to a function that sorts them in ascending order. Another function should be called that calculates the average score. The program should display the sorted list of scores and averages with appropriate headings.

NOTE: I am fairly new to using pointers in C++ and I do realize that the error in my code may be fairly easy for some to spot, so please do go easy. Also, if there are any suggestions as to what I can improve on or where I might find additional references to pointers, that would be great!

#include <iostream>
#include <iomanip>

using namespace std; 

//Function Prototypes
void arrSelectionSort( double *[], int );
double testAverage( double *[], int );

int main() { 

    double *testScores; //To dynamically allocate an array

    int numTest;    //To hold the number of test - size of array
    int count;      //Counter variable


    //User input
    cout <<"Please enter the number of test scores: ";
    cin >> numTest; 

    //Dynamically allocate an array for test scores 
    testScores = new double[numTest];

    //Obtain the individual test scores from user
    cout << "\nEnter each test score.\n";
    for ( count = 0; count < numTest; count++ ) { 

        cout << "Test " << ( count + 1 ) << ": " << endl; 
        cin >> testScores[count];

    }

    //Function call to sorting algorithm 
    arrSelectionSort( testScores, numTest );


    //Display sorted array
    cout << "Here are the sorted test scores.\n";
    for ( int x = 0; x < numTest; x++ ) { 

        cout << "\nTest " << (x+1) << ": " << testScores[x];

    } 


    //Display average of all test scores
    cout <<"\nAverage of all test scores is: " << testAverage( testScores, numTest );


    //Free up allocated memory
    delete  [] testScores;
    testScores = 0; 

    return 0;
}

void arrSelectionSort( double *arr[], int size ) { 

    int startScan, minIndex; 
    double *minElement; 

    for ( startScan = 0; startScan < ( size - 1 ); startScan++ ) { 

        minIndex = startScan; 
        minElement = arr[startScan]; 

        for ( int index = startScan + 1; index < size; index ++ ) { 

            if (*(arr[index]) < *minElement) {

                minElement = arr[index]; 
                minIndex = index;  
            }

        }

        arr[minIndex] = arr[startScan]; 
        arr[startScan] = minElement; 
    }
}

double testAverage( double *arr[], int size ) { 

    double average;
    double total; 

    //Accumulating Loop
    for ( int count = 0; count < size; count++ ) { 

        total += arr[count];
    }

    average = total/size; 

    return average; 
}

Solution

  • Remove the * from your function parameters:

    void arrSelectionSort( double arr[], int size );
    double testAverage( double arr[], int size );
    

    You want to pass in an array of double values, not an array of double* pointers.

    And then, inside of arrSelectionSort(), get rid of the uses of * since you want to sort values, not sort pointers:

    void arrSelectionSort( double arr[], int size ) { 
    
        int startScan, minIndex; 
        double minElement; 
    
        for ( startScan = 0; startScan < ( size - 1 ); startScan++ ) { 
    
            minIndex = startScan; 
            minElement = arr[startScan]; 
    
            for ( int index = startScan + 1; index < size; index ++ ) { 
    
                if (arr[index] < minElement) {
    
                    minElement = arr[index]; 
                    minIndex = index;  
                }
    
            }
    
            arr[minIndex] = arr[startScan]; 
            arr[startScan] = minElement; 
        }
    }