Вы находитесь на странице: 1из 2

Program 3.

45
(a) I used a recursive function compute_factorial to

compute the factorial, every time call the function will return

n*(n-1) until n=1 and return the result.

Because of the datatype integer has maximum 2147483647, so


when n>=17 the result will overflow.

(b) To estimates the value of the mathematical constant e I

write a function estimates_e that return double, input x=1

for calculate e^1 and in the function compute_factorial has

been called again without rewriting the complex compution

to calculate the factorial again.

Also, the result depends on the input n so if n is small the

estimated value would have relative error.

(c) The method like (b) but have additional input to compute

e^x.

I encapsulate each calculation into different function, some

calculation in the function will be called if needed. The suggest

n for estimating the mathematical constant e is from range 10

to 16, if x the greater the error is also greater.


Program 3.47
I build a struct Day to store the month, day, year and declare

birth and current standing for the birthday and current day.

Two variables age and maxrate store age and the maximum

heart rate. The calculation only need one statement so I dont

put them into another function. The target-heart-rate range

only uses once so it isnt stored.

Program 3.48
I define two variables encrypt and decrypt for cryptography

method to make the program clear. If encrypt, then add 7 to

each digit. If decrypt, then add 3 to each digit. Parameter a, b,

c, d store four digit respectively. When return value, swap the

digit.

Вам также может понравиться