Sunday, January 15, 2017

Programming Challenge 6.23 - Prime Number List

Example File: PrimeNumbers.txt

/* Prime Number List - The function written in Programming Challenge 6.22
   is used in this program to test a range of numbers from 1 through 100
   for primality. The numbers found to be prime are then written to a file
   called "PrimeNumbers.txt".
  
   The following main function is used:
  
      * isPrime
     
   The following additional function is used:
     
      * writeToDisk */

#include "Utility.h"

/* Prototype: Is prime, Write to disk */
bool isPrime(int);
void writeToDisk(int);

int main()
{
   /* Variable: Prime */
   int prime = 0;

   /* Call: writeToDisk */
   writeToDisk(prime);

   pauseSystem();
   return 0;
}

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

   This function passes the numbers 1 through 100 to
   isPrime to test them for primality, accepts the result,
   and writes the prime numbers found to disk.
   ******************************************************* */

void writeToDisk(int prime)
{
   /* Instantiate: File stream object */
   ofstream primeNumbers;

   /* Open: file for writing */
   primeNumbers.open("PrimeNumbers.txt");

   /* If the file was successfully created, the prime numbers discovered
      are written to disk */
   if (primeNumbers)
   {
      /* Display: Header */
      primeNumbers << "\t\tPrime Number List - 1 through 100\n\n";

      /* The numbers 1 through 100 are passed to isPrime, where the
         number is tested for primality, and if the number is prime,
         the number is written to "PrimeNumbers.txt" */
      for (int prime = 1; prime < 100; prime++)
      {       
         if (isPrime(prime))
         {
            primeNumbers << "" << prime << endl;
         }
      }
   }
   else
   {
      /* Display: Error message */
      cout << "\nError: The data could not be written to file. Please\n"
         << "make sure that the file or folder the file should be\n"
         << "written to is not write-protected or damaged. Close\n"
         << "the program and try again.\n";
   }

   /* Close: File "PrimeNumbers.txt" when the file was successfully
             written to disk */
   cout << "The data was successfully written to disk. The file will "
        <<  "now be closed.\n";
   primeNumbers.close();
}

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

   This function accepts integer values in the range from 1
   through 100 as arguments, tests them for primality, then
   returns true if the number is prime, 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
      conditions are 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 */
   for (int divisor = 2; divisor < primeNumber; divisor++)
   {
      if (primeNumber % divisor == 0)
      {
         return primeTest = false;
      }
   }

   /* Return: primeTest true if numbers are prime */
   return primeTest = true;
}

1 comment:

  1. how would I modify this code to meet these parameters I have almost the exact code for an assignment but I need make it prompt the user for a range between 1 and 100 and prompt the user to name the file?

    ReplyDelete