Question

#include <iostream>
using namespace std;

void arrSelectSort(int *[], int), showArrPtr(int *, int);
void showArray(int [] , int);

int main()
{
    int numDonations;
    int *arrPtr;
    cout << "What was the number of donations?: ";
    cin >> numDonations;
    arrPtr = new int[numDonations];
    cout << "What were the donations values?: ";
    for (int count = 0; count < numDonations; count++)
        cin >> arrPtr[count];
    arrSelectSort(arrPtr, 3);
    cout << "The donations, sorted in ascending order are: \n";
    showArrPtr(arrPtr, 3);
    cout << "The donations, in their orginal order are: \n";
    showArray(values, 3);
    system(" Pause ");
    return 0;
}

void arrSelectSort(int *array[], int size)
{
    int startScan, minIndex;
    int* 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 showArray(int array[], int size)
{
    for (int count = 0; count < size; count++)
        cout << array[count] << " ";
    cout << endl;
}

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

This is very confusing and I cannot figure out how to pass a dynamically memory allocation array to a function. I know it's possible because this is part of the exercise in the C++ packet. When I try to remove the bracket in the selectsort function, it gives me some errors inside. when I try to remove the * it gives me other errors.

Was it helpful?

Solution

void arrSelectSort(int *[], int)

The first parameter is of type int**.

You call the function like this:

arrSelectSort(arrPtr, 3);

where arrPtr is of type int*. This is the type mismatch that the compiler is informing your of.

I think that the error is in the declaration of arrSelectSort. It should be:

void arrSelectSort(int[], int)

The first parameter is now of type int*. This is exactly you need, a pointer to an array of int.

You then have a load of other errors in the implementation of arrSelectSort but I don't particularly want to attempt to debug them all.

You'll need to make minElem be of type int. And in a couple of other places you'll need to remove a level of indirection. For instance, this line:

if (*(array[index]) < *minElem)

should be:

if (array[index] < minElem)

and so on.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top