Vous êtes sur la page 1sur 3

School of Mathematics

Indian Institute of Science Education and Research Thiruvananthapuram


MAT3101/4101: Programming and Data Structures
Dr. K. R. Arun and Ms. Chitra M. T.
Assignment 4
1. The standard library <assert.h> contains a macro assert() that can be used to ensure
that the value of an expression is as expected. Here is an example of how assert() can
be used to ensure a variable n is positive, but less than or equal to 100:
#include <assert.h>
void f (int n) {
assert (n > 0 && n <= 100);
}
If an assertion fails, the system will print out a message and abort the programme. When
the macro NDEBUG is dened at the point where <assert.h> is included, e.g.
#define NDEBUG
#include <assert .h>
then all assertions are ignored. Thus assertions can be freely used during programme
development and later discarded (for run-time eciency) by dening NDEBUG. Write a
programme to generate Fibonacci numbers ensuring that they are positive using an
assertion.
2. Write a function having prototype:
int sumsq (int n, int m);
that returns the sum of squares n
2
+ (n + 1)
2
+ + (m 1)
2
+ m
2
for n < m. The
function should also be able to handle the case n > m.
Please Turn Over. . .
3. Write a function that outputs (e.g. the rst 1000 terms of) the sequence d
n
=

n

n 1 for n = 1, 2, 3, . . .. Write another function that computes d


n
in a mathematically
equivalent but numerically more robust way:
d
n
=
1

n +

n 1
for n = 1, 2, 3, . . . .
Compare the dierence in the outputs of the two functions. The second one should give
more accurate results since it avoids subtracting two nearly equal numbers and thus
reduces cancellation errors.
4. Write a function that takes three doubles a, b and c as input and computes the roots of
the quadratic equation
ax
2
+ bx + c = 0.
When a = 0 and b = 0 the equation has only one root x = c/b. When a = 0, its roots
are given by
x
1
=
_
b +

b
2
4ac
_
2a
, x
2
=
_
b

b
2
4ac
_
2a
.
If the discriminant

b
2
4ac is negative, the two roots are complex numbers. Output
complex numbers in the form of r + mi, where i =

1 and r and m are real and


imaginary parts. Write a main() programme to test your function.
5. Write a recursive function having the prototype:
long fibonacci (int n);
that takes an integer n and calculates the Fibonacci number f
n
. Also output the number
of recursions needed to obtain f
n
.
6. Write a function to print out a decimal integer v in base b having the prototype:
void print(int v, int b = 10); // b defaulted to be 10
with 2 b 16. Your programme should print out an error message when a user
attempts to input b outside this range. Hint: an integer v can be expressed in base b as
a
n
a
n1
a
1
a
0
if v = a
0
+ a
1
b + a
2
b
2
+ + a
n
b
n
with 0 a
i
< b, i = 0, 1, . . . , n and
a
n
= 0.
7. A man has just started a xed-rate mortgage of total amount of S euros with annual
percentage rate r and length of m months. But he decides to payo the mortgage early
and pay P
1
, P
2
, . . . , and P
n1
euros for the rst , second, . . ., and (n 1)th month,
respectively, and payo the mortgage at the end of the nth month with n m. The
payo amount P
n
can be computed from the formula:
S =
n1

i=1
P
i
_
1 +
r
12
_
i
+ P
n
_
1 +
r
12
_
n
,
Please Turn Over. . .
assuming that each P
i
is no smaller than his regular monthly payment so that no
penalty is imposed on him. Write a function that computes the payo amount P
n
,
given S, r, P
1
, P
2
, . . ., and P
n1
. If the man has a 15-year mortgage of 200000 euros at
rate 0.0625, but pays P
i
= 2000 + 10i euros in month i for i = 1, 2, . . . , 59 and wants
to payo the mortgage in month 60, what is his payo amount P
60
? Hint: Horners
algorithm should be used in calculating the summation.
8. The base of the natural algorithm e is
e = 2.7182818284590452353602874713526624977572 . . .
to 41 signicant digits. It is known that
x
n
=
_
1 +
1
n
_
n
e, as n .
Write a function that generates the sequence {x
n
} and prints out the error e x
n
. A
very slow (worse than linear) convergence should be observed for x
n
e. Indeed it can
be shown that
|e x
n+1
|
|e x
n
|
1, as n .
A computationally eective way to compute e to arbitrary precision is to use the innite
series:
e =

n=0
1
n!
= 2 +
1
2!
+
1
3!
+ .
To reduce roundo errors, smaller terms should be added rst in calculating partial
sums. It can be shown that

e
m

n=0
1
n!

<
e
(m + 1)!
.
Write another function to generate the partial sums for approximating e and compare
the convergence. Horners algorithm may be used to eciently compute this kind of
partial sum.
9. Compute the integral:
_
2
1
sin x
x
dx
using composite trapezium rule and Simpson rule with ve digit accuracy after the
decimal point.
End of Assignment

Vous aimerez peut-être aussi