CS24, Fall 2016

Lab01:
Orientation


Goals for this lab

By the time you have completed this lab, you should be able to

And most of you will have chosen a partner with whom you will work on future labs.

Step by Step Instructions

Step 1. Create/verify your College of Engineering Account, and log on

By now you should already have a "College of Engineering" (CoE) computer account.

Only if you do not have a CoE account yet:

Log onto your account. If your username/password don't work - see troubleshooting login.

Open a terminal window. That's the Application Menu, then System Tools, then Terminal Window.

Step 2: Create a cs24 directory and a lab01 directory

Create a directory named cs24 inside your home directory. Then create a sub-directory named lab01 inside cs24. Finally, cd into that directory and use pwd to verify before proceeding to Step 3. That is, execute the following commands (in bold) at the terminal prompt:

-bash-4.3$ mkdir ~/cs24
-bash-4.3$ mkdir ~/cs24/lab01
-bash-4.3$ cd ~/cs24/lab01
-bash-4.3$ pwd
/cs/faculty/mikec/cs24/lab01

The system's response (last line) will start differently for you, but verify the rest matches this example.

Step 3: Use an editor to create an ID file, and turn it in with turnin

Create a new file name id.txt using emacs or another editor available in Phelps 3525 if you prefer. Here is some emacs help if you need it.

The contents of this file are very specific. It should have exactly 3 lines as follows:

  1. Your official last name. By official we mean the name used by the registrar (and us) to identify you, and by last name we mean your surname (family name). If you are unclear about which name this is, then look it up in Gold.
  2. Your official first name. Again we mean the name used by the registrar, not a nickname or anything else.
  3. Your complete email address. We prefer your umail address here if you have one.
Here is an example id.txt for the instructor. Notice no spaces at the start or end of any line, each line (including the last one) ends with a carriage return, and there are no blank lines. Exit the editor when you are done creating the file.

Now you must turn in the file to help us identify you from your CoE account. Type the following at the prompt:

-bash-4.3$ turnin Lab01@cs24 id.txt

You should be asked if you want to turn in this file. Respond "yes", and then you should get a message indicating that your efforts were successful! Read the message, and redo whatever is necessary until you succeed. Here is the instructor's successful turn in (user input is boldface):

-bash-4.3$ turnin Lab01@cs24 id.txt

These are the regular files being turned in:

	    Last Modified   Size   Filename
	    -------------- ------  -------------------------
	 1: 09/26/15 10:09     35  id.txt

****************************************************************************

You are about to turnin 1 files [1KB] for Lab01 to cs24

*** Do you want to continue? yes
id.txt

*** TURNIN OF Lab01 TO cs24 COMPLETE! ***

Step 4: (Aside) Use a program named ch to try some C++ features

This section of the lab is mostly just for you to "wet your feet" writing some C++ statements, in case you have not done that in awhile. It introduces you to all the C++ you'll need for later lab steps, and as a bonus it helps you get (or regain) familiarity with C++ reference variables and functions. Finally we want to make sure you know about the ch program.

The program named ch at Phelps 3525 (and CSIL) can be used to execute C++ (and C) statements without writing and compiling whole programs. Type ch at the prompt to start the program:

-bash-4.3$ ch
                              Ch (64-bit)
                  Student edition, version 7.0.0.15191 
              Copyright (C) SoftIntegration, Inc. 2001-2012
                     http://www.softintegration.com
ch>

By default, the ch prompt is your current working directory. The prompt is simplified in these instructions. You can change it for a particular session by entering "_prompt = "ch> "" or to anything else you put between the quotes.

With C++ we use the cout object's insertion operator (<<) to print to standard output, and the cin object's extraction operator (>>) to get data from a user and store it in a variable. Try both of those things now.

First print "Hi" to standard out as follows, then print it again with a newline (ch will also print its own newline each time):

ch> cout << "Hi";
Hi
ch> cout << "Hi" << endl;
Hi

ch>

Notice how the second statement uses the insertion operator twice, once to print the string "Hi" and again to print endl (a "stream manipulatator" meaning a newline). By the way, the cin object's extraction operator works that way too, if you want to get two values from the user in a single statement.

Now declare an int variable named x, get this value from the user, and print it:

ch> int x;
ch> cin >> x;
175
ch>
ch> cout << "You entered " << x << endl;
You entered 175

ch>

You should have learned about pointers in CS 16 as a way to indirectly access a variable (even if you took that course when it was taught in C). Additionally, C++ has reference variables that let you use a simpler syntax than pointers. An & is used in the declaration to identify the variable as a reference, and the variable (which cannot really vary) must be initialized to an existing memory location immediately. Declare a reference to int named rx, and initialize it to x:

ch> int &rx = x;

Now rx is like an alias for x - just another way to refer to x. Try setting it to a value, and then notice what happened to the value of x:

ch> rx = 7;
7
ch> x
7

The reason rx is not really a variable is that it cannot be assigned to any other memory location - it is permanently associated with x now. Of course, there is no real need to have a reference variable set to a variable already in the same scope like this example. Reference variables are especially useful as function parameters though.

To witness the value of reference variables as function parameters, type (or copy/paste) the following two function definitions:

ch> void triple1(int i) { i *= 3; }
ch> void triple2(int &i) { i *= 3; }

You should notice that function triple1 is useless - it just triples a copy of the value passed to it. Try it with x, then let ch display x to see it has not been changed. Then pass x to function triple2, and let ch display it again:

ch> triple1(x);
ch> x
7
ch> triple2(x);
ch> x
21

Do you know why triple2 was able to change the value of x, but triple1 was not? Think about how a function parameter is initialized by the value that is passed to it, and in this case that means the reference variable becomes associated with the variable in the calling program.

You can exit ch now. Just remember it is available to try out simple things.

Step 5: Write, compile and run a simple C++ program

Create a new file named hello.cpp (use emacs or another editor)

Make the following edits to this file.

  1. Type a comment at the top with your name and the date you are doing this lab. Note: this is IMPORTANT, and you should make it a habit to include such a comment at the top of every program you ever write - especially for this class - from now on. Remember that a C++ comment begins with two slashes (//).
  2. Then type (or copy/paste) the following two lines after your header comment:
    #include <iostream>
    using namespace std;
  3. Define a main function:
    int main() { ... }
  4. Inside main, use cout and its insertion operator to print exactly the following message:
    Hello!
    CS24 is fun.
    Be sure the message is shown on two separate lines, and that a newline is printed after the second line too. There must not be any spaces before or after the text on either line. Make sure it matches the sample exactly.

Save the file, and quit the editor.

Use make to compile your program (later you will learn other ways to compile, but make is good enough for this lab). Then run it to make sure everything works. Here is a test of our solution (again, user input is bold):

-bash-4.3$ make hello
g++     hello.cpp   -o hello
-bash-4.3$ ./hello
Hello!
CS24 is fun.

If errors occur: (a) don't panic, and (b) don't immediately ask the TA for help! Instead, take a deep breath, and then actually read the message that comes up. It will often give a line number, and some hint as to what might be wrong.

Then, look at your program, around that line, and also immediately before that line. Often, if the error is reported on, say, line 15, it is because of a missing semicolon, on line 13 or 14, for example.

If you've really looked, and are still stumped after two or three tries, then you might ask the TA for help. But try to fix it on your own first.

When you are satisfied it works, proceed to Step 6.

Step 6: Create a submit.cs account, and join this class

If you don't already have an account on the submit system, make one now at https://submit.cs.ucsb.edu/form/user.

The Create New Account form will insist that you sign up using your umail address, and we insist you enter the name listed on the roster (not a nickname that you go by) - in fact, you should use exactly the same first and last names from id.txt that you created and turned in for Step 3.

Once you've created your account, login with the email and password you used to register. At the login page, follow the Join Class link, and then click the button labelled "Join CS24_f16" to join our class for this quarter.

Step 7: Submit your hello.cpp program, and make sure it's correct

You should see CS24_f16 appear on your homepage after logging into the submit.cs system from now on. Click on the course now.

Then find "Lab01" and click on the "Make Submission" button next to it.

This is the web interface for submitting your code for the assignment. You can upload your source file directly on this page. Either drag and drop, or let the browser open a dialog to let you navigate to the directory containing your hello.cpp file and select it.

Once your file is uploaded, click the "Submit 1 File" button. Once you submit, you should see a page detailing your submission. The system will automatically grade your program, and will show you the results on this page after a 1 minute (or so) delay.

You can alternatively submit your code from the command line (terminal) on any CS machine, including the Phelps lab machines or the CSIL server. You can use this method when logged in remotely. To submit the hello.cpp file to this assignment, execute the following command:

~submit/submit -p 526 hello.cpp
Notice this command is also shown on the web interface. Every assignment will have a different project number (e.g., "-p 526" for this assignment) that you can look up by clicking its "Make Submission" button. The program will ask you to login with your submit.cs username and password (the password will not be printed to the terminal, but what you type will be used). It will also offer the option to save your credentials, so that you do not have to login next time you submit. You may choose to do this or not. After the submission succeeds, you should see the program output something like:
Results will be available at: https://submit.cs.ucsb.edu/submission/xxxxx
You can copy the URL and paste into a web browser to reach the same submission result page as described above.

After the 1-minute delay, the submit system will show your score and give you feedback. Refresh the webpage after a minute to see this information. A correct submission will earn the full 30 points (30/30). If your submission fails the test, it might mean your program caused compilation errors when the system tried to compile it, but more likely (we hope) it means your program's output does not exactly match the expected output. Either way, fix whatever is wrong and submit again until it earns 30/30 points.

Step 8: Select a partner for future labs/assignments

Choose a lab partner. The intent is for you to work together with this person in future labs, and optionally on programming assignments that allow it. An odd number of students in attendance means one student will not be assigned a partner today - the TAs will make sure it is only one person. Ask a TA for help if you cannot easily find a partner.

We hope this won't be your first experience with "Pair Programming." If it is, then please try to pair with a student who is experienced at it. You will learn more about pair programming as the quarter progresses. In short, there are two roles: pilot and navigator. The pilot types, and the navigator helps by observing, inspecting, thinking and suggesting - but neither of you directs the other. You are helping each other so that you both understand the work.

The TA will provide a sign-up sheet (or other way) for you to note your partnership. Go together with your partner to verify that is done before leaving the lab today.


Evaluation and Grading

Each student must accomplish the following to earn full credit [50 total points] for this lab:

Please do not pester the TA to be dismissed if you finish early. Instead, experiment with ch a bit more, help one of your neighbors if they are stuck, and/or work on PA1.


Prepared by Michael Costanzo.