oxford_logo Dunn_School

DTC Introduction to Programming

Practice Problems for Array Structures

  1. Download the C example code from the lecture. Open a terminal (Ctrl + Alt + T on Linux systems), navigate to the directory with the zipped folder, and unzip it by running "unzip dtc_arrays_code.zip". It will extract two files: fib.c and pointers.c.
  2. A geometric sequence is a sequence of numbers where each new number is obtained by multiplying the previous one by a fixed number x. For example, starting from 2 and letting x = 3, the first 10 numbers of this geometric sequence are 2, 6, 18, 54, 162, 486, 1458, 4374, 13122. Open fib.c and modify it so that it prints the first 15 numbers of a geometric sequence, starting from 5, where x = 2. (Solution)
  3. In the lecture, I talked about the importance of preallocating memory for arrays. Here's code where memory is properly allocated and an example of bad practice where memory is reallocated with every loop iteration. Download each of these. Then compile and run them (instructions for doing so are in the code comments). The output is the code runtime (in seconds). Which was faster? On my machine, there was about a 4x speed increase for preallocating the memory.
  4. In bioinformatics, the Hamming distance provides a notion of similarity between two strings of equal length. The Hamming distance is equal to the number of positions where the strings differ. For example, if we have strings 'AGTC' and 'ACCC', then the Hamming distance between these two strings is equal to 2 because they differ at two positions (namely, the middle two positions). For the purpose of this exercise, we'll treat strings as arrays. Choose your favourite language, and write something that calculates the Hamming distance between [A, T, G, G, T] and [A, T, G, C, C]. (Hint: If you're using C, you may find this useful.) (Solution in C, Solution in Python)