fork download
  1. // Kurt Feiereisel CSC5 Chapter 9, p.538, #7
  2. /*******************************************************************************
  3.  *
  4.  * Display Donations
  5.  * _____________________________________________________________________________
  6.  * This program displays the donations in the original order they were entered
  7.  * and in descending order.
  8.  * _____________________________________________________________________________
  9.  * INPUT:
  10.  * donations[NUM_DONATIONS] : Donations stored in order they were
  11.  * entered in.
  12.  * OUTPUT:
  13.  * donations[NUM_DONATIONS] : Donations stored in descending order
  14.  *
  15.  * ****************************************************************************/
  16. #include <iostream>
  17. using namespace std;
  18.  
  19. // Function Prototypes
  20. void arrSelectSort(int *[], int NUM_DONATIONS);
  21. void showArray(const int [], int NUM_DONATIONS);
  22. void showArrPtr(int *[], int NUM_DONATIONS);
  23.  
  24. int main()
  25. {
  26. const int NUM_DONATIONS = 15; // Number of donations
  27.  
  28. // An array containing the donation amounts
  29. int donations[NUM_DONATIONS] = {5, 100, 5, 25, 10, 5, 25, 5, 5, 100,
  30. 10, 15, 10, 5, 10};
  31.  
  32. // An array of pointers to int
  33. int *arrPtr[NUM_DONATIONS];
  34.  
  35. // Each element of arrPtr is a pointer to int.
  36. for(int count = 0; count < NUM_DONATIONS; count++)
  37. arrPtr[count] = &donations[count];
  38.  
  39. // Sort the elements of the array of points
  40. arrSelectSort(arrPtr, NUM_DONATIONS);
  41.  
  42. // Display the donations using the array of pointers. This will display
  43. // them in sorted order.
  44. cout << "The donations, sorted in descending order, are: \n";
  45. showArrPtr(arrPtr, NUM_DONATIONS);
  46.  
  47. // Display the donations in their original order
  48. cout << "The donations, in their original order, are : \n";
  49. showArray(donations, NUM_DONATIONS);
  50. return 0;
  51. }
  52. /*
  53.  * Definition of Function: arrSelectSort
  54.  * This function sorts an array into descending order. Each element of this
  55.  * array points to an element of another array. The elements of the first
  56.  * array will point to the elements in the second array in descending order
  57.  */
  58.  
  59. void arrSelectSort(int *arr[], int size)
  60. {
  61. int startScan;
  62. int maxIndex;
  63. int *maxElem;
  64.  
  65. for(startScan = 0; startScan < (size - 1); startScan++)
  66. {
  67. maxIndex = startScan;
  68. maxElem = arr[startScan];
  69. for(int index = startScan + 1; index < size; index++)
  70. {
  71. if(*(arr[index]) > *maxElem)
  72. {
  73. maxElem = arr[index];
  74. maxIndex = index;
  75. }
  76. }
  77. arr[maxIndex] = arr[startScan];
  78. arr[startScan] = maxElem;
  79. }
  80. }
  81.  
  82. /*
  83.  * Definition of Function: showArray
  84.  * This function displays the elements of an array
  85.  */
  86.  
  87. void showArray(const int arr[], int size)
  88. {
  89. for (int count = 0; count < size; count++)
  90. cout << arr[count] << " ";
  91. cout << endl;
  92.  
  93. }
  94. /*
  95.  * Definition of Function: showArrPtr
  96.  * This function displays the elements of the array
  97.  */
  98. void showArrPtr(int *arr[], int size)
  99. {
  100. for(int count = 0; count < size; count++)
  101. cout << *(arr[count]) << " ";
  102. cout << endl;
  103. }
  104.  
Success #stdin #stdout 0s 5296KB
stdin
Standard input is empty
stdout
The donations, sorted in descending order, are: 
100 100 25 25 15 10 10 10 10 5 5 5 5 5 5 
The donations, in their original order, are : 
5 100 5 25 10 5 25 5 5 100 10 15 10 5 10