// Kurt Feiereisel CSC5 Chapter 9, p.538, #7
/*******************************************************************************
*
* Display Donations
* _____________________________________________________________________________
* This program displays the donations in the original order they were entered
* and in descending order.
* _____________________________________________________________________________
* INPUT:
* donations[NUM_DONATIONS] : Donations stored in order they were
* entered in.
* OUTPUT:
* donations[NUM_DONATIONS] : Donations stored in descending order
*
* ****************************************************************************/
#include <iostream>
using namespace std;
// Function Prototypes
void arrSelectSort(int *[], int NUM_DONATIONS);
void showArray(const int [], int NUM_DONATIONS);
void showArrPtr(int *[], int NUM_DONATIONS);
int main()
{
const int NUM_DONATIONS = 15; // Number of donations
// An array containing the donation amounts
int donations[NUM_DONATIONS] = {5, 100, 5, 25, 10, 5, 25, 5, 5, 100,
10, 15, 10, 5, 10};
// An array of pointers to int
int *arrPtr[NUM_DONATIONS];
// Each element of arrPtr is a pointer to int.
for(int count = 0; count < NUM_DONATIONS; count++)
arrPtr[count] = &donations[count];
// Sort the elements of the array of points
arrSelectSort(arrPtr, NUM_DONATIONS);
// Display the donations using the array of pointers. This will display
// them in sorted order.
cout << "The donations, sorted in descending 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);
return 0;
}
/*
* Definition of Function: arrSelectSort
* This function sorts an array into descending order. Each element of this
* array points to an element of another array. The elements of the first
* array will point to the elements in the second array in descending order
*/
void arrSelectSort(int *arr[], int size)
{
int startScan;
int maxIndex;
int *maxElem;
for(startScan = 0; startScan < (size - 1); startScan++)
{
maxIndex = startScan;
maxElem = arr[startScan];
for(int index = startScan + 1; index < size; index++)
{
if(*(arr[index]) > *maxElem)
{
maxElem = arr[index];
maxIndex = index;
}
}
arr[maxIndex] = arr[startScan];
arr[startScan] = maxElem;
}
}
/*
* Definition of Function: showArray
* This function displays the elements of an array
*/
void showArray(const int arr[], int size)
{
for (int count = 0; count < size; count++)
cout << arr[count] << " ";
cout << endl;
}
/*
* Definition of Function: showArrPtr
* This function displays the elements of the array
*/
void showArrPtr(int *arr[], int size)
{
for(int count = 0; count < size; count++)
cout << *(arr[count]) << " ";
cout << endl;
}