Star Catalog 1.0

This post covers the first part of a project to develop a star catalog in C. The aims of this initial stage are quite modest - we will write a program to import star data from a file into a data structure and then print it to the screen in a neat format.

Future plans for the project include calculating a few extra pieces of information from existing data, sorting and filtering the data, and exporting it to various other formats.

All these things are specific to stellar data but of course the principles and coding techniques can be applied to any field.

Continue reading
Posted in Uncategorized | Leave a comment

Pascal’s Triangle

The numbers in the graphic below form the first five rows of Pascal's Triangle. The first row consists of a single number 1. In subsequent rows, each of which is has one more number than the previous, values are calculated by adding the two numbers above left and above right. For the first and last values in each row we just take the single value above, therefore these are always 1.

Pascal's Triangle

Pascal's Triangle in its conventional centred layout

(If you do an image search for Pascal's Triangle you will find plenty more, most frequently in a honeycomb grid, sometimes animated, but always nicer than mine. I'm a software engineer, not a graphic designer!)

Continue reading
Posted in Uncategorized | Leave a comment

Object-Oriented Programming in C

C is not, of course, an object oriented language and does not even have any discernible features of one. The three core characteristics of object oriented programming are frequently stated to be encapsulation, inheritance and polymorphism, but a more fundamental characteristic is the combining of data (or properties) and the functions (or methods) which work on that data into a single entity which can be used to access both. This can be simulated in C by adding function pointers to a struct. The nuts and bolts of doing so are not as slick as in a real OOP language such as C++ and frankly look a bit clunky, but in this post I will write a short demonstration of the principle. Whether it is actually worthwhile is a moot point!

Continue reading

Posted in Uncategorized | 3 Comments

The Soundex Algorithm

Soundex is one of a number of phonetic algorithms, assigning values to words or names so that they can be compared for similarity of pronounciation. It is probably the best know such algorithm as it is built in to most major RDBMSs, as well as PHP and other languages.

It doesn’t take much thought to realise that the whole area of phonetic algorithms is a minefield, and Soundex itself is rather restricted in its usefulness. In fact, after writing this implementation I came to the conclusion that it is rather mediocre but at least coding it up does give a better understanding of how it works and therefore its usefulness and limitations.

Wikipedia has a surprisingly brief article on the topic Soundex on Wikipedia which you might like to read.

Continue reading

Posted in Uncategorized | Leave a comment

Creating a Static Library

In a number of previous posts I have used more than one source code file, one for the main function and others for functions used by main. This is purely to make the source code easier to handle than would be the case if all the code were in one large source file.

In this project I will go beyond separating source code into different files by compiling the additional files into static libraries which can be copied to a central location along with their header files and used by many programs, in exactly the same way as the standard library is used.

Continue reading

Posted in Uncategorized | Leave a comment

On Success and On Error Callbacks

This is a short post to demonstrate the use in C of a technique which is common in other languages – using callbacks as function parameters to tell the function what to do in certain circumstances. In this project I will write a function which will simulate retrieving data from a database and then call one of the two functions passed to it as pointers, the first if the data retrieval was successful and the second if there was an error.

Continue reading

Posted in Uncategorized | Leave a comment

Normal Distribution

One of the most useful bits of number-crunching you can do with data is to calculate the probability distribution, in the earnest hope that it will be a reasonable fit for one of the recognised distributions such as the normal distribution. In this project I will write a small C library to calculate the normal distribution for a given data set.

Continue reading
Posted in Uncategorized | Leave a comment

Calculating Great Circle Distances

The shortest distance between two locations on the surface of Earth (or any planet) is known as the Great Circle Distance. Except for relatively short distances these cannot be measured on a map due to the distortion and flattening necessary to represent a sphere on a flat plane. However, the calculation of the Great Circle Distance from two coordinates is simple, although I suspect generations of midshipmen might not have agreed. In this post I will write a short program in C to calculate the distance from London to various other cities round the world.

Continue reading
Posted in Uncategorized | Leave a comment

Graphing Data Using a Logarithmic Plot

The majority of data can easily be plotted on a graph with equal intervals on the axes, for example 1, 2, 3 or 100, 200, 300 etc.. Some data, typically that which increases or decreases exponentially, cannot comfortably be graphed on such a scale without squashing the data up so much at one end that it becomes incomprehensible. The solution to this problem is to use a logarithmic scale.

Continue reading
Posted in Uncategorized | Leave a comment

Now on Github

A very quick micro-post just to say that I have pushed the source code for all my posts to date to Github. The URL is

https://github.com/code-in-c

The source code for all future posts will also be on Github so feel free to follow me if you wish.

Posted in Uncategorized | Leave a comment