Monday, January 23, 2017

Programming Challenge 7.4 - Larger Than N

/* Larger Than N - This program has a function that accepts the following
   three arguments:

      * An array
      * The size of the array
      * A number n

   The function displays all of the numbers in the array that are greater
   than the number n. */

#include "Utility.h"

/* Prototype: Show greater N */
void showLargerThanN(const int[], int, int);

int main()
{
   /* Constant: Array Size */
   const int ARRAY_SIZE = 10;

   /* Variable: Larger N */
   int largerN[ARRAY_SIZE] = { 100, 1294, 128, 15, 31,
                               177, 212, 89, 45, 120 };

   /* Variable: My number, Again */
   int myNumber = 0;
   char again = ' ';

   /* Display: Introduction */
   cout << "\t\tLarger Than N\n\n"
        << "Enter a random number between 1 and 1300 and find out\n"
        << "which of the 10 numbers stored in a so called 'array'\n"
        << "are larger than your number.\n";

   /* Ask the user for a number as long as he or she does not wish
      to quit */
   do
   {
      cout << "\nPlease tell me your number: ";
      cin >> myNumber;

      /* Validate: Input */
      while (myNumber < 0)
      {
         cout << "\nSorry, the number you entered can not be accepted.\n"
              << "It has to be a positive number, at least 1.\n"
              << "Please tell me your number: ";
         cin >> myNumber;
      }

      /* Call: showLargerThanN */
      showLargerThanN(largerN, ARRAY_SIZE, myNumber);

      /* Ask if the user wishes to try another number */
      cout << "\n\nDo you wish to try another number (y/n)? ";
      cin >> again;

      /* Display: A quit message if the user wishes to exit */
      if (again == 'N' || again == 'n')
      {
         cout << "\nNow exiting this program ...\n"
              << "Press Enter to close the window.";
      }
   } while ((again != 'N') && (again != 'n'));

   pauseSystem();
   return 0;
}

/* **********************************************************
   Definition: showLargerThanN

   This function accepts an array and its size, and a number
   the user has entered, as its arguments. It determines if
   there are numbers larger than the user has entered. If
   there are any, they are then displayed.
   ********************************************************** */

void showLargerThanN(const int largerN[], int sizeOfArray,
                     int myNum)
{
   /* Variable: Compare, Is largest (to get the largest number stored
                in the array, to compare it to myNumber) */
   int compare = 0,
       isLargest = largerN[0];

    
      /* Display: The numbers found to be larger than N */
      for (compare = 0; compare < sizeOfArray; compare++)
      {
         if (largerN[compare] > myNum)
         {
            /* isLargest gets the largest number stored in the array */
            isLargest = largerN[compare];
            cout << "\nThe numbers larger than yours are: "
                 << largerN[compare];
         }
      }

      /* If isLargest is smaller than the number the user entered, a
         message is displayed */
      if (isLargest < myNum)
      {
         cout << "\nThere was no number greater than yours!" << endl;
      }
}

Example Output: 





No comments:

Post a Comment