Previous Contents Next

Exercises

Resolution of Linear Systems

This exercise revisits the resolution of linear systems presented as an exercise in the chapter on imperative programming (3).
  1. By using the Printf module, write a function print_system that aligns the columns of the system.

  2. Test this function on the examples given on page ??.

Search for Prime Numbers

The Sieve of Eratosthenes is an easily programmed algorithm that searches for prime numbers in a range of integers, given that the lower limit is a prime number. The method is:
  1. Enumerate, in a list, all the values on the range.
  2. Remove from the list all the values that are multiples of the first element.
  3. Remove this first element from the list, and keep it as a prime.
  4. Restart at step 2 as long as the list is not empty.
Here are the steps to create a program that implements this algorithm:
  1. Write a function range that builds a range of integers represented in the form of a list.

  2. Write a function eras that calculates the prime numbers on a range of integers starting with 2, according to the algorithm of the Sieve of Eratosthenes.

    Write a function era_go that takes an integer and returns a list of all the prime numbers smaller than this integer.

  3. We want to write an executable primes that one will launch by typing the command primes n, where n is an integer. This executable will print the prime numbers smaller than n. For this we must use the Sys module and check whether a parameter was passed.

Displaying Bitmaps

Bitmaps saved as color array array are bulky. Since 24 bits of color are rarely used, it is possible to encode a bitmap in less space. For this we will analyze the number of colors in a bitmap. If the number is small (for example less than 256) we can encode each pixel in 1 byte, representing the number of the color in the table of colors of this bitmap.

  1. Write a function analyze_colors exploring a value of type color array array and that returns a list of all the colors found in this image.

  2. From this list, construct a palette. We will take a vector of colors. The index in the table will correspond to the order of the color, and the contents are the color itself. Write the function find_index that returns the index of a value stored in the array.

  3. From this table, write a conversion function, encode, that goes from a color array array to a string. Each pixel is thus represented by a character.

  4. Define a type image_tdc comprising a table that matches colors to a vector of strings, allowing the encoding of a bitmap (or color array) using a smaller method.

  5. Write the function to_image_tdc to convert a color array array to this type.

  6. Write the function save_image_tdc to save the values to a file.

  7. Compare the size of the file obtained with the saved version of an equivalent palette.

  8. Write the function from_image_tdc to do the reverse conversion.

  9. Use it to display an image saved in a file. The file will be in the form of a value of type bitmap_tdc.

Previous Contents Next