Computer programming - Lab 4

1. Function root Write a recursive function that computes the root of a given continuous function f in an interval [a, b] using the bisection method, assuming f(a) and f(b) have opposite signs. Compute f in the midpoint of the interval, and continue the process for the subinterval for which f changes sign.

2. Wordcount Implement the classic wordcount program which counts characters, words and lines in a text read until end of input. Words are sequences of characters other than whitespace. Only complete lines are counted. Check your answer comparing with the Unix wc program.

3. C strings Write a program that prints out and computes the total length of all strings in a given text. Strings are delimited by quotes ". Inside strings, a backslash and the next character are considered to represent a single special character, in particular \" represents a quote.

4. C comments and strings Write a program that filters both styles of C comments from a text given as input, and also handles strings (see previous problem) correctly, i.e., comments cannot start in a string, neither can strings start in a comment.

5. Floating-point numbers Write a function that reads a floating-point number in C syntax from input and returns its value (a double). A floating-point number has optional sign (+/-), optional integer part, optional fractional part (with period), and optional base-10 exponent (letter E/e followed by integer with optional sign). If there is a decimal point, at least one of integer or fractional part have to be present. If there is no decimal point, integer part and exponent part have to be present.

6. Phone numbers Write a program that finds and prints phone numbers in a text read from input. A phone number is a sequence of the form (prefix)number, where the prefix has 4 digits, starting with 0, and the number has 6 digits, e.g. (0723)123456 .

7. Fixed-width printing Write a function that takes an unsigned n as parameter, and prints a text read until end of input, fitting n characters per line. Words (sequences of non-whitespace chars) are printed with one space in between. A word that would exceed the line limit will be split inserting a hyphen - as last character on the line (no extra hyphen is needed if the last character that fits is a hyphen itself). Newlines in the original text are observed.
Hint: to handle the end of the line, write a function peek() which returns the next character without consuming it (use ungetc()).

8. Fibonacci strings. Fibonacci strings are defined as follows: S0 = 0, S1 = 01, Sn = Sn-1Sn-2 (concatenation).
Write a function that takes an unsigned n and prints Sn.

9. Transforming numbers Let f(x) = x2 + x + 12. We define the following transformation on numbers: T(n) is obtained by taking every digit d in the base-10 representation of n and replacing it with f(d) (concatenating the resulting digits).
Write a function that takes two unsigned numbers n and k and that prints the digits of the number Tk(n) = T(T(...(T(n)))) (T applied k times).

10. Prettyprinting Write a program that properly indents a text read from standard input that has balanced braces { } . The rules are as follows:

11. Reverse definitions Given a sequence of definitions of the form var1 = num1 var2 = num2 ... varn = numn (read until end of input), where vari are single lowercase letters, and numi are natural numbers, print the definitions in reverse order.

Marius Minea
Last modified: Thu Oct 16 15:05:00 EEST 2014