I need help with my program. Please put comments in the program so i can learn f
ID: 644391 • Letter: I
Question
I need help with my program. Please put comments in the program so i can learn from you! Thanks!
Maximum amount of points offered, I need comments and the program working perfectly for the points!
This program needs to have the constructors listed in the program explanation and most of the work done in the contructors/definitions
Donation List
Problem Description:
This assignment focus on how an array of pointers can be used to display the contents of a second array in sorted order, without sorting the second array.
American Heart Association, the nation
Explanation / Answer
IDEONE link to program(to copy easily): http://ideone.com/BJnmwK
The program is given below. Feel free to ask any doubt.
#include <iostream>
using namespace std;
//Class Declaration
class DonationList
{
private:
int numDonations;
double *donations;
double **arrPtr;
void selectSort();
public:
DonationList(int num, double gifts[]);
~DonationList();
void show();
void showSorted();
};
//Constructor
//num: no. of gifts
//gifts array: holds the list of donation values.
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
DonationList::~DonationList()
{
if (numDonations > 0)
{
delete [ ] donations;
donations = 0;
delete [ ] arrPtr;
arrPtr = 0;
}
}
//Selection Sort, i hope you know this.
void DonationList::selectSort()
{
int minIndex;
double *minElem;
for (int 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;
}
}
//Display in sequential order
void DonationList::show()
{
for (int count = 0; count < numDonations; count++)
cout << donations[count] << " ";
cout << endl;
}
//Display in sorted order
void DonationList::showSorted()
{
for (int count = 0; count < numDonations; count++)
cout << *(arrPtr[count]) << " ";
cout << endl;
}
int main()
{
double funds[] = {5, 100, 5, 25, 10, 5, 25, 5, 5, 100, 10, 15, 10, 5, 10};
DonationList ahAssociation(15, funds);
cout << "The donations sorted in ascending order are: ";
ahAssociation.showSorted();
cout << "The donations in their original order are: ";
ahAssociation.show();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.