Saturday, April 1, 2017

Programming Challenge 9.9 - Median Function

/* Median Function - In statistics, when a set of values is sorted in
   ascending or descending order, its median is the middle value. If
   the set contains an even number of values, the median is the mean,
   or average, of the two middle values.
  
   This program contains a function that accepts the following:
  
      * An array of integers
      * An integer that indicates the number of elements in the array

   The function determines the median of the array. This value is
   returned as double. (The numbers are sorted).

   Pointer prowess is demonstrated by using pointer notation instead
   of array notation. */

#include "Utility.h"

void   getNumbers(int *, int);
void   sortNumbers(int *, int);
double getMedian(int *, int);
void   freeMem(int *);

int main()
{
   /* Dynamically allocated array to
      hold a list of numbers */
   int   *numList = nullptr;

   int    numels = 0;
   double median = 0.0;

   cout << "\n\tMEDIAN DISCOVERY FUNCTION\n\n"
        << "\n\tHow many elements should your set of numbers contain? ";
   cin >> numels;

   numList = new int[numels];

   /* Gets a list of numbers by way of input */
   getNumbers(numList, numels);

   /* Sorts the numbers in ascending order by using an selection-sort
      algorithm */
   sortNumbers(numList, numels);

   /* Determines and returns the median */
   median = getMedian(numList, numels);

   /* Display the median value */
   cout << fixed << showpoint << setprecision(2);
   cout << "\n\tThe median is: " << median << " ";

   /* Frees the memory before the program exits */
   freeMem(numList);

   pauseSystem();
   return 0;
}

/* **********************************************************
   Definition: getNumbers

   This function accepts numList and numels as arguments. The
   numbers entered by the user are stored in numlist.
   ********************************************************** */

void getNumbers(int *numList, int numels)
{
   cout << "\n\n\tPlease enter " << numels << " numbers:\n\n";

   for (int index = 0; index < numels; index++)
   {
      cout << "\tNumber #" << (index + 1) << ": ";
      cin >> *(numList + index);
   }
   cout << "\n";
}

/* **********************************************************
   Definition: sortNumbers

   This function accepts numList and numels as arguments. It
   uses a selection-sort algorithm to sort the numbers in an
   ascending order.
   ********************************************************** */

void sortNumbers(int *numList, int numels)
{
   int startScan = 0,
       index = 0,
       minIndex = 0,
       minEl = 0;

   for (startScan = 0; startScan < numels; startScan++)
   {
      minIndex = startScan;
      minEl = *(numList + startScan);

      for (index = startScan + 1; index < numels; index++)
      {
         if (*(numList + index) < minEl)
         {
            minEl = *(numList + index);
            minIndex = index;
         }
      }

      *(numList + minIndex) = *(numList + startScan);
      *(numList + startScan) = minEl;
   }

   cout << "\n\tYour numbers in sorted order:\n\n";

   for (int index = 0; index < numels; index++)
   {
      cout << "\t" << *(numList + index) << " \n";
   }
}

/* **********************************************************
   Definition: getMedian

   This function accepts numList and numels as arguments. It
   determines the median and returns it. This is achieved by
   using a ternary operator, that does the following:

      * The first condition determines whether the number of
        elements in the array is even or odd.

      * If the number is even, midLower gets the middle value
        found in the lower half of the array, and midUpper is
        assigned the middle value at the upper half of the
        array.

      * Then the median is calculated by adding the middle
        values and dividing the sum by 2.

      * If the number of elements is odd, median gets the
        middle element. The calculation to determine it has
        already been carried out.

   Once the median is found, it is returned.
   ********************************************************** */

double getMedian(int *numList, int numels)

   double median = 0.0;

   int      middleElem = numels / 2,
          midLower = 0,
          midUpper = 0;

   numels % 2 == 0 ? midLower = *(numList + middleElem - 1),
                                  midUpper = *(numList + middleElem),
                                  median = (midUpper + midLower) / 2 :
                                  median = *(numList + middleElem);

   return median;
}

/* **********************************************************
   Definition: freeMem

   This function accepts numList as argument. It frees the
   allocated memory before the program exits.
   ********************************************************** */

void freeMem(int *numList)
{
   delete[] numList;

   numList = nullptr;
}

Example Output: 




No comments:

Post a Comment