3 - Control Flow
Exercise 3-1
Our binary search makes two tests inside the loop, when one would suffice (at the price of more tests outside). Write a version with only one test inside the loop and measure the difference in run-time. [1]
My solution
tbd
Official solution
tbd
Exercise 3-2
Write a function
escape(s, t)
that converts characters like newline and tab into visible escape sequences like\n
and\t
as it copies the stringt
tos
. Use aswitch
. Write a function for the other direction as well, converting escape sequences into the real characters. [1]
My solution
tbd
Official solution
tbd
Exercise 3-3
Write a function
expand(s1, s2)
that expands shorthand notations likea-z
in the strings1
into the equivalent complete listabc ... xyz
ins2
. Allow for letters of either case and digits, and be prepared to handle cases likea-b-c
anda-z0-9
and-a-z
. Arrange that a leading or trailing-
is taken literally. [1]
My solution
tbd
Official solution
tbd
Exercise 3-4
In a two's complement number representation, our version of i toa does not handle the largest negative number, that is, the value of
n
equal to . Explain why not. Modify it to print that value correctly, regardless of the machine on which it runs. [1]
My solution
tbd
Official solution
tbd
Exercise 3-5
Write the function
itob(n, s, b)
that converts the integern
into a baseb
character representation in the strings
. In particular,itob(n, s, 16)
formatsn
as a hexadecimal integer ins
. [1]
My solution
tbd
Official solution
tbd
Exercise 3-6
Write a version of
itoa
that accepts three arguments instead of two. The third argument is a minimum field width; the converted number must be padded with blanks on the left if necessary to make it wide enough. [1]
My solution
tbd
Official solution
tbd