Lab 1: Getting Started with Linux and C++

The objectives for this lab are to:
  • Learn basic Unix/Linux skills in navigating the filesystem and editing files.
  • Compile and run your first C++ program.
  • Demonstrate your understanding of basic I/O functions and mathematical expressions in C++.
  • zyBook's zyLab

    The zyLab exercises for this week are visible and available on your zyBooks account.
    (If you would like to practice first, do the short training exercises in 1.12 and 1.13).

    Pre-Lab: Getting Started with Linux

    When you are in room B019 Fitzpatrick, if you want to use one of the room's computers, it is strongly recommended that you use the Linux boot of the desktop computer. Yes, you could also do the work on the Windows boot if you wish, but that will require that you putty to a linux machine. Feel free of course to use your laptop if you prefer. Either way, you will eventually have to submit your files to your course dropbox. Refer to the class discussion for details; TAs in the labs will also reinforce those details.

    Open a terminal window on your screen. Inside the terminal window, you will be in a regular linux shell, which basically displays a prompt and waits for you to type a command. The TA may present an overview of useful Linux commands in this lab session (many of the commands were also presented in the class lecture).

    To get started, you can follow the recipe below.

    Note: the description below is for the setup where you work on all your files in your personal space, and then when you are done you submit the required files by copying them to your dropbox under the course directory. An alternative setup would be to go straight to your dropbox, and do all the work there; that way you don't have to copy files across the network; but with that alternate model, you would not have a copy in your personal space, you would just have the files in the course directory. It's up to you what model you prefer.

    For organizational purposes, it is recommended that you first create a directory for this course; then you will basically create a sub-directory for each lab during this semester. And you will do the same under your personal dropbox in the course directory, where you will submit a copy of your files.

    So, to get started, from your home directory:
        mkdir cse20311
    Of course, you can name your directory anything you want.
    You now have your own directory for this course in your personal space, which you can use for the whole semester.
    Now, for this lab, create a lab1 (or any name of your choosing) under your own course directory:
        cd cse20311
        mkdir lab1
    And now cd into the newly created directory.
        cd lab1

    Your prompt may indicate what directory you're in, but it's usually a good idea to type pwd every now and then to double check.

    Note that if you are in your home directory, you can go straight to that lab1 directory in just one command:   cd cse20311/lab1

    Text Editor: Most Linux systems come with several editors, such as nano, vi/vim, gedit, emacs, etc., and you are welcome to figure them out and use them on your own. You may use any editor you want. You will soon identify that one text editor that you are most comfortable with. We recommend vim, but some of you may prefer nano which is quite easier to learn at first.

    You may incidentally find it more practical to have two terminals, both at the same directory, one for editing a program, one for running commands.

    In class you have learned the following Linux commands: pwd, mkdir, cd, ls, mv, cp. That's enough to get started. There is much more to learn about Linux, but you will pick it up week by week with practice.

    Let's now start with the programs for lab 1.

    Part 1: Hello World

    The purpose of this part is to learn to create a program with a text editor, to compile it, and to run it.
  • Use any text editor of your choice to create a file called prog1.cpp. Enter in the source code for the following program, and save it.
  • As shown in class, compile with the following command: g++ prog1.cpp -o prog1. If there are any errors, go back and make sure you entered the code correctly. If the compile was successful, you will just get the linux prompt.
  • Notice that a successful compile will create the file prog1. That is the executable. Use ls -l to see that it is there.
  • Enter ./prog1 to run the program and see how it successfully displays the output: Welcome to C++!
  • Congratulations, you have written your first C++ program. (Don't worry, it gets harder.)

    Part 2: Football Score Formula

  • Use any text editor of your choice to create a file called prog2.cpp
  • Write a C++ program that asks the user for the number of touchdowns, of extra points, of field goals, and of safeties (worth, resp., 6, 1, 3, and 2 points) scored by the Irish, and that then computes and displays the total number of points scored by the team.
  • Refer to class examples for the proper use of cin for reading values from standard input.
  • Compile your program with g++ prog2.cpp -o prog2 . If there are any errors, edit the file and try again.
  • Test your program by running ./prog2
  • Part 3: Formula Calculator

    (You know the commands to edit and compile programs now, so I will just give you general instructions for the program.)

    Create a C++ program (prog3.cpp) that computes the result of a non-trivial formula of two or more inputs that is useful to you. Choose a formula that you are familiar with, perhaps something from another math or science class. The program should ask the user for each of the relevant input variables, then display the result of the formula.

    If your formula needs non-integer values, declare your variables as float (or double) instead of int. You may assume that the user will always input valid data, so no need to check and verify.

    Lab Report

    For each lab this semester, you will create a short report that explains the creative portion of the lab (usually, part 3). This will help the grader to understand what you have written, and assure us that you are on the right track. Don't try to impress us with flowery language, just get to the point with a plain, clear explanation. Take care to organize your thoughts into paragraphs with correct grammar and spelling. The first lab report will be quite trivial but later reports will require more detail.

    This particular lab report should explain the following things:

  • What your program from part 3 is meant to do, from the user's perspective.
  • How the program is structured, from the programmer's perspective.
  • How you evaluated the program for correctness. Did you verify that it produces correct results? Are there any cases where it produces incorrect results? Why?
  • Please write your lab report in plain text using either nano or any text editor of your choice, and save it in a text file named report.txt.

    Turning In

    This assignment is due on Sunday, Sept 8 at 11 pm.

    All submissions will be done electronically, will will actually make life a little easier for you as well as for the graders.

    Copy all of your source files (prog1.cpp, prog2.cpp, prog3.cpp) and the lab report (report.txt) into your personal handin directory, in the course directory's dropbox. You will first need to create a lab1 directory under your personal dropbox, which is where your files will be submitted. It is important that the directory be created before you attempt to copy files to it.

    (Hint: to save time, after having run the above command, you can then use the up-arrow to bring it back and modify it for the other files, and then run the modified command)

    You can verify that your files are handed in by going to that directory, and running the ls command.

    Note: in the dropbox, you must name the directory lab1, since this is where your homework files will be submitted; in your personal home directory, you may of course give your directories any names you want; in case you give them the same name, make sure you don't confuse them.

    Note that we will soon learn how to create symbolic links, which will make all the above much shorter and quicker!

    If you find a mistake after turning in but before the deadline, you can update your submission by simply copying the necessary files again. You are free to turn in assignments multiple times before the deadline expires.

    Note: you may of course use any means of your choice to submit the files. However, learning how to do it (and how to do many other tasks) at the command line will become an essential, and soon necessary, skill to have.

    Have fun!