Computer Programming (I)

Practice Problems

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.

8.     劉邦又來借兵，這次的人數條件除了兩兩數之餘一、三三數之餘二、五五數之餘三、七七數之餘五、九九數之餘八外，還必須不能是一個prime number。寫程式印出韓信可借出的最少士兵人數。