Computer Programming (I)
Nov. 15, 2007 (Week 10)
1. Write a program to compute and print out the sum of,, …, .
2. Write a program to compute and print out the average of,, …, .
3. Write a function roll_dice(int n) that uses rand( ) to simulate the rolling of n dice and returns the result. For example, the return value of roll_dice(3) may be one of 3, 4, …, or 18. The exact return value, of course, depends on the random number generator. Write a main program to test your function.
4. Once you believe your roll_dice( ) works correctly, use it to solve Problem 9 (The craps game simulation problem) on p. 130 of Savitch’s book. You can skip the loop part.
5. Write a function called isPrime(int n) that returns true if n is a prime number, false otherwise. Write a main program to test your function.
6. Once you believe your isPrime()works correctly, use it to write a program that prints out all prime numbers between 1000 and 3000.
7. A twin prime pair is a pair of prime numbers that differ by exactly two. Examples of twin prime pairs are (3, 5), (5, 7), (11, 13), and (29, 31). Use isPrime() to compute and print out all twin prime pairs smaller than 1000.