Thursday, March 16, 2017

Programming Challenge 9.6 - Case Study Modification #1

/* Case Study Modification #1 - 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.

   This is a modification of Program 9-19, the United Cause case study
   program. It now can be used with any set of donations. The program
   dynamically allocates the donations array and asks the user to input
   its values. */

#include "Utility.h"

/* Function prototypes */
void getDonations(double *, const int);
void initPtrArray(double *[], double *, const int);
void arrSelectSort(double *[], const int);
void displayArray(const double *, const int);
void showArrPtr(double *[], const int);

int main()
{
   /* Dynamically allocated arrays */
   double *donations = nullptr;
   double **arrPtr = nullptr;

   int numDonations = 0;

   cout << "\n\t\tCK Graphics United Cause Donations\n\n"
        << "\tEnter the number of donations we collected: ";
   cin >> numDonations;

   /* Dynamically allocated array */
   donations = new double[numDonations];

   /* Array of pointers to double*/
   arrPtr = new double *[numDonations];

   /* Initializes arrPtr */
   initPtrArray(arrPtr, donations, numDonations);

   /* Gets the donations from the user */
   getDonations(donations, numDonations);

   /* Sorts the array with an selection sort algorithm */
   arrSelectSort(arrPtr, numDonations);

   /* Displays the donations using the array of pointers in
      sorted order */
   showArrPtr(arrPtr, numDonations);

   /* Display the donations in sorted order */
   displayArray(donations, numDonations);

   /* Frees the memory */
   delete[] arrPtr;
   delete[] donations;

   donations = nullptr;
   arrPtr = nullptr;

   pauseSystem();
   return 0;
}

/* **********************************************************
   Definition: initPtrArray

   This function accepts arrPtr, donations, and numDonations
   indicating the number of elements in the array of pointers
   and in donations as arguments. It initializes the elements
   in arrPtr, then it makes each element point to an element
   in donations.
   ********************************************************** */

void initPtrArray(double *arrPtr[], double *donations, const int numDonations)
{
   for (int index = 0; index < numDonations; index++)
   {
      *(arrPtr + index) = { nullptr };
   }

   for (int index = 0; index < numDonations; index++)
   {
      *(arrPtr + index) = &donations[index];
   }
}

/* **********************************************************
   Definition: getDonations

   This function accepts donations and numDonations as its
   arguments. It asks the user for the amount of donations
   and stores it in the array.
   ********************************************************** */

void getDonations(double *donations, const int numDonations)
{
   cout << "\n";
   for (int index = 0; index < numDonations; index++)
   {
      cout << "\tDonation #" << setw(3) << right << (index + 1)
         << ": $ ";
      cin >> *(donations + index);
   }
}

/* **********************************************************
   Definition: displayArray

   This function accepts arrPtr and numDonations as its
   arguments. It displays the contents of arrPtr.
   ********************************************************** */

void displayArray(const double *donations, const int numDonations)
{
   int index = 0;

   cout << "\n\tThe donations, in their original order are: \n";
   for (int index = 0; index < numDonations; index++)
   {
      cout << "\n\tDonation #" << setw(3) << right
           << (index + 1) << ": $ ";
      cout << setw(8) << right << *(donations + index) << " ";
   }
}

/* **********************************************************
   Definition: arrSelectSort

   This accepts arrPtr and numDonations as its arguments. It
   performs an ascending order selection sort on arrPtr,
   which is an array of pointers. Each element of the array
   points to an element of a second array. After sorting the
   elements, arrPtr will point to the elements of the second
   array in ascending order.
   ********************************************************** */

void arrSelectSort(double *arrPtr[], int numDonations)
{
   int startScan = 0,
       minIndex = 0;

   double *minElem;

   for (startScan = 0; startScan < (numDonations - 1); startScan++)
   {
      minIndex = startScan;
      minElem = arrPtr[startScan];

      for(int index = startScan + 1; index < numDonations; index++)
      {
         if (*(arrPtr[index]) < *minElem)
         {
            minElem = arrPtr[index];
            minIndex = index;
         }
      }
      arrPtr[minIndex] = arrPtr[startScan];
      arrPtr[startScan] = minElem;
   }
}

/* **********************************************************
   Definition: showArrPtr

   This function accepts arrPtr and numDonations as its
   arguments. It displays the contents of the array pointed
   to by arrPtr.
   ********************************************************** */

void showArrPtr(double *arrPtr[], const int numDonations)
{
   cout << fixed << showpoint << setprecision(2);

   cout << "\n\tThe donations, sorted in ascending order are: \n";

   for (int index = 0; index < numDonations; index++)
   {
      cout << "\n\tDonation #" << setw(3) << right << (index + 1)
           << ": $ ";
      cout << setw(8) << right << *(arrPtr[index]) << " ";
   }
   cout << "\n";
}

Example Output:





No comments:

Post a Comment