Programming Assignment #2 by ygq15756

VIEWS: 5 PAGES: 1

									Programming #5B: Prime Numbers & Functions
This programming assignment is a follow-up version of assignment #4B on prime
numbers. We want to wrap up the code we have in #4B to create separate functions to
deal with prime numbers. By doing so, we can readily call these functions for prime
number testing whenever we have such a need in our program. For example, in this
particular assignment, we are going to call these functions from your main function.
This makes our program conceptually clearer and more readable. After you finish this
assignment #5B, compare your code to what you have for #4B. You should see the
benefit of defining and calling functions in your program, instead of writing a flat
piece of code in a single main function to do everything.

First of all, declare the following function prototypes in the beginning of
your .cpp source code file (after the #include and the using namespace stuff)
        bool isPrime(int n) ;
        void printAllPrimesLessThan(int n) ;
        int getTheNthPrime(int n) ;

Second, define the three functions specifically:
(1) Define the prime-number-testing function: bool isPrime(int n) {…your code …}
To call this function, the caller needs to give an integer n of int type and the function
will return a value of bool type (i.e. either true or false). You can polish and reuse
certain part of the code of programming assignment #4B as the code for this function
so that it is going to return true if and only if the n is a prime number and it returns
false otherwise.
(2) Define the function void printAllPrimesLessThan(int n) {…your code …}
To call this function, the caller needs to give a positive integer n of int type, the
function is responsible for finding and printing out all the prime numbers less than n.
In your code for this function, you should call the isPrime function to help conduct
prime-number tests.
(3) Define the function int getTheNthPrime(int n) {…your code …}
To call this function, the caller needs to give a positive integer n of int type, the
function is responsible for returning the n-th prime number. Again, in your code for
this function, you should call the isPrime function to help conduct prime-number tests.

Finally write the main function to call and test the functions defined above:
Like the ATM service program you wrote for programming assignment 4A, in your
main function you should set up a loop to do the following things on each iteration of
the loop until the user chooses the quit option:
 display a menu showing information about four options: (i) testing whether a
    number n (to be given by the user) is a prime number, (ii) print all prime numbers
    less than a number n (to be given by the user), (iii) determine the n th prime
    number (n to be given by the user), and (iv) quit,
 prompt the user to type in a character to choose one of the options, and then
    prompt the user to provide a positive integer n, and
 according to what the user’s choice is, you should either call the isPrime function
    to determine whether n is a prime number, and display a message about this result,
    or call the printAllPrimesLessThan function to display the first n prime numbers,
    or call the getTheNthPrime function to determine the n-th prime number and
    display it, or quit the program after a tank-you message.

								
To top