Friday, December 16, 2016

Programming Challenge 5.13 - The Greatest And The Least Of These

/* The Greatest And The Least Of These - This program uses a loop that lets
   the user enter a series of integers. To signal the end of the series, the
   user has to enter -99. After all numbers have been entered, the program
   displays the largest and the smallest numbers entered. */

#include "Utility.h"

int main()
{
    /* Constant: Quit input */
    const int QUIT_INPUT = -99;

    /* Variables: Count (counter variable), Numbers, Smallest Number,
       Greatest Number */
    int count = 1,
        number = 0,
        smallestNumber = 0,
        greatestNumber = 0;

    /* Display: Introduction */
    cout << "\t\tThe Greatest And The Least Of These\n\n"
         << "This program allows you to enter a series of numbers.\n"
         << "After you have finished your input and enter: -99\n"
         << "the smallest and greatest among all values will be\n"
         << "displayed.\n\n";

    /* For loop: As long as number is not equal to QUIT_INPUT (-99), this
       loop will iterate */
    for (count; number != QUIT_INPUT; count++)
    {
        /* Input: Ask the user for numbers */
        cout << "\nPlease enter a series of numbers: ";
        cin >> number;

        /* Validation::Input: If the number equals LOWEST_NUMBER (-99),
           the input will be ignored, and not be stored in the variable
           smallestNumber */
        if (number == -99)
        {
            continue;
        }

        /* Conditional Expressions: Is the number the user entered smaller
           than the number stored in smallest number? If so, the number is
           stored in the variable smallest number */
        smallestNumber = smallestNumber < number ? smallestNumber : number;
      
        /* Conditional Expressions: Is the number the user entered greater
           than the number stored in greatest number? If so, the number is
           stored in the variable greatest number */
        greatestNumber = greatestNumber > number ? greatestNumber : number;

        /* Display: The currently smallest and greatest number during
           input */
        cout << "\n " << smallestNumber << " is currently the least of these.\n";
        cout << " " << greatestNumber << " is currently the greatest of these.\n";

    }

    /* Display: The Greatest And The Least Of These Numbers */
    cout << "\nThe least of these numbers was: " << smallestNumber << endl;
    cout << "The greatest of these numbers was: " << greatestNumber << endl;

    pauseSystem();
    return 0;
}

No comments:

Post a Comment