The C Programming Language

 

References


Examples

Examples marked by the (advanced) symbol contain slightly more advanced material and will not be shown in class.

math_ops.c [html]

Performs arithmetic calculations on the input numbers.
               + Usage of the arithmetic operators.
               + Usage of the printf() and scanf() functions.

bit_ops.c [html(advanced)

Performs bitwise manipulations on the input numbers.
               + Usage of the bitwise operators.
               + Usage of more advanced flags and options in printf().

sign_pair.c [html]

Reads a integer and prints its sign and it parity (whether it is even or odd).
               + Usage of if - else blocks (including nested blocks).
               + Usage of the ? operator.

roman_dig.c [html]

Reads a Roman digit, and prints out its numerical value.
               + Usage of switch - case.

euclid.c [html]

Reads two integers and computes their greatest common divisor using Euclid's algorithm.
               + Usage of while loops.

power.c [html]

Reads two integers and computes x^y (x raised to the power of y).
               + Usage of for loops.

mul_tab.c [html]

Prints out the multiplication table.
               + Usage of nested for loops.

app_sqrt.c [html]

Approximate the square-root of the input number using the ancient Babylonian technique.
               + Usage of a do - while loop.

prime.c [html]

Reads an integer and determines whether it is prime.
               + Usage of break in a for loop.

grades_1.c [html]

Reads a sequence of grades (terminated by -1) and computes their average and variance.
               + Usage of break and continue in an infinite while loop.

grades_2.c [html(advanced)

Reads a sequence of grades (terminated by -1) and computes their average and variance. Aborts in case of a -2 input.
               + Usage of goto with labels (not recommended).

choose.c [html]

Reads two integers n and k, and computes (n over k), the number of different sub-sets of size k one can choose from a set of size n.
               + Usage of simple functions.

dist.c [html]

Reads two points and computes the Euclidean distance between them.
               + Usage of the sqrt() function (compile with -lm).

l_dist.c [html]

Reads two points and computes the distance between them using several l_k norms.
               + Usage of the fabs() and pow() functions (compile with -lm).

pow_tab.c [html]

Prints a power of tables.
               + Usage of two-dimensional arrays.
               + Usage of #define for defining constants.

cmp_prefix.c [html]

Read two input strings and find their longest common prefix.
               + Usage of strings.
               + Usage of the library functions strlen(), strncmp(), strncpy().

app_pi.c [html]

Computes an approximation to the value of pi.
               + Review: Efficient loops.

rep_alpha.c [html]

Replaced all the letters in a text line using a replacement table.
               + Review: Clever use of arrays.

lat2dec.c [html]

Converts a number in base 26 to its decimal value.
               + Review: Manipulation of characters.

stirling.c [html]

Checks Stirling's formula for evaluating ln(n!).
               + Usage of a tail recursion.

mod_power.c [html]

Reads three integers and computes (x^y) mod m.
               + More sophisticated use of recursive functions.

hanoi.c [html]

Solves the problem of the towers of Hanoi.
               + Usage of recursion.
               + Receiving numerical inputs as a string, using atoi().

quad_eq.c [html]

Computes the real-valued solutions of a quadratic equation.
               + Passing output variables to a function by their address.
               + Using the function's return value.

rev_str.c [html]

Reads a string an reverses it.
               + Passing strings to functions.
               + Usage of a non-standard for loop.

bubble.c [html]

Performs bubble-sort on an array of integers.
               + Passing arrays to functions.
               + Implementation of a (little) more sophisticated algorithm.

srch_str.c [html]

Reads two input strings and prints all the occurrences of the second string within the first one.
               + Usage of the strstr() function.
               + Using pointer arithmetic.

bin_srch.c [html]

Answers queries on a sorted array of random integers.
               + Usage of the rand() function.
               + Implementation of the binary search algorithm.

let_hist.c [html]

Reads an input file and prints out a histogram of all the letters it contains.
               + Usage of input files: fopen(), fgetc(), fclose().
               + Usage of command-line arguments.

conv_file.c [html]

Converts a file to upper (or to lower) case.
               + Usage of output files: fputc(), fprintf().

cipher.c [html(advanced)

Enciphers or deciphers a file using a cumulative xor formula.
               + Usage of binary input and output files.

grades_3.c [html]

Reads a sequence of grades and computes their average and standard deviation.
               + Usage of dynamic allocation of memory.
               + Some tricks using printf().

greeting.c [html]

Reads the user's first and last name and prints a greeting.
               + Usage of dynamic memory allocation for strings.
               + Usage of strcpy(), strcat() and sprintf().

cards_1.c [html]

Picks up five cards and prints them.
               + Usage of enumerated types.

dates_1.c [html]

Reads a date and prints the dates of the days before and after it.
               + Usage of structures.
               + Passing structures to functions by value.

cards_2.c [html]

Picks up five cards and prints them (improved version).
               + Usage of structures containing enumerated types.
               + Usage of an array of structures.
               + Usage of typedef to simplify the code.

dates_2.c [html]

Reads a date and prints the dates of the week after this date.
               + Usage of pointers to structures.

points.c [html]

Reads a series of input points, computes their centre of mass and locates the points closest and farthest from the centre.
               + Usage of dynamically allocated arrays of structures.

shapes.c [html(advanced)

Reads an input file called shapes.txt, analyzes it and computes the area of each shape it specifies.
               + Usage of the fgets() and fscanf() functions.

obs_list.c [html]

Reads a sequence of observations (real numbers) and prints the deviation of each observation from the mean.
               + Implementation of a singly-linked list.

list.c [html]
list.h [html]

A package for maintaining doubly-linked lists of real numbers.
               + Representation of a common data structure.
               + Iteration on a linked list.

test_list.c [html]

Tests the functionality of the doubly-linked list package (compile with list.c).
               + An example of a test module.

merge_sort.c [html]

Sorts a list of numbers using the merge-sort algorithm.
               + Usage of linked lists.
               + Usage of a recursive algorithm.

sort_city.c [html(advanced)

Read an input file called cities.txt containing city entries, and prints the cities sorted by their name and by population.
               + Usage of pointers to functions.
               + Usage of the qsort() function.

tree.c [html(advanced)
tree.h [html]

A package for generic binary trees.
               + Representation of a slightly more intricate data structure.
               + Usage of void pointers and pointers to functions.

test_tree.c [html(advanced)

Constructs a tree of random integers and lets the user execute interactive commands on it (compile with tree.c).
               + Usage of a generic data structure.
               + Implementation of a simple interactive program.

word_cnt.c [html(advanced)

Reads an input file and counts the number of occurrences of the words in it (compile with tree.c).
               + Usage of structures with a generic data type.
               + Parsing text (dividing to words).


 

Appendices