Due: next Monday before 2359

Part 0: Lab Ground Rules

Labs in this class are your best opportunity to practice programming in a setting where you can get lots of help.

The key to succesfully finishing labs is reading the instructions carefully.
Make a good habit of reading every word -- don't skip any text.

Part 1: The Computing Environment in Your Laptop

The system setup page and resources page are your starting points for help on how to use the computing environment and get things done for this class.

Of course, you should have already followed these instructions to set up your Ubuntu environment on your laptop. If not, now is the time to do it!

The computers in the lab are also running Ubuntu! To get them set up with your ic210 directory, and the submit script that will be used to submit your labs, just like in WSL, follow these instructions. To open a terminal on an Ubuntu machine (like the ones in the lab), you can click Ctrl+Alt+t.

Part 2: Editor

Choose your editor

We will be using the Unix environment to create, compile&link and execute C++ programs. But there’s an important, momentous decision to make first: what text editor will you use? The following are all available on your laptops (following the install instructions) and on the lab computers. Choose wisely!

The way you open these text editors is a bit different: For Atom (which runs both on Windows and Linux), you open Atom from your start menu, then you navigate to your ic210 folder to create and edit files.

If you're using gvim, emacs, or geany you can open your text editor by running a command in the terminal directly. This can be pretty convenient, since you already have to have a terminal window open to compile and run your programs.

For example, to open or create a file hello.cpp using geany, assuming you are already in the ~/ic210/lab01 folder, you could just type:

geany hello.cpp &
Notice the & at the end - this makes sure geany doesn't "take over" your terminal window and you can keep using it while geany is open. Replace "geany" with "gvim" or "emacs" to use those programs instead. Note that gvim is awesome and actually doesn't require you to use the &. If you're using VI (i.e., gvim), start with this quick intro and list of useful commands. If you're using emacs, check out this getting started guide. The resources page also has further references to help you learn the powerful features of these text editors. (You should be able to get started in atom or geany without any extra help.)

Part 3: Your First Program

  1. Open a Linux (Ubuntu) terminal window. In WSL, this means opening the Windows Terminal (be sure your tab has the penguin!) or, if you couldn't get that installed, running wsl from the start bar. In Ubuntu labs, that means clicking Ctrl+Alt+t, or selecting Terminal from the menu.
  2. You should see a prompt like
    m012345@WK2ESUIO4MID:~$

    This is actually telling you some useful information! In this example:

  3. Type

    ls
    to see the contents of your current directory. You should see something like

    Desktop Downloads bin ic210

    The Desktop and Downloads are links to those folders in Windows, and ic210 is a link to the exact same folder called ic210 on your Desktop.

  4. Of course the ic210 directory is the place to be! To move into that directory type

    cd ic210

    Notice that your prompt changes to indicate you are now in a different directory!

  5. It's a good idea to always start and end any period of work that you do, by syncing up with git. More detail on this below, but for now you just need to run:

    210sync
  6. Now we want to make a new directory to hold your files for lab01 using "mkdir" command, and finally change to this new directory:
    mkdir lab01 
    cd lab01
    
  7. Open your preferred text editor.
  8. Now use your text editor to copy in the following source code into the main.cpp file:
    #include <iostream>
    
    int main()
    {
      std::cout << "Hello World!" << std::endl;
      return 0;
    }
    
    When finished, save your file.

  9. Compile (and link)

    If you want the programs you write to actually do something then you must compile them (turn source code into machine language). Converting high-level C++ source code into machine language is very complex, but compiler vendors solve this problem for you.

    To compile your program,

    There should be a new file in your working directory called m012345. Check this by typing:
    ls

    If you omit the "-o m012345", which we will often do, then a file with default name of "a.out" is created. Try:

    g++ main.cpp
    ls 
    The new files m012345 and a.out are the "executable file" for your program.

    If you do see any error messages from g++, then get assistance from the instructor. Tips for deciphering error messages are given below at the end of this tutorial.

    Note: technically, the single command you issued actually "compiled" and "linked" your program. We'll talk much later about this, but in short "linking" (amongst other things) takes your code and combines it with other code provided elsewhere to do standard things like input and output (this is referenced by the #include <iostream> line in your code). Often, we'll informally call this whole process "compiling."

  10. Run!

    Once you successfully compiled and linked your program (no errors), then you are ready to execute your program. In your current working directory, you will see a new file called m012345 (or whatever you have named it). That is your program. To execute a file, type the file's name preceded by a "dot slash" (literally, "./") and hit the enter key.

    screenshot
    Note: the "dot slash" is telling the computer to look for the program (m012345) in the current directory (indicated by the "dot").

  11. Program Output:

    Your program will output to the same terminal from where it was executed, as seen above.

    Now, let's view the executable file you just executed. Type

    ls -l
    to list details of the contents of your current working directory.
    screenshot

Part 4: Let's do it all over again!

You just accomplished quite a bit; you typed in a C++ program, compiled it and executed it. It is important that you understand all the steps involved, and that you are comfortable using a text editor and the terminal.

Repeat Part 3 of this lab all over again, with these changes:

  1. First, close all open windows (including the editor and terminal).
  2. Open a terminal and "cd" to the correct directory for lab01.
  3. Instead of entering the "Hello World" program, enter in this simple addition program (name your source file add.cpp instead of main.cpp and make sure you replace first line comment with your actual name and alpha):
    add.cppsample run
    // (Your name and alpha)
    // This program adds two numbers
    
    #include <iostream>
    
    int main()
    {
      int number1, number2, sum;
      number1 = 12;
      number2 = 13;
      sum = number1 + number2;
      std::cout << "The sum of these two integers is "
                << sum << std::endl;
      return 0;
    }
    
    ~/$ ./add
    The sum of these two integers is 25
  4. Compile your program giving the executable the name add.
  5. Run your program, making sure it prints out the right answer! Fix it if it doesn't!

Part 5: Fixing errors in compilation

How to read the error message

When you make certain kinds of common mistakes (syntax errors), the compiler won't be able to understand your program and will issue error statements in the terminal window.

Tips

You will improve as you see enough errors to associate the messages with the syntax error. Here are a few simple tips:

Errors on purpose: Don't skip this part!

Experience and practice help a lot here. So for each of the below, purposefully make the syntax error in your current program and try to compile to see what the error message is. Correct the error before introducing the next error into your program (e.g., only add ONE ERROR at a time). Go slowly and pay attention, this will save you time later!

Part 6: Write code to submit

  1. In your Terminal, cd to ~/ic210/lab01.
  2. Create lab01.cpp with the following content:
    #include <iostream>
    
    int main()
    {
       std::cout << "Hello World!" << std::endl;
       std::cout << "This is a 1!" << std::endl;
    
       return 0;
    }
    
  3. Modify the program lab01.cpp so that it's output matches what's shown exactly!
    required output
    Hello World!
    Hello Solar System!
    Hello Galaxy!
    Hello Universe!
    Hello ... ?
    This is a 1!
    This is a 2!
    This is a 3!
    This is a 4!
    This is a 5!
    Use editor commands: Your focus is on using the editor to make these changes as efficiently as possible! In other words, of course you can do it, I want you to concentrate on doing it smart!

    It's almost a puzzle to use as few keystrokes as possible. In particular, what editor commands would do the following?

    • Copy a line
    • Paste
    • Delete (or replace) a word

Submit this lab01.cpp to the submission system

  1. Make sure your are in the directory that contains the lab01.cpp file.
    cd ~/ic210/lab01
  2. Submit your program by giving the following command:
    ~/bin/submit -c=IC210 -p=lab01 lab01.cpp
  3. Important: Using your web browser, check the submission server and see if your work passes the test case.

    If you did not get the output 100% correct, the page should give you an indication of what's wrong with your output.

    Keep fixing your program and resubmitting until it works perfectly.

Part 7: Syncing your code to git

You'll be writing a lot of code this semester. It would be a shame if anything happened to it!

Run 210sync to save your work in github.

Remember to run 210sync every time you do some work, either on your laptop or on your lab machine. Also make sure you always do your work in your ic210 directory!!

.