Lab 1. Basics of Unix. Small C++ programs

Lab 1. Basics of Unix. Small C++ programs

The Unix commands you need to know

You need to know just a few commands to work comfortably in a Unix terminal: ls, cd, less, mkdir, cp, mv, rm.

A brief summary:

ls   list files in the current directory
ls path/to/a/directory   list files in the directory
cd path/to/a/directory   change directory
cd ../   go to the parent directory

pwd   print the current working directory

less file   view the contents of the file (press Q to exit)
cat file   print the contents of the file on the screen (stdout)

mkdir newdirectoryname   create new directory
cp file1 file2   copy file1 and call the copy file2
mv file1 file2   rename (move) file1 to file2
rmdir directoryname   remove empty directory
rm file   remove file

man command   documentation about the command

Try this Unix tutorial for more information.

Programming assignments

Each program should be submitted through Blackboard (Course Materials > Lab).

You can submit all your programs at the end of the lab session. This way, we can hopefully avoid the situation when you are quickly writing your program, immediately uploading it to Blackboard, but then, say 10 minutes later, realizing that there is a bug in it.

Basically, submit when you are sure that it will be your final version.

Each program should start with a comment that contains your name and a short program description, for example:

  Author: Your Name 
  Description: Lab 1. Task 1. Hello world

You can submit incomplete programs, but their “incomplete” status must be clearly mentioned in the comment to the program. In this case, also briefly describe what is implemented, and what is not.

Task 1. Write a first C++ program

The goal is to write a simple program that succesfully compiles. The program should print “Hello World!” on the screen.

You can use gedit text editor to write it.

If the program is called task1.cpp, compile it with g++ and execute:

$ g++ -o task1 task1.cpp
$ ./task1

Task 2. Write a program that computes the roots of a quadratic equation with integer coefficients.

Quadratic equation

The program should ask the user to enter the coefficients of the equation. Use the standard input (cin) for that.

Then, it should print out the roots, correctly handling three possible cases:

Try to write it yourself, without looking at the lecture notes I’ve posted. Use the notes only if you need help or don’t remember something.

Task 3. Classifying characters.

The program should ask the user to input a character.

The character should be classified into one of the following groups:

The program should print out name of the character group to which the input character belongs.

Use switch and if statements where appropriate.

Since characters in C++ are represented by their integer ASCII codes, you can check the ASCII table and use the comparison operatots <= and < to capture whole ranges of characters, for example a character c is a digit if

bool c_is_a_digit = ('0' <= c) && (c <= '9');

You can get the ASCII table from the the Internet or using the following terminal command:

$ man ascii

If you are done with that, how would you make the program recognize Uppercase letters and Lowercase letters?