File Byte Reader in C

Writing code to read or write text files can be tricky because the exact contents of a file cannot be viewed in a text editor if the file contains non-printable characters such as line feeds or carriage returns. This simple utility program will take a filename as a command line argument and print out its exact contents, including descriptions of any non-printable or whitespace characters.

Continue reading

Complex Numbers in C

In this post I will just give an overview of the C programming language's support for complex numbers.

C99 introduced a new addition to the standard library to support complex numbers. The area of complex numbers is a vast one so I will just give an overview of the topic in C, as well as writing a function to draw an Argand diagram using SVG.

Continue reading

Selection Sort in C

There are many sorting algorithms, often with variations and optimizations, and every now and again I will be coding some of them for this site.

A while ago I wrote a post on bubble sort and here is a follow up on another sorting algorithm called selection sort. As with bubble sort it's not particularly efficient but it is simple to understand and implement.

Continue reading

One-Dimensional Cellular Automaton in JavaScript

For this post I will write a simple implementation of a 1-dimensional cellular automaton in JavaScript. The concept of cellular automata has existed since the middle of the 20th century and has grown into a vast field with many practical and theoretical applications.

A cellular automaton consists of any number of "cells" arranged in 1, 2, 3 or more dimensions. Each has a state associated with it (in the simplest case just on or off) and each cell and therefore the entire automaton transitions from one state to the next over time according to a rule or set of rules.

The number of dimensions, the number of possible cell states, and the rules can become arbitrarily large and complex but for this project I will implement the simplest type of one-dimensional cellular automaton, known as an elementary cellular automaton.

Continue reading

Using Valgrind to Detect Memory Management Problems in C

The C language has a reputation for being difficult to learn and to code in. I think this is unfair as it is actually a very small and simple language, but most of the perceived difficulty with using C comes from its memory management, or rather lack of. Only the smallest and simplest programs can get away with using auto variables: sooner or later you are going to have to use dynamic memory, opening yourself up to an extensive range of tricky bugs. There's no foolproof way to get round this, but you can catch most bugs before they wreak havoc in production with a brilliant little program called Valgrind.

Continue reading

Allocating Dynamic Memory in Large Blocks with C

When creating a dynamic data structure it is tempting to allocate just the amount of extra memory you need to add each item. However, malloc, realloc or calloc are quite expensive in terms of resources so for situations where many items are likely to be added it is more efficient to allocate a large block of memory in one go. This can be gradually used up, and then another block allocated when necessary. In this post I will write a simple demonstration of this principle, complete with monitoring code to verify that we are in fact improving efficiency.

Continue reading

Plotting Taylor Series Sine Waves in C

Previous posts have included an SVG library, memoization of factorials and Taylor Polynomials. In this post I will bring these all together to plot various sine waves created using Taylor Polynomials.

Very briefly, Taylor Polynomials are used to approximate functions, in this case sine and cosine, to any level of accuracy. We can plot these levels to show how they become increasingly accurate, which is the subject of this post.

Continue reading