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.
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.
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++
.
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.
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.
Connect to CSIL using SSH.
Mac OS or Linux systems
USERNAME
with your CoE username.$ 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:
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.
Install a program called PuTTY. Follow the guide here.
csil-01.cs.ucsb.edu
, you should use csil.cs.ucsb.edu
(without the -01
numbers).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
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:
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.
Vim (available by default on most UNIX-based systems)
Emacs (available by default on most UNIX-based systems)
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:
"This will be printed out to the terminal." << endl; cout <<
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.
This second part of the lab needs you to:
cin
)var1
, var2
, var3
)3 * (var1 + 2 * var2) - 4 * var3
.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.
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
.
Once you are satisfied that your programs are correct, then it’s time to submit them.
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
.
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!