https://github.com/ImperialCollegeLondon/RCDS-intro-cpp
This course provides an introduction to the C++ programming language. The topics covered will include basic concepts on data science such as variable types, loops, conditional statements, arrays and vectors. The aim of the course is to provide strong foundations at the mathematical and theoretical level, while providing practical exercises to work on real data.
The course is organized in six chapters, covering the topics listed below. It will involve practical sessions and hands-on coding, which you can do both in your computer or in the codespaces provided by the course github. No prior experience on programming is required for the attendance of this course.
Instructions are in the appendix.
- Introduction to Linux operative systems, terminal and bash.
- Programing languages and performance comparison. Text editors.
- First steps with C++. Hello world.
- Variable types in C++, input / output statements.
- Basic mathematical operations.
- Compiling in parallel. Makefile.
- Conditional statements if / else.
- Loops for, while, do while.
- Data file manipulation. Reading and writing files.
- Arrays and vectors. Basic algebra.
- Functions in C++.
- Compiling functions and header files.
- Pointers and memory allocation.
- Classes and OOP.
- Basic statistics. Computing momenta of a distribution.
- Real algebra: scalar product, matrix-vector product
- Armadillo library
- Real RCDS problems: Rectilinear movement, estimate Higgs mass, numerical integration.
Follow the instructions below, depending on which type of machine you will use.
We are going to be writing our code in a text editor. This is a program which lets us produce plain text files. There are lots of choices available, but we recommend downloading VSCode.
A compiler is a program which takes the text file we have written and turns it into something we can run. Compilers do not look like normal programs, with windows and graphics. Instead they are run using the command line. We will discuss this in class but for now, follow the instructions below, depending on which type of machine you will use.
A C++ compiler should be installed by default on macOS and most Linux distributions.
- Open Terminal.
- Type
c++
and press Enter. - If it says
clang: error: no input files
orc++: fatal error: no input files
, then you are ready to go. - For Mac, if you don't see either of the error messages in 3. run
xcode-select --install
in terminal and try again.
First let's check that you haven't already got a C++ compiler on your computer. If you have installed Fortran in the past, you might already have a C++ compiler.
- Open Command Prompt from the Start menu (right-click the Start button and select Command Prompt).
- Type
g++
and press Enter. - If you get the error
g++: fatal error: no input files
, then you are good to go. If you get a different message, you don't have a C++ compiler installed and should continue following these instructions. - To install the compiler, follow the instructions on this YouTube video. Alternitavely you can install the windows subsystem for linux (WSL) and install a linux distribution from the Microsoft store. This is a bit more complicated and larger but will give you a linux environment on your windows machine with a C++ compiler pre-installed (I will be using WSL2 Ubuntu throughout the course). Is this awkward? Yes. Is it worth it? Yes!
- Check that it works by following steps 1--3 above.
Timestamps are in the descriptions and correspond to the numbering in this document. Content in the videos may differ slightly.
- Part 0: Pre-course instructions
- Part 1. You must have C++ set up on your machine before starting.
- Part 2
- Part 3
- Playlist
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International Licence.