CS 127 Getting Started in C++ Lab

Overview

Welcome to CS 127!!!

The students in this class have a variety of backgrounds. Specifically, many are already fluent in C++. Others are fluent in Java. This lab is designed to bring everyone up to speed with the same amount of C++ knowledge, and using C++ on our systems in particular. Those students who are already familiar with C++ should see this lab as a review and as an opportunity to fill in gaps where they might exist.

For this lab, find a partner to work with. If one of you has trouble logging into the machines, let the other person log in for today. If you both have trouble, see if you can shuffle groups so that each person has a partner that can successfully log in.

Logging In

We're going to be using the Linux operating system for this class. Make sure that the machine in front of you is, in fact, running Linux and not Windows. You'll know if you're running Linux if you see the GNOME footprint next to the login screen.

If you see a Windows Novell login, you need to reboot the system. To do this:

Log into Linux by entering your username and password. The password is the one that you have used for CS Linux-based classes in the past, or the one on the form which has been passed out to you if this is the first class in which you will be using Linux.

Creating a folder and writing a test program

1. It's a good idea to keep your programs organzied by keeping them not in your root directory (also known as your home directory), but rather in a subdirectory. To do this, enter the following inside the terminal window (I have descriptions of each command following them):
cd ~
Emacs is a very powerful text editor used for writing programs, that I recommend you use for editing text. If you are familiar with another Linux text editors and wish to use instead, you are free to do so. Since I assume that most of you have little Linux experience (perhaps except for BlueJ), we'll go through some exercises in getting started with emacs. Most of what we do below will be described via keyboard shortcuts. You can do most of it as well with the menus within Emacs, if you like - but you'll be much faster and more effective in the end if you learn the keyboard techniques.

1. Start up emacs by typing

emacs &

inside the "terminal" window. The "&" means start it as a background job, so that you still have access to the terminal window if you want it.

2. Enter in the following C++ program:
#include <iostream>

int main()
{
    cout << "Hello world!" << endl;
}

One of the really nice things about emacs is that it will automatically indent your code for you. Use the "tab" key liberally; it doesn't put tabs in as you would expect. Instead, it lines up each line exactly right.

3. Save your program by typing ctrl-x, ctrl-s (the s means "save"). When it prompts you for a filename at the bottom, call this program hello.cpp (the cpp identifies the program as "c-plus-plus"), and then click back on the terminal window. Type "ls". You should see your hello.cpp program listed. You may or may not see a file called hello.cpp~. If you do, the ~ at the end indicates that this is an automatic backup of an old version of your program that emacs has created for you.

4. Compile your program by entering the following at the command prompt:

5. Run your program by typing in the name of your executable. In other words, at the prompt, type:
hello
and see your program run!
 
6. When you wish to close emacs, you can do so by typing ctrl-x, ctrl-c (the c means "close").

Turning in your work

Turn in your "hello" program electronically using the hsp homework submission program. To do this, type the following in a terminal window:
hsp musicant cs127 file
So to submit your file named "hello.cpp", type
hsp musicant cs127 hello.cpp
You can find detailed information on hsp at http://www.mathcs.carleton.edu/system_notes/hsp.html.

Once you have submitted a file with a particular name, you cannot submit another file of that same name.
 

Writing Some Methods (also known as member functions)

1. Copy the files ArrayTools.cpp and ArrayTools.h from the course directory to your cs127 directory. To do this, type
cp /Accounts/courses/cs127/dmusican/ArrayTools.cpp .
cp /Accounts/courses/cs127/dmusican/ArrayTools.h .
Make sure to include that "." at the end of each of the two lines as above. cp means copy, and the dot is an abbreviation for the current directory that you are in. So the first line effectively says "copy the file /Accounts/courses/cs127/dmusican/ArrayTools.cpp to here."

2. Open up the two files inside two separate Emacs windows, and take a look at them. Some of the syntax may be new to you, regardless of background. Walk through the program and see what you think it does. Write down on a piece of paper what the output should be.

3. Compile the program by issuing the following command in the terminal window:

g++ ArrayTools.cpp -Wall -o ArrayTools
4. Run the program by typing "ArrayTools" at the command prompt. Do you see what you expected? If not, try to figure out why.

5. Add a method to the program called binarySearch that does a binary search on the array for a particular number. To refresh your memory, a binary search does what you would ntuitively do -- start in the middle of a sorted list. If your number is too small, then jump halfway in between the number you last picked and the maximum value. And so on...

6. Add a method called selectionSort that sorts the list using a selection sort. A selection sort searches through the array for the smallest number. When it finds it, it swaps it with the number in the first position. It then searches the rest of the array (everything but the first element) for the smallest number. When it finds it, it swaps it with the number in the second position. And so on...

For example, here are the iterations that selection sort would go through in sorting a list of numbers:

17 14 16 15 19 13
13 14 16 15 19 17
13 14 15 16 19 17
13 14 15 16 17 19

7. Turn in your work with hsp at the end of the class period. I won't be grading this lab. As such, it is not essential that you get all the way through steps 6 and 7, though I would like to see how far you can get. Have fun, and see you Monday in the classroom! 

Important Last Step: Checking your email

Each of you now has a separate email account on the mathcs machines, which is independent from your regular Carleton email. Having two email accounts is irritating, and it makes sense to forward your mail to a single location. If this is the first Linux-based computer science class at Carleton, all your mathcs email is automatically forwarded to your regular Carleton account and you can skip the following directions. If you have taken a Linux-based computer science class previously at Carleton, like our old C++ 117 or the Java 117 during spring '02, your email is not automatically forwarded and you should follow the following steps.

To set this up mail forwarding if it has not been set up for you, create a file called ".forward" (the period in front is important) in your home directory. In this file, place a single line containing your regular Carleton email address. For example, my file might look like

dmusican@carleton.edu

That's all you need to do! Test it by sending email to yourself at the mathcs department and see if it forwards to your Carleton address. Here's a quick way to test it: at a Linux prompt, I would type the following:

mail dmusican@mathcs.carleton.edu < ArrayTools.h

This should mail me a copy of the ArrayTools.h file. Next time you check your email, make sure that you received it.