, 3 min read

Simple Exercises for a C Programming Language Course

Original post is here eklausmeier.goip.de/blog/2014/05-10-simple-exercises-for-a-c-programming-language-course.

I sometimes teach a C programming language course. I use the following simple exercises for the students to solve on their own. Most solutions are no longer than 10-20 lines of C code.

  1. Exercising simple loop and printf(): Print a table of square root values for arguments 1 to 30.
  2. Exercising printf(), scanf(), arrays, if-statement, flag-handling: Read a list of numbers into an array, and then sort the array with Bubblesort.
  3. Print table of all combinations of two numbers between 1 and 10. In a similar vein, print all combinations of three numbers.
  4. Exercising loop and if-statement: Compute Collatz sequence until either the next computed element is one, or one has computed more than 10.000 elements: $\displaystyle{ a_{n+1} = \begin{cases} a_n/2, & \text{if }n\text{ is even,}\\ 3a_n+1, & \text{if }n\text{ is uneven.}\end{cases} }$
  5. Exercising loops, file handling, pointers, and arrays: A trainer for vocabulary, i.e., ask the user for a word and check whether the word matches another word, usually in another language. Read the list of words from a file.
  6. Exercising file handling: Read a file and check whether parenthesis and curly braces in the file are balanced.
  7. Encrypt a file with symmetric encryption method using XOR.
  8. Compute an approximation for $\pi$ using the formula $\displaystyle{ \pi = 4 \times \sum_{n=0}^\infty {(-1)^n\over 2n+1} =4\times\left(1 - {1\over3} + {1\over5} - {1\over7} \pm\cdots \right) }$
  9. Excercising loop statements: Compute an approximation for Euler's number using $\displaystyle{ e = \lim_{n\to\infty}\left(1 + {1\over n}\right)^n }$
  10. Compute an approximation for Euler's number using $\displaystyle{ e = \sum_{n=0}^\infty {1\over n!} = {1\over0!} + {1\over1!} + {1\over2!} + {1\over3!} + \cdots }$
  11. Exercising nested loops: Compute prime numbers in a range, for example between 1001 and 2001.
  12. Exercising do/while: Compute greatest common divisor of two numbers using Euclid's method. Alternatively one can test for divisibility for each number from one of the numbers stepping down by one.
  13. Print int as bit-sequence, or more generally convert from decimal system to an arbitrary (positive) number system.
  14. Exercising recursion: Read text file with possible macro statement #include, which includes the file. Included file may again contain #include statement.
  15. Exercising function pointers with Newton's method for finding a zero of a scalar function: $\displaystyle{ x_{n+1} = x_n - {f(x_n)\over f'(x_n)} }$

On GitHub I have collected some of the pupil's solutions to above exercises. Beware, I also teach the goto-statement, which is considered evil in some corners. So a number of solutions of the exercises make use of the goto-statement. Please read Donald E. Knuth: Structured Programming with go to Statements, also here. See also An Empirical Study of goto in C Code.