Comp 1671, Autumn 2002
Programming Project 2
Assigned October 4, 2002
Due Friday, October 18, 2002

These are the instructions for your third programming project. Recall that all code must be written individually and independently.

The purpose of this program is to make a list of prime numbers. Your program should repeat the following indefinitely: Ask the user to input an integer greater than or equal to 2. If they input an invalid number, then re-prompt them for input until they enter something valid. Next print out a comma-separated list of all of the primes less than or equal to the upper-limit they typed in. Output 10 primes per line. Then, on the next line, print out the total number of primes that were found between 2 and their number. Lastly, ask them whether they want to quit or continue the program.

Here is a sample program output:

ftl> proj3
This program will print out all of the primes between 2
and an upper limit set by you.

How high do you want to go? 10

These are the primes less than or equal to 10:
2, 3, 5, 7
The total number of primes less than or equal to 10 is 4.

Would you like to continue? y
How high do you want to go? 200

These are the primes less than or equal to 200:
2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
31, 37, 41, 43, 47, 53, 59, 61, 67, 71,
73, 79, 83, 89, 97, 101, 103, 107, 109, 113,
127, 131, 137, 139, 149, 151, 157, 163, 167, 173,
179, 181, 191, 193, 197, 199
The total number of primes less than or equal to 200 is 46.

Would you like to continue? Y
How high do you want to go? -500
Invalid input, try again
How high do you want to go? 500

These are the primes less than or equal to 500:
2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
31, 37, 41, 43, 47, 53, 59, 61, 67, 71,
73, 79, 83, 89, 97, 101, 103, 107, 109, 113,
127, 131, 137, 139, 149, 151, 157, 163, 167, 173,
179, 181, 191, 193, 197, 199, 211, 223, 227, 229,
233, 239, 241, 251, 257, 263, 269, 271, 277, 281,
283, 293, 307, 311, 313, 317, 331, 337, 347, 349,
353, 359, 367, 373, 379, 383, 389, 397, 401, 409,
419, 421, 431, 433, 439, 443, 449, 457, 461, 463,
467, 479, 487, 491, 499
The total number of primes less than or equal to 500 is 95.

Would you like to continue? n
ftl>

Instructions for submitting:
As always, don't forget that your program should start with a few lines of comments giving your name, the date, the course this is for, and the assignment number. You should also include a few lines that describes what the program does and how to use it. The program should be written with a well organized structure, and the variables should be named in a useful way. The program should be formatted consistently and clearly, and should include comments that explain the code. The program should compile without any compiler warnings. When you're done writing and testing the program, print out a copy of your source code. Also print out a copy of the output of your program, which shows how the program works for several sets of input data. Bring these to class on the due date and turn them in at the beginning of class. Also, before the beginning of class on the due date, mail your source code (your ".cpp" file) to comp1671@cs.du.edu. Do not include it as the message, instead make it an attachment to the email.

If you need further challenge:
The most naive (and slowest) method of testing if a number n is prime is to simply try all possible divisors and show that none of them divide n. A better method was devised by Eratosthenes (275-195 BC). This method is also pretty simply, but requires you to use arrays or vectors (you'll have to read ahead or come see me). Modify your program so it uses this method (called the sieve of Eratosthenes). You can read about it, for example, on the cool website http://www.math.utah.edu/~alfeld/Eratosthenes.html. This web site has a graphic demonstration so you can really see how the algorithm works.