Hello, I posted this earlier today andddd I am now left with one issue: the original sequence is still ordered. No clue how to fix it, please help if you can :)
This can't be your real code, or you have left out some of it. The declaration of arrSelectSort does not match the definition. The type of the first parameter is different.
You have an array. You sort it. You print it, and then you print it again. Well of course it will be printed in the same order both times because it's the same array that you have not modified in between.
Note that there is no difference in the behaviour between showArray and showArrPtr. They do exactly the same thing.
I don't know what the heck I was on when I posted that, a reminder to all: don't try to code when you're too tired, you'll make the weirdest...mistakes...if you can even call it that.
#include <iostream>
usingnamespace std;
// Function prototypes
void arrSelectSort(int *[], int);
void showArray(int[], int);
void showArrPtr(int *[], int);
int main()
{
constint NUM_DONATIONS = 15; // Number of donations
int * donations = newint[NUM_DONATIONS];
for (int count = 0; count < NUM_DONATIONS; count++)
{
cout << "Donation " << (count + 1) << ": ";
cin >> donations[count];
}
// An array of pointers to int.
int *arrPtr[NUM_DONATIONS];
// Each element of arrPtr is a pointer to int. Make each
// element point to an element in the donations array.
for (int count = 0; count < NUM_DONATIONS; count++)
arrPtr[count] = &donations[count];
// Sort the elements of the array of pointers.
arrSelectSort(arrPtr, NUM_DONATIONS);
// Display the donations using the array of pointers. This
// will display them in sorted order.
cout << "The donations, sorted in ascending order are: \n";
showArrPtr(arrPtr, NUM_DONATIONS);
// Display the donations in their original order.
cout << "The donations, in their original order are: \n";
showArray(donations, NUM_DONATIONS);
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;
}