Computer Programming (I)

Programming Assignment 3 (1B & 1D)

Oct. 31, 2006

1.
Write a function called my_abs(int
i) that computes the absolute value of i.

2.
Write a function called my_fabs(double x) that computes
the absolute value of x.

3.
Write a function called my_pow(double
x, int i) that computes the
ith power of x (x的i次方)。Note that in order to make the problem easier, I
have changed the type of the second parameter from double to int.

4.
Write a function called my_ceil(double x) that computes
the ceiling of x. (You may assume that x is small
enough to fit in an int.)

5.
Write a function called my_floor(double x) that
computes the floor of x. (You may assume that x is
small enough to fit in an int.)

6.
Write a function roll_dice(int
n) that uses rand( ) to simulate the rolling of n dice and return the result.
For example, the return value of roll_dice(3) may be one of 3, 4, …, 18. The exact return value, of
course, depends on the random number generator.

7.
Use roll_dice( ) in (6)
to solve Problem 9 on p. 137 of Savitch, but skip the
loop part.

8.
Write a function called isPrime(int i) that return true if i is a prime number(質數),
false otherwise.

You need to write a main(
) to test these functions.