Sunday, January 15, 2017

Programming Challenge 6.22 - isPrime Function

/* isPrime Function - A prime number is a number that is only evenly
   divisible by itself and 1. For example, the number 5 is prime
   because it can only be evenly divided by 1 and 5. The number 6,
   however, is not prime because it can be divided evenly by 1, 2, 3
   and 6.
  
   The following main function is used:

      * isPrime()

   The following additional functions are used:

      * showMenu()
      * showIntro()
      * getData()
      * displayResult()

   The function is demonstrated in this program. */

#include "Utility.h"

/* Prototypes: Is prime, Show menu, Show Intro, Get data, Display result */
bool isPrime(int);
void showMenu();
void showIntro();
void getData();
void displayResult(int);

int main()
{
   showMenu();

   getData();

   return 0;
}

/* **********************************************************
   Definition: showMenu

   This function shows a small menu
   ********************************************************** */

void showMenu()
{
   /* Variable: Again, Introduction (initialized to 'I') */
   char intro = ' ',
      again = ' ';

   /* * Ask if the user wants to view a short introduction to
        the program
   * Call: showIntro, catchInfiniteLoop
   * Validate: Menu choice */
   cout << "\t\tPrimality Test\n\n"
      << "Do you wish to view a short introduction (Y/N)? ";
   cin >> intro;

   if (intro == 'y')
   {
      showIntro();
   }

   while (!(intro == 'y' || (intro == 'n')))
   {
      cout << "\nInvalid selection. Please enter 'y' if you wish to view an\n"
           << "introduction, or 'n' if you wish to start right away.\n";
      cout << "Please select: ";
      cin >> intro;
   }
}

/* **********************************************************
   Definition: showIntro

   showIntro displays a short introduction, so that the user
   can learn about the functionality and purpose of this
   program.
   ********************************************************** */

void showIntro()
{
   cout << "\n\t\tPrimality Test - Introduction\n\n"
        << "This program uses a custom function to test a number for\n"
        << "primality. The result is returned as boolean value. In other\n"
        << "words:\n\n"
        << " * If a number is a prime number, the result returned\n"
        << "   is true\n"
        << " * If a number is not a prime number, the result returned\n"
        << "   is false.\n\n"
        << "The following simple rules are applied:\n\n"
        << " * 1. Is the number % 2, 3 ... n-1 equal to 0\t| Yes ? not prime |\n"
        << " * 2. Is the number entered <= 1\t\t| Yes ? not prime |\n"
        << " * 3. Is the number entered divisible by itself | Yes ? is prime  |\n\n"
        << "Once one of these conditions is met, the result will be returned\n"
        << "and displayed. As long as you don't choose to quit, you can test\n"
        << "any positive number for primality.\n";
}

/* **********************************************************
   Definition: getData

   This function asks for a number to be tested, then calls
   displayResult, to display the result.
   ********************************************************** */

void getData()
{
   /* Variable: Number */
   int number = 0,
       displayIntroduction = 0;

   char again = ' ';

   /* As long as the user wishes not to quit, he or she is asked
      to enter a number to test for primality.

      Get: Number, Choice to continue or quit
      Call: catchInifiniteLoop, displayResult
      Validate: Input, User choice */
   do
   {
      cout << "\nEnter a number and I will tell you if it is prime: ";
      cin >> number;
      cout << endl;

      while (number < 0)
      {
         cout << "\nThe entered number is invalid. Please enter a positive\n"
              << "integer value (1 or above): ";
         cin >> number;

         catchInfiniteLoop();
      }

      displayResult(number);

      cout << "\nDo you wish to try another number? "
           << "(Enter 'y' to continue or 'n' to quit): ";
      cin >> again;

      while (!(again == 'y' || (again == 'n')))
      {
         cout << "\nInvalid menu choice!\n"
              << "Do you wish to try another number? ";
         cin >> again;

         catchInfiniteLoop();
      }

      if (again == 'n')
      {
         cout << "\nThank you for testing this program!\n"
              << "(Press Enter to exit)\n";

         pauseSystem();
      }

   } while (again != 'n');
}

/* **********************************************************
   Definition: isPrime

   This function takes an integer value as argument and tests
   it for primality. If the number is prime, it returns true,
   else it returns false.
   ********************************************************** */

bool isPrime(int primeNumber)
{
   /* Variable: Prime test (initialized to true) */
   bool primeTest = true;

   /* If any number lower than or equal to 1 is entered,
      primeTest gets false, and the result false is returned */
   if (primeNumber <= 1)
   {
      return primeTest = false;
   }
  
   /* primeNumber is tested until one of the following
      condition is met:
     
         1. primeNumber % divisor (2, 3 ... n-1) equals 0,
            which means that the number is not prime, so
            primeTest gets false, and the result false is
            returned
       
         2. primeNumber is divisible only by itself, then
            the loop exits, primeTest gets true, and true
            is returned
          
      Since divisior is already initialized to 2, and 2 is
      the only even number that is prime, this case is
      taken care of and 2 is displayed as prime number */
   for (int divisor = 2; divisor < primeNumber; divisor++)
   {
      /* Display: The steps it takes until one of the conditions
                  is met. (Should be deleted or commented out
                  when greater numbers are tested that are
                  prime ...) */
      cout << "Number: " << primeNumber << " % " << divisor << "\n";

      if (primeNumber % divisor == 0)
      {
         return primeTest = false;
      }         
   }

   /* Return: primeTest true if number is prime */
   return primeTest = true;
}

/* *******************************************************
   Definition: displayResult

   This function takes the result of primeTest as argument
   and displays whether the number is prime or not.
   ******************************************************* */

void displayResult(int prime)
{
   if (isPrime(prime))
   {
      cout << "\n" << prime << " is a prime number.\n";
   }
   else
   {
      cout << "\n" << prime << " is not a prime number.\n";
   }
}

Example Output





No comments:

Post a Comment