Friday, March 10, 2017

Programming Challenge 8.11 - Using Files - String Selection Sort Modification

Example File: names.txt

/* Using Files - String Selection Sort Modification - This program is
   a modification of Programming Challenge 8.6. It reads in 20 strings
   from the file "names.txt" and sorts them. */

#include "Utility.h"

int getFile(string[], const int);
void selectionSort(string[], const int);
void displayUnsorted(const string[], const int);
void displaySorted(const string[], const int);

int main()
{
   const int NUM_NAMES = 20;
  
   int returnCode = 0;

   string names[NUM_NAMES] = { " " };

   /* Gets and stores the names stored in "names.txt" */
   returnCode = getFile(names, NUM_NAMES);

   if (returnCode != -1)
   {
      /* Displays the unsorted names list */
      displayUnsorted(names, NUM_NAMES);

      /* Sorts the names with selection sort */
      selectionSort(names, NUM_NAMES);

      /* Displays the sorted name list */
      displaySorted(names, NUM_NAMES);
   }

   pauseSystem();
   return 0;
}

/* **********************************************************
   Definition: getFile

   This function accepts names and NUMELS, containing the
   number of elements in the array, as arguments. If opened
   successfully, the content of the file "names.txt" will be
   stored in names. In case of a file processing error, an
   error message is displayed, and the program will exit.
   ********************************************************** */

int getFile(string names[], const int NUMELS)
{
   ifstream getNames;
  
   int count = 0;

   getNames.open("names.txt");

   if (getNames)
   {
      while (count < NUMELS && !getNames.eof())
      {
         getline(getNames, names[count]);

         count++;
      }
   }
   else
   {
      cout << "\nFile open error: The file 'names.txt' could not\n"
           << "be opened or processed. Please make sure that the filename is\n"
           << "correct and the file is not damaged or has been moved from the\n"
           << "program folder. Please close this program now and try again ...\n\n";

      return -1;
   }

   getNames.close();

   return 0;
}

/* **********************************************************
   Definition: displayUnsorted

   This function accepts names and NUM_Names, containing the
   number of elements stored in the array, as its arguments.
   It displays the unsorted name list stored in names.
   ********************************************************** */

void displayUnsorted(const string names[], const int NUMELS)
{
   cout << "\n\tUNSORTED NAME-LIST\n\n";

   for (int index = 0; index < NUMELS; index++)
   {
      cout << "\t" << names[index] << " \n";
   }
   cout << "\n";
}

   /* **********************************************************
   Definition: selectionSort

   This function accepts names and NUM_Names, containing the
   number of elements stored in the array, as its arguments.
   It uses election sort algorithm is used to sort the names
   in alphabetical order.
   ********************************************************** */

void selectionSort(string names[], const int NUM_NAMES)
{
   int startScan = 0,
       minIndex = 0,
       index = 0;

   string minAlpha = " ";

   for (startScan = 0; startScan < (NUM_NAMES - 1); startScan++)
   {
      minIndex = startScan;
      minAlpha = names[startScan];

      for (index = startScan + 1; index < NUM_NAMES; index++)
      {

         if (names[index] < minAlpha)
         {
            minAlpha = names[index];
            minIndex = index;
         }
      }

      names[minIndex] = names[startScan];
      names[startScan] = minAlpha;
   }         
}

/* **********************************************************
   Definition: displaySorted

   This function accepts names and NUM_Names, containing the
   number of elements stored in the array, as its arguments.
   It displays the names in ascending alphabetic order.
   ********************************************************** */

void displaySorted(const string names[], const int NUMELS)
{
   cout << "\n\tSORTED NAME-LIST\n\n";

   for (int index = 0; index < NUMELS; index++)
   {
      cout << "\t" << names[index] << " \n";
   }
   cout << "\n";
}

Example Output:




No comments:

Post a Comment