I see that this problem has been posted a few times before but I cannot manage to find the bug in my program. When it runs, it does not return the values in ascending order. For example, upon entering two test values, I get 0 and 1519848. Any hints would be greatly appreciated.
#include <iostream>
#include <iomanip>
using namespace std;
// Function prototypes
void arrSelectSort(int *[], int);
void showArrPtr(int *[], int);
int main()
{
double *scores, // To dynamically allocate an array
total = 0.0, // Accumulator
average; // To hold average scores
int numScores, //To hold the number of test scores
count; //Counter variable
// An array of pointers to int.
int *arrPtr[numScores];
// Each element of arrPtr is a pointer to int. Make each
// element point to an element in the scores array.
// Sort the elements of the array of pointers.
arrSelectSort(arrPtr, numScores);
// Get the number of test scores
cout << "How many test scores would you like to enter? ";
cin >> numScores;
// Dynamically allocate an array large enough to hold
// that many test scores
scores = new double[numScores];
// Get the test scores
cout << "Enter the test scores below.\n";
for (int count = 0; count < numScores; count++)
{
cout << "Score " << (count + 1) << ": ";
cin >> scores[count];
if (scores[count] < 0)
{
cout << " \nNegative test values are not possible.\n";
cout << "Please run the program again and enter a positive integer.\n";
while (scores[count] < 0);
}
}
// Calculate the total of the scores
for (int count = 0; count < numScores; count++)
{
total += scores[count];
}
// Calculate the average score.
average = total / numScores;
// Display the results.
cout << fixed << showpoint << setprecision(2);
cout << "Average Score: " << average << endl;
// Display the donations using the array of pointers. This
// will display them in sorted order.
cout << "The scores sorted in ascending order are: \n";
showArrPtr(arrPtr, numScores);
// Free dynamically allocated memory
delete [] scores;
scores = 0;
cout << endl;
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 showArrPtr(int *array[], int size)
{
for (int count = 0; count < size; count++)
cout << *(array[count]) << " ";
cout << endl;
}