Lab 1: Getting Started

Due: Tuesday, June 29, 2021 (11:59 PM PDT)

Introduction

Your first lab in this course is an introduction to programming in C++ and the tools you’ll need to do so. The intended outcomes are:

This lab must be completed individually. In later labs you will be encouraged to work with a partner, but not this one.

Step 1: Setup

Step 1a: Pick an environment

Depending on your computing setup, you have several choices on how to complete labs for this course. No matter what you choose, the outcome is that you must have access to a C++ compiler that supports the C++11 standard.

Here are your options. Details for each follow.

  1. Install C++ compiler on your personal computer
  2. Use a provided VirtualBox image with compatible packages already installed
  3. Remotely access CSIL (the CS department’s lab computers)

Option 1: Install C++ compiler locally

How to install C++ depends on which operating system your computer runs.

Linux

It’s possible your system already has a compatible C++ compiler installed. To check, open a terminal, and run the following command (don’t type the $).

$ g++ -v

If it says g++: command not found, then you need to install the g++ package from your package manager. On Linux distributions that use apt as the package manager (like Ubuntu, Debian, Mint), you can install g++ using the two commands:

$ sudo apt-get update && sudo apt-get upgrade -y
$ sudo apt install g++

Mac OS

If you have Xcode installed it’s possible your system already has a compatible C++ compiler. To check, open a terminal, and run the following command (don’t type the $).

$ g++ -v

If it says g++: command not found, then you need to install a C++ compiler. On OS X 10.9 and later you will be prompted to installed Mac’s Command Line Tools. Install those and you should be set. Otherwise, you can either install Xcode via the Mac App Store, or install a C++ compiler separately through a package manager like brew (but you’re on your own with that).

Windows

For Windows, it’s highly recommended to install the Windows Subsystem for Linux (or WSL). Follow the guide here to install WSL. Select “Ubuntu” as the Linux distribution. Open a bash terminal and follow the Linux section above to install g++.

Option 2: Using VirtualBox

A previous instructor made a super helpful video on how to download, set up, and run VirtualBox to compile your code! Watch it here.

The username and password for the provided VirtualBox OS are both ucsbcs.

It is possible that your Windows computer might give an error when you try to unzip the Debian.vdi.zip file. To make it work, download 7-Zip and use that to unzip the file.

Option 3: Connect remotely to CSIL

  1. Create a CoE account if you don’t have one already. You need this to remotely access the CSIL environment. Create a College of Engineering account at https://accounts.engr.ucsb.edu/create. If you forgot your CoE password, reset it here.

  2. Connect to CSIL using SSH.

Mac OS or Linux systems

$ ssh USERNAME@csil.cs.ucsb.edu

You will be prompted with a question asking if you want to connect, type yes and then enter to continue. Enter your CoE password and then press enter. You should be greeted with a shell prompt and are now connected to CSIL.

Windows systems

Two options:

  1. Install a Linux Bash terminal on Windows following the guide here. Then open a bash terminal and follow the above section on connecting to CSIL for Linux systems.

  2. Install a program called PuTTY. Follow the guide here.

Step 1b: Create cs16 and lab1 directories

Now that you have a terminal open, run the following command to make sure you are in your “home” directory. In shell, ~ is a shorthand for your user’s home directory. cd is a command that stands for “change directory”.

$ cd ~

Then, let’s create a directory called cs16. The command mkdir stands for “make directory”.

$ mkdir cs16

After that, change into the cs16 directory by issuing the command:

$ cd cs16

Since this is Lab 1, let’s make a lab1 folder here, and change into it. Run the next two commands.

$ mkdir lab1
$ cd lab1

Step 1c: Pick a text editor

The next step is to pick a text editor. A text editor is a program that’s specially made for efficiently writing and editing code. It’s like a word processor for code (but please don’t use MS Word to write code).

Text editors come in a lot of different flavors and varieties, and there is no one “best” text editor. My advice is to just pick one and give it a try for a while! Here’s a few choices to get started:

  1. Microsoft Visual Studio Code

Note: If you are connecting to CSIL I highly recommend using Visual Studio Code as your editor. It has a very useful SSH extension that let’s you easily connect to CSIL and transfer files.

  1. Atom

  2. Vim (available by default on most UNIX-based systems)

  3. Emacs (available by default on most UNIX-based systems)

  4. Micro

Step 2: Create 2 C++ programs

Program 1: hello.cpp

This part of the assignment needs you to write a program that prints out two lines on the display, and nothing else. Remember that newlines, spaces, and tabs are characters. The output should look exactly as follows (no spaces before or after each line, except the 2 newlines):

Hello, world!
I am ready for CS16!

Start with a “skeleton program” (or template) that contains the necessary structure but that does not do anything:

#include <iostream>
using namespace std;

int main() {
    // Your printing code should go here

    return 0;
}

Now, using the text editor you chose in Step 1c, create a file named hello.cpp. Then, type the template above into that file. Next, you will need to replace the comment with code that prints out the expected output. Comments in C++ are lines that start with // or multi-line text between a /* and */.

Note: For students familiar with Python, remember that lines start with # are not comments in C++. Rather, they are important include statements that allow your program to use libraries (like iostream for input/output functionality).

To print out text to the terminal, you can use the cout stream. To output something use the << operator as shown below:

cout << "This will be printed out to the terminal." << endl;

The endl keyword will create a newline (i.e. a carriage return).

You can adapt this line to achieve the objective of the assignment. You need to print two lines, each with a newline at the end. You can do this with one or two statements.

Program 2: calculate.cpp

This second part of the lab needs you to:

The output should look exactly as follows when using the following example input values.

Please enter 3 numbers.
2
3
4
The formula result is: 8

You can start with the same template as the previous exercise.

Step 3: Compile the code

Now that the code is written, we need to compile it. This is done using a special program called a compiler.

Before moving on, make sure that you save your code. The following step will be done in the terminal.

For C++ code we will use the g++ compiler that’s built into many UNIX-based systems. You can compile the hello.cpp file into an executable called hello with the following command:

$ g++ -o hello hello.cpp

This will compile your code (hello.cpp) and make an executable version of it (hello). Specifically, it will tell the compiler to take the source code file hello.cpp and compile and link it to an executable called hello.

If the compilation is successful, you won’t see any output from the compiler. But if run an ls command from the terminal, you should see a new file has appeared: hello.

You can then use the following command to run your program:

$ ./hello

This means, “in the current directory (represented by the .), run the program hello”. You should then see the program output the two expected lines.

The other possibility is that the program may not compile successfully. What to do then? If you run the g++ command and are unsuccessful with compilation, then you might see an output that looks like this:

hello.cpp: In function 'int main()':
hello.cpp:10:1: error: expected ';' before '}' token

The compiler will try to give you hints on the line where the error occurs (in this case, it’s complaining about line 10). The error in this case is about a missing semicolon ;. You will also note that when an error occurs an executable file is not produced.

If you encounter an error, use the compiler hints and examine the line in question. If the compiler message is not sufficient to identify the error (which happens more than sometimes), you can search online to see when the error occurs in general. Once you have fixed the error, run g++ again. Debugging your code is a necessary ritual in almost all programming (even code written by expert coders). More on that in a later class.

Now, do the same for calculate.cpp.

Step 4: Submit your programs for grading

Once you are satisfied that your programs are correct, then it’s time to submit them.

Submitting your program for CSIL users

If you’re using CSIL to write your code, you need transfer the source files (hello.cpp and calculate.cpp) to your local computer. If you did not use CSIL, you can skip this section.

There are several ways to do this:

Visual Studio Code SSH plugin

Visual Studio Code has an extension called Remote - SSH provided by Microsoft that let’s you connect to a remote machine (like CSIL) and interact with it as though it were on your local system.

Install the extension and follow the instructions on the extension page to connect to CSIL. Then you should see the files on your CSIL account show up on the left (as well as a terminal on the bottom which you can use to issue commands to CSIL, very useful).

Now you can directly copy these files (or copying the content by highlighting it) and save them to your local system. There you go! Move on to the next section.

scp

Open a terminal on your local computer. We will use the scp command (“secure copy”) to connect to CSIL and transfer your files to your local computer. Here’s an example of how to use it:

$ scp USERNAME@csil.cs.ucsb.edu:/cs/student/USERNAME/cs16/lab1/hello.cpp .

Replace USERNAME with your CSIL username. The general template for this is scp USERNAME@URL:REMOTE-PATH LOCAL-PATH.

Repeat this command for calculate.cpp.

Submitting on Gradescope

We will use Gradescope to grade all homework, exams/quizzes, and lab assignments. You should have received information about how to log into Gradescope and register for the course.

Log into Gradescope and select CMPSC 16 under Summer 2021, and navigate to the Lab 1 assignment. Then click on the “Upload Submission” button on the bottom right corner to make a submission.

You will be given the option of uploading files from your local machine or submitting code from a GitHub repo. Choose the first option and follow the steps to upload your 2 source files, hello.cpp and calculate.cpp, to Gradescope (do not upload any other files). If your files do not have those exact names, the autograder will not grade them. You can resubmit your files as many times as you like before the assignment deadline.

You should receive 50 points for each correct program for a total of 100 points.

Congratulations on completing your first C++ programs!