Computer programming - Lab 1

Compiling your program

Compiling and running are two distinct steps.
The compiler converts the source file (.c) to an executable file.
Command:  gcc  options   file.c
You should use these options (in any order) To run your program, use the command
./a.out
where . (dot) means the current directory (where you compiled), a.out is the executable file name (replace as needed) and / is the directory separator character under Linux.

Simple functions with decisions

1. Write a function that takes three unsigned parameters and returns 1 if they can be the sides of a triangle and 0 otherwise.

2. A clerk works in an office, with a lunch break between 12:00 and 13:00. A client comes with a task that takes two hours to solve. Write a function that returns the time when the client can leave the office, given the time when he arrives (an integer, i.e., on the hour).

3. Write a function that returns the median of three numbers (i.e., the middle one, when ordering the numbers by value).

Integers, reals, rounding

4. Write a function that rounds its floating point argument to two decimal places (i.e., to the closest real number with two decimal places, away from zero). Example: 13.34295 is rounded to 13.34, whereas -3.435 is rounded to -3.44. Choose from the following functions to use: ceil, floor, nearbyint, rint, round.

Recursion

5. Write a function that computes xn by repeated squaring, i.e., if the exponent is even, we compute e.g. x6 = (x2)3, which leaves us with the simpler problem of computing the 3rd power (in the same way); if the exponent is odd, we do likewise, and multiply again with the base: x7 = (x2)3*x . Write the function, first for unsigned, and then for integer exponent.

6. A really inefficient way of computing Fibonacci numbers is by using the recurrence directly as given: F0 = 0, F1 = 1, Fn = Fn-1 + Fn-2. Figure out how many calls are needed to compute Fn. Write a recursive function that computes this number efficiently.

7. A simple fractal figure can be produced from a square as follows: if the side of the square is small (≤ 2), draw the square. Else, split the square in 3x3 squares, do nothing for the four corner squares, and draw recursively such a figure for the remaining five squares.

Marius Minea
Last modified: Sun Sep 29 0:30:00 EEST 2013