1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
// This program shows the donations made to the United Cause
// by the employees of CK Graphics, Inc. It displays
// the donations in order from lowest to highest
// and in the original order they were received.
#include <iostream>
#include "donlist.h"
using namespace std;
int main()
{
double funds[] = {5, 100, 5, 25, 10,
5, 25, 5, 5, 100,
10, 15, 10, 5, 10 };
DonationList ckGraphics(15, funds);
cout << "The donations sorted in ascending order are:\n";
ckGraphics.showSorted();
cout << "The donations in their original order are:\n";
ckGraphics.show();
system("pause");
return 0;
}
#include <iostream> //needed for cout
#include "donlist.h"
using namespace std;
//**********************************************************
// Constructor. *
// The argument passed to num indicates the number of *
// elements in array passed to gifts. The gifts array *
// holds the list of donation values. The constructor *
// allocates the donations and arrPtr arrays. The gifts *
// array is copied to the donations array. The elements *
// of the arrPtr array are made to point to the elements *
// of the donations array, and then sorted in ascending *
// order by the selectSort function. *
//**********************************************************
DonationList::DonationList(int num, double gifts[])
{
numDonations = num;
if (num > 0)
{
// Allocate an array of doubles
donations = new double[num];
// Allocate an array of pointers-to-doubles
arrPtr = new double*[num];
// Initialize the arrays.
for (int count = 0; count < numDonations; count++)
{
donations[count] = gifts[count];
arrPtr[count] = &donations[count];
}
// Now, sort the array of pointers
selectSort();
}
}
//***********************************************************
// Destructor frees the memory allocated by the constructor *
//***********************************************************
DonationList::~DonationList()
{
if (numDonations > 0)
{
delete [ ] donations;
donations = 0;
delete [ ] arrPtr;
arrPtr = 0;
}
}
//************************************************************
// The selecSort function performs a selection sort on the *
// arrPtr array of pointers. The array is sorted on the *
// values its elements point to. *
//************************************************************
void DonationList::selectSort()
{
int scan, minIndex;
double *minElem;
for (scan = 0; scan < (numDonations - 1); scan++)
{
minIndex = scan;
minElem = arrPtr[scan];
for(int index = scan + 1; index < numDonations; index++)
{
if (*(arrPtr[index]) < *minElem)
{
minElem = arrPtr[index];
minIndex = index;
}
}
arrPtr[minIndex] = arrPtr[scan];
arrPtr[scan] = minElem;
}
}
//*******************************************************
// The show function uses cout to display the donations *
// array in sequential order. *
//*******************************************************
void DonationList::show()
{
for (int count = 0; count < numDonations; count++)
cout << donations[count] << " ";
cout << endl;
}
//*********************************************************
// The showSorted function uses cout to display the values*
// pointed to by the elements of the arrPtr array. Since *
// arrPtr is sorted, this function displays the elements *
// of the donations array in ascending order. *
//*********************************************************
void DonationList::showSorted()
{
for (int count = 0; count < numDonations; count++)
cout << *(arrPtr[count]) << " ";
cout << endl;
}
|