Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jan 16th, 2013  |  syntax: None  |  size: 4.68 KB  |  hits: 28  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Assignment #2 50 Points
  2.         Use only the instructions of chapters 1-8, i.e. absolutely no recursion
  3. For this assignment
  4. A prime number is a positive integer divisible only by itself and 1. There are several ways to find prime numbers, and also to determine whether or not a number is prime. One method to determine whether or not a positive integer N is prime is to simply determine if there is any integer K <= N / 2 such that N % K == 0. In such a case, N is not prime. Unfortunately this means that you must divide N by up to N/2 different values. A more efficient method relies on two facts. First, the smallest divisor K such that K % N == 0, meaning N is not prime, must also be a prime number. Second, for such a value K, it must be <= square_root(N).
  5.  
  6. In this problem, we will build a class called PrimeNumberTester. While the major function of an object of this class is to determine whether or not a number is prime, such an object will also create and hold an array of prime numbers which will be used to determine if a number is prime. In addition to using a class to solve this problem, you will also be expected to separate class specification from the implementation and client code (see page 424 of the text). In other words, you should have a file PrimeNumberTester.h which holds the prototypes and attributes for the class; a file PrimeNumberTester.cpp which holds the method definitions (implementation) for the class, and a main.cpp file which is the driver program. Your main() program should execute as follows:
  7. In a while loop, ask the user to input an integer 1 < MAX <=4000 or -1, where MAX is the maximum number of primes to be created. Remember, this is not the maximum prime number to find but the number of primes to find. This is the list of prime numbers that need to be used in determining whether or not a number is prime. In the PrimeNumberTester class (PNT) there will be a member attribute called primeNumberList that holds the first MAX prime numbers. If -1 is input, the program should terminate. Thus, when main() starts and reads a value for MAX, in creating an object of class primeNumberTester it will initialize the array primeNumberList with the first MAX prime numbers. If the input value is not legal, i.e < -1 or > 4000, remain in the loop until a legal value is input. You do not need to consider if the value input is too large as an integer itself.
  8.  
  9. Note: Since we are only concerned with up to the first 4,000 primes, we can use an integer array to store the primes.
  10. Assuming that the terminate option has not been input. Main() should next, in an inner while loop, ask the user to input a -1, 0, or 1:
  11. (-1) terminate the inner most while loop. This would cause the program to move “up” to the containing outermost while loop, destroy (call its destructor) the current object, and again ask the user to input a new MAX value.
  12. If a 0 is input, the program should output the list of primes currently stored in primeNumnberList, and then again ask for an input of -1, 0, or 1.
  13. If a 1 is input, the program should ask the user to input an integer (N) which is to be tested to determine whether or not it is prime. The value N must be >1. Using N and the primeNumberList, the program should use the just described algorithm to determine whether or not N is prime.  If N is prime, the program should output that it is prime. If N is not prime, the program should output the value of the smallest prime divisor. In the case in which sqrt(N) is larger than the largest prime in primeNumberList, the program should still test the first MAX primes as divisors. In this case, if a divisor is not found it should output that the test failed but sqrt(N) was > MAX and so the search was terminated early, and thus not conclusive.
  14. The following is an example run of the program. It does not thoroughly test the program so beware.
  15. User input is in RED.
  16.  
  17. Welcome to the Prime Number Checker Program
  18. Input the maximum number of primes for the prime number table
  19.     (must be > 1 and <= 4,000) or -1 to quit: 5
  20. Creating table of the first 5 prime numbers
  21. Table created
  22.  
  23. Input:
  24. -1 to create a new prime number table or quit
  25.  0 to output current table of prime numbers
  26.  1 to determine if a number is prime: 1
  27.  
  28. Input a value to check for prime (> 1): 29
  29. 29 is prime
  30.  
  31. Input:
  32. -1 to create a new prime number table or quit
  33.  0 to output current table of prime numbers
  34.  1 to determine if a number is prime: 0
  35.  
  36. The list of primes in the prime number table is as follows:
  37. 2
  38. 3
  39. 5
  40. 7
  41. 11
  42.  
  43. Input:
  44. -1 to create a new prime number table or quit
  45.  0 to output current table of prime numbers
  46.  1 to determine if a number is prime: 149
  47.  
  48. No primer divisor found up to MAX of 11. The value 149 may or may not be prime.
  49.  
  50. Etc………