Linear Regression in C

In a previous post I implemented the Pearson Correlation Coefficient, a measure of how much one variable depends on another. The three sets of bivariate data I used for testing and demonstration are shown again below, along with their corresponding scatterplots. As you can see these scatterplots now have lines of best fit added, their gradients and heights being calculated using least-squares regression which is the subject of this article.

Continue reading

Using the C Library’s qsort Function

I recently wrote an article on Bubble Sort, more as an academic exercise than a piece of practical and usable code. At the bottom of the post I suggested that the C library's built-in qsort (Quicksort) function was the best option for sorting arrays in most situations.

However, there is more to using qsort than just throwing an array at a function: you need to provide your own comparator function, the implementation of which can be slightly fiddly if you are sorting anything other than primitive data types. In this article I'll start off with a simple int-sorting example, and then go on to sort an array of structs, firstly by an int and then by a string.

Continue reading

An Introduction to ncurses in C

The ncurses library provides a range of functionalities for writing console applications which go beyond simply printing text, but without the complexity of writing a full GUI application. To me the most useful things ncurses provides is moving the cursor (and therefore print position) around the console, and printing with various text and background colours.

In this post I will introduce those two abilities but ncurses can do a lot more. If you want to look into the topic further I recommend Dan Gookin's book on the subject.

Continue reading