Programming in C++

Exercise Sheet 1
==> Instructions for using C++ in the labs <==

The purpose of this lab is to get familiarised with creating simple programs and projects in C++.

  1. Get the file hello.cc containing the “hello world” program from the lecture. (Our C++ source files will always end in “.cc”; other systems use different conventions.)

    Now copy-paste it into a new project, compile it, and execute it. Or, if you have a bash terminal (Linux/Mac/Windows with WSL + Ubuntu), type (copy-paste actually, to ensure there are no typos!) the following:

        export CXXFLAGS='-g -std=c++20 -pedantic -Wall -Wpointer-arith -Wwrite-strings -Wcast-qual -Wcast-align -Wformat-security -Wformat-nonliteral -Wmissing-format-attribute -Winline -funsigned-char'
        echo "export CXXFLAGS='-g -std=c++20 -pedantic -Wall -Wpointer-arith -Wwrite-strings -Wcast-qual -Wcast-align -Wformat-security -Wformat-nonliteral -Wmissing-format-attribute -Winline -funsigned-char'" >> ~/.bashrc

    Note: the first line defines CXXFLAGS (flags to pass to the C++ compiler) for the current terminal session, while the second one stores these settings inside your terminal initialisation file (called "~/.bashrc"), so that you don't have to type them again.
    These flags are for the GNU C++ compiler (g++).

  2. Compile the program with the command
        make hello

    This will create an executable file called hello, which you can run by typing

        ./hello

    On lab Windows PCs, make is called "mingw32-make" (see the instructions for using C++ in the labs) and of course we'd need to use a backslash instead of a slash when running hello, since Windows uses backslashes to separate folders on a path instead of slashes that Unix uses:

        .\hello

    If you make any changes, you’ll need to type “make hello” again before running the new version.

  3. Download this program add0.cc
    1. Try compiling it. Change it just enough that it compiles without errors first.
      There should be *loads* of them - if only a few, you haven't done the "export CXXFLAGS ..." step at the top of the page!
    2. Now, write the code inside add_by_1 that adds a and b by incrementing b times a by one.
    3. Is your code correct? Which values should you try to test it?
    4. Is your code well-designed? Can you re-structure it to improve it? (avoid almost duplicate code, avoid doing exactly the same thing many times when doing it once would have been enough, etc.)
    5. Can you implement a function mult(a, b) that takes two integers and returns their product, which it computes using add_by_1? So mult(4, 3) should end up calling add_by_1 2 times (since 4 * 3 = 4 + 4 + 4).
  4. Do the same with the words.cc program. To run it reading input from a file, say
        ./words <file

    where file is some text file; words.cc itself will do.

    Note that to terminate input that you're typing yourself at the terminal, you need to type the End-of-File (EOF) character on a single line at the terminal.
    On Windows terminal, that's Control-Z. On Linux/Mac/Windows + WSL + Ubuntu that's Control-D.

  5. Now alter the words program to just print the count of words in the input (followed by a newline).
  6. Write a program using getline from the lectures to count the number of lines in the input.
  7. Make a copy of that program, and modify the copy to print out the longest line of the input.
  8. Make a copy of that program, and modify the copy to store the lines of the input in a vector, and then print them out in reverse order.