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.
- Exercising simple loop and
printf(): Print a table of square root values for arguments 1 to 30.
if-statement, flag-handling: Read a list of numbers into an array, and then sort the array with Bubblesort.
- Print table of all combinations of two numbers between 1 and 10. In a similar vein, print all combinations of three numbers.
- 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:
- 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.
- Exercising file handling: Read a file and check whether parenthesis and curly braces in the file are balanced.
- Encrypt a file with symmetric encryption method using XOR.
- Compute an approximation for using the formula
- Excercising loop statements: Compute an approximation for Euler’s number using
- Compute an approximation for Euler’s number using
- Exercising nested loops: Compute prime numbers in a range, for example between 1001 and 2001.
- 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.
intas bit-sequence, or more generally convert from decimal system to an arbitrary (positive) number system.
- Exercising recursion: Read text file with possible macro statement
#include, which includes the file. Included file may again contain
- Exercising function pointers with Newton’s method for finding a zero of a scalar function:
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. See also An Empirical Study of goto in C Code.