Please read this lab thoroughly, and follow any directions given by your TA.
The lab is due next Tuesday at 10:30 AM.
Important: when your TA is lecturing, we expect you to pay attention.
Also, we expect you to follow the instructions precisely in this and all labs.
At any time, feel free to ask a TA for help. Begin by reading the lab
policy lab policy.
The goal of this lab session is to introduce you to:
- Computers: In particular, Dell PCs.
- The Linux Operating System: The operating system (OS) is the interface
between you and the machine, and allows you to give instructions to the machine. The OS we are running
Ubuntu 10.04 (aka Lucid Lynx)/Linux.
- zsh: The Z shell; a command interpreter used
to pass user commands to the OS.
- The vi Text Editor: A text editor allows you to enter
information or data into the machine and allows you to store, retrieve,
and edit that information.
- Programs:
"A set of explicit and unambiguous instructions expressed
in a programming language,"
from How to Solve It by Computer,
page 1, by R. G. Dromey.
- Program Compilation and Execution (i.e., running a program): The compiler we use is g++.
Important: Attendance is required at all labs. If you leave early,
you must submit your files before you leave.
Important: Failure to do a lab, to submit
your work on time, or to make a proper attempt
on any part of the lab may result in a grade
reduction. Take the labs seriously; we do. Good work will be rewarded.
Please, do your own work.
1. Logging in for the First Time
A TA gave you a sheet with your username/password. Read the entire
sheet, log in, and note how to change your password.
Notice that once logged in, you are automatically connected to the Internet.
Firefox is the browser that comes with Ubuntu.
The icon looks like an orange-colored fox curled around the earth.
- When you logged on, the machine launched the window
manager. Your panel (at the top) and Applications button (at the top, left) appeared.
- The Applications menu. At the top, left corner of your screen
is a button with the Ubuntu icon and the word Applications. Click on this
button to reveal a large menu with many submenus and more buttons. Clicking one
of these buttons will launch that Application; passing your mouse cursor over one
of the submenus will reveal the buttons within that submenu.
- The Panel. Across the top of your screen there is a gray bar or
Panel that holds the buttons to your Applications menu and other menus. The
Panel can also hold launcher buttons for quick access to launch frequently used
Applications.
- We will perform most tasks in a terminal window.
To launch the Z shell terminal, click the Applications
button at the top, left of the Ubuntu desktop, mouse over Accessories, and click Terminal.
Using two Terminals allows you to edit your code in one terminal and test or execute that same code
in the other Terminal. Open two terminals now.
Note that when your terminal opens, you are in your home (upper level) directory.
- Since we will be using the terminal frequently, we want to pin it to the panel for quick access.
Click the Applications button at the top, left of the Ubuntu desktop, mouse over Accessories,
right-click Terminal, and left-click "Add this launcher to panel".
- Changing your Password. To change your password, type utkcs-passwd, press Enter
and follow the instructions. If you want to read more about passwords, click
here.
2. Learning the Operating System -- Some Linux Commands
In a terminal window, you may type one or more commands on a line,
called the command line.
Multiple commands on a single command line are separated by semi-colons.
Hit Enter to terminate the command line and instruct the machine
to execute a command. Try each of the commands in this section.
- ls This command is stands for "list files." Information is stored
in files that are organized into directories,
which are simply collections of files.
List the contents of your current directory with the
command ls.
ls -l stands for "list long" and lists the contents
of a directory in more detail. The -l is called a switch, and
many Linux commands have switches that modify the command's behavior.
- c When you want a clean
screen, enter the command clear (or c for short).
- mkdir This command stands for "make directory."
You must do your work
in the appropriate directory (a special kind of file) for each lab.
This directory is like an electronic file drawer where you will place
the files you create during lab. Create
your Lab1 directory now by issuing these commands:
- mkdir cs102
- cd cs102
- mkdir Lab1
The ~ (tilde) is shorthand for your home directory, and
~/cs102 refers to the cs102 subdirectory of your home directory.
Omitting ~/cs102/ will work just as well if you are already in your
cs102 subdirectory.  ~/cs102 is called a path; it tells
where the file Lab1 is.
Now type ls ~/cs102 (or ls cs102)
to see the contents of your cs102 subdirectory.
Notice that ls lists a ~/cs102/Lab1 directory, displayed as
Lab1/. The slash (/) indicates that file Lab1 is a
directory; the slash is not part of the file name.
Below cs102 you will create a directory for each lab.
- cd This command stands for "change directory."
To change to your ~/cs102/Lab1 subdirectory in any particular window, type one of the following commands:
cd ~/cs102/Lab1   -OR-   cd cs102/Lab1
You are changing to the Lab1 subdirectory of the cs102
subdirectory of your home directory. Notice the change in the
Linux prompt.
The command cd without a path name puts you in your
home (upper level) directory. The command cd .. moves
you up one level in the directory tree.
- pwd This command stands for "print the
working directory." When you enter this command, you
should see output that is similar to the following...
/home/username/cs102/Lab1
...but with your username listed is place of "username."
This path name ends with the name of the current directory,
Lab1
(Your Linux prompt indicates where you are without your
having to use the pwd command. This is something we have
done for you; it is not a standard Linux feature).
Move the cursor to a different window and activate it.
Type pwd into the different window and compare the result to that in the first
window. Note that the working directories are not the same.
Changing directories in one
window does not change directory in any other window. Change to
your Lab1 subdirectory in all windows now.
- cp This command stands for "copy" and is used for copying files.
In order to copy files from the CS 102 management account into your Lab1 directory,
enter the following command in any window:
cp   ~cs102/robot_labs/Lab_Skeletons/Lab1/*   ~/cs102/Lab1
~cs102 refers to the home directory of the CS 102 management
account, and Lab1 is a subdirectory in the cs102 account.
The asterisk is a wildcard and means
all the files. Thus, each file from the ~cs102/Lab1 directory is
copied to your ~/cs102/Lab1 directory. The file names are the same.
In English, the cp command above says, "Copy all the files from
the Lab1 subdirectory of the cs102 home directory into my cs102/Lab1
subdirectory." Notice the presence (or absence) of the slash
character after the tildes:
- ~cs102 refers to the home
directory of user cs102.
- ~/cs102 refers to the cs102
subdirectory of your home directory.
- List files again   List the contents of your current
directory by typing ls. You should see several new file names
listed that you have just copied. If not, please ask a TA for help now.
- less Now you will learn
less, a Linux command for browsing files.
Activate a smaller window and type
to browse the README file. In long files,
move from page to page by hitting the space bar, and move
back a page by hitting b.
When you are finished, hit q to quit
less. Remember how to browse using less; it is a
safe way to view a file since it does not alter either the file
contents or its time/date stamp.
- cat Similar to less, cat is useful for reading files
that are less than a screenful in length. To read the README files using cat,
type the following command:
Note that you do not have to enter any commands to make cat stop
executing. It will simply print the contents of a file to the screen and
stop.
3. Learning the vi Editor
A common way to get information (including programs) into the
machine is with an editor.
We teach a simple but useful editor called vi (for
visual). vim is an enhanced version of the vi editor
which is available on most UNIX/Linux boxes.
vi has two ways of interacting with you,
command mode and insert mode.
In command mode, the commands
you type do not appear on the screen; they cause various things to happen
to the existing characters in the file, or allow you to move around in the file.
In insert mode, the characters you type appear on the screen
and are inserted into the file. When you
enter vi, you are automatically in command mode.
Once you get into insert mode, you can return to command mode
at any time by pressing the escape (Esc) key (top left of the
keyboard). The letters i, I, a, A, and cw
are some commands that allow you to enter insert mode.
4. Practice Exercises to Do Now
Activate the left window and open the rhymes file
by typing the following command:
The text file rhymes is now displayed on
your screen, and you are in command mode.
You may freely modify rhymes since it will not be graded. Note:
<Ctrl> f, for example, means to hold down the Ctrl and f keys
at the same time. <number> means to type a reasonable positive number.
Try all of the following while you have the file rhymes in the vi editor:
1. Command mode (remember, hit Esc to enter command mode):
- arrow keys - Experiment with the arrow
keys and learn to move around a file.
- h, j, k, l - The same as the arrow keys.
- arrow key or h, j, k, l  - Move
the cursor the number of lines or spaces.
Try 4j, 20h, and others.
- <Ctrl> f - Forward one screen.
Experiment with this to see how it works.
- <Ctrl> b - Backward one screen. Same idea as
forward.
- G and gg - G will take you to the end of the file.
Once at the bottom,
try the previous two commands and use arrow keys some more.
Use gg to go to the top of the file.
- <number>G - Go to line <number>.
Pick any line number and go there, such as 22G.
- :<number> - Go to line <number> as well. Try :20 to
go to line 20. Tip: In vi, you do not have to use the Shift
key to get the colon.
- /<pattern>  - Search for pattern.
Search for the word pig (/pig). Now type
n to find the next instance of pig. Repeat the n until it is
clear what is happening. Notice that /pattern is not case
sensitive.
- dd - Delete the line that the cursor is on at the time. Go to line 18
(:18) and delete it. Go somewhere else in
the file and delete another line. Note: The cursor does not have to be
at the front of the line in order to delete it using the dd command.
- <number>dd - Delete <number> lines. Search for
"flappity" and type 7dd to delete seven lines.
- u - Undo the last change you made. You just deleted seven lines; now
type u. The lines will reappear since you "undid" the last command.
Try u again and again; notice what happens.
- x - Delete the character that the cursor is on at the time.
Type x to try it. Type 6x to delete six characters.
- dw - Delete the word that the cursor is on at the time. Search for
"semantics" and delete it. Search
for "funny" and type 2dw to delete two words.
Try u again to restore the words.
- . (period) - Repeat the last command. Go to line
23 and delete it. Try the . command to delete the next line.
Now try 4. to repeat it four times. Try u again.
- $ - Move to the end of the current line.
- 0 (zero) - Move to the beginning of the current line.
- z (Enter) - Redraw the screen with the current line at the top (hit z, then Enter).
- Go to the middle of a line and try the three commands w,
b, and e. Notice what they do.
2. Insert mode - Enter insert mode when you want to
add text to the file. Exit with <Esc> (Escape).
- <Esc> - Return to command mode from
insert mode. If you are already in command mode and hit Esc,
no harm is done. Try it now: Bang (gently!) on Esc a few
times; nothing is changed.
Hit Esc if you aren't sure what mode you're in, or if
strange things are happening. Remember this!
- i - Insert. This command will enter insert mode before the
position of the cursor.
Go to line 8 and insert "silly" before "man".
You can do this with a search (/man, then hit n until you get to line 8), or use the arrow keys, whichever you like.
Make sure the cursor is at the beginning of the word man (on the letter 'm'), then type
i silly. Return to command mode with Esc.
- a - Append. This command will enter insert mode after the
position of the cursor. This is especially valuable if you want to
append to the end of a line. Search for "Bo-Peep" and go to
the end of the line by typing $.
Hit the character a and type any word to append the word to the line.
Return to command mode with Esc.
- A - Append. This command will enter insert mode at the end of the current line.
- o - Open a line below the current line and enter insert
mode. Try it now, type something, then return to command mode.
- O - Open a line above the current line and enter insert
mode.
- cw - Change word. Enter insert mode to change a word.
Changes the current
word from the cursor to the end of the word, replacing the
characters with the new characters until you type Esc.
Try it now, type a couple of words, then return to command mode.
Now try 3cw to change three words.
- <Backspace> - While in insert mode, delete characters before
the cursor. Go into insert mode and
insert some characters. Do not hit Esc. Now hit
Backspace one or more times to delete some but not all of
the characters you typed in while in insert mode. Now hit Esc.
3. Saving files, exiting vi
Practice the following four commands and learn the differences to
prevent heartache.
- :w - Write the file to save the current changes.
This command modifies the time/date stamp on the file.
It is important to save your changes periodically, especially if you
are working remotely (away from the lab) in case there is some
hardware failure or power outage. Try this now on file rhymes.
- :q - Quit vi if no changes have been made.
Make a change to rhymes: Delete a word, insert a
character, whatever. Try the
:q command. "No write since last change
(add ! to override)" will be printed at the bottom of your screen.
The :q command executes only if you have not
modified the file.
- :wq - Write and quit vi. This saves the changes
to the file and exits vi. This command also modifies the
time/date stamp on the file.
Try it now to save your changes and exit the file rhymes.
- :q! - Quit vi without saving changes. Be very
careful with this command since you will lose all your work for
that session if you haven't saved the changes. This command is most
commonly used when you enter vi and misspell the file name.
It does not modify the time/date stamp.
5. Handy List of Common vi Commands
You must be in command mode to enter these commands.
Open the file rhymes again and try each command listed.
Motion:
- move around within text:   use the arrow keys, or h, j, k, l
- move forward one screen:   <Ctrl> f
- move back one screen:   <Ctrl> b
- move to bottom of text:   G
- move to top of text:   gg   or   :1 or   1G
- move to a pattern:   /<pattern>
- move to next instance of the pattern:   n
Inserting text:
- insert before cursor:   i
- insert after cursor:   a
- insert at end of line:   A
- return to command mode:   <Esc>
- delete a character in insert mode:   <Backspace>
Deleting text:
- delete a character:   x
- delete a word:   dw (cursor must be on beginning of word)
- delete a line:   dd
- delete 12 lines:   12dd
- delete 31 words:   31dw
Miscellaneous:
- undo last change:   u
- undo last undo:   <Ctrl> r
- undo all changes to line:   U
- change a word:   cw (followed by the change)
- replace a letter:   r (followed by the new letter)
- save text:   :w (write to file saving changes)
- exit vi (no changes made): :q   (quit, do not save changes)
- save and exit:   :wq
- exit without saving:   :q! (Both dangerous and useful!)
- refresh screen:   <Ctrl> l (the letter l, not the number 1)
Return to this part of the lab and practice the vi commands until you
are comfortable with them.
One way to master the editor is to access the online tutorial.
On the cs102 web page in the menu on the left, move your cursor over the line labeled "Other Links" (a menu will pop up),
and click on "vim tutorial." You can take a peek now, but
continue with the lab now while you have TA help. Plan to return
to the tutorial before the next lab.
6. Solving a Problem on the Computer
Once you understand a problem and how to solve it, you will follow these
basic steps.
- Design an algorithm to solve the problem
- Follow your algorithm to write your C++ program
and use an editor to enter it into a file
- Trace your C++ code until you are convinced it is correct;
modify it as necessary
- Compile the program
- Correct any errors and recompile
- Execute or "run" the program
- Type in any required input
- Examine the output to make sure it is reasonable
- Document (comment) your code
If the program contains syntax or parse errors (errors in the
form of the
program), error messages will appear on the screen when you attempt to
compile it. Read them! Note the line numbers, and remember that
the command <number> G takes you to the line in number.
(Also, :<number> does the same thing.)
Sometimes, the error is actually on the line above.
You must correct these errors before you proceed.
Often the compiler will inform you that your code has many errors when
in fact it has only one or two. Therefore, first fix any error listed
as a parse error, which will most likely be a syntax error.
Then recompile your program. Often many (or all)
errors will disappear.
7. Editing and Compiling a Program
- Move to a window on the right. List your files (ls)
and notice the file hello.cpp. The letters cpp
following the file name is called an extension and suggests to
the computer that the file may be written in C++. Your C++ programs
will have the .cpp extension.
- Move to the left window and type vi hello.cpp to
open the file. Read it and use the editor to add your name in the
header at the top of the file. The cw command is useful here.
- Recall that a compiler is a program that translates your source code
into code the
computer can understand. We are using the g++ compiler (and
linker) in CS 102. Move to a different window and try to compile
hello.cpp by typing
g++ hello.cpp
A list of error messages will now be displayed on the screen. There
is something very wrong with the program, which is what these error
messages are all about. In this case, the compiler says that there is
a problem with operator <. One < was
omitted in the cout statement. Once the error is fixed, the
compiler will recognize the program as valid.
- Move back to the left window, which still displays the contents
of the file hello.cpp. Use your editor to correct the error.
The corrected line of code should look exactly like this:
cout << "Hello, world.\n";
- Again, activate the right window and attempt to compile hello.cpp.
Do not panic: If you get the same error
this time, it is because you have followed the directions exactly.
Think about what happened. You corrected the error but did not save
the change. Save the changes and recompile.
- When your program compiles correctly, the Linux prompt will appear
with no error messages displayed. If error messages appear, return to
your editing window and repeat the required steps until the program
successfully compiles. Ask for assistance if necessary.
- In your right window, use the ls
command to view the contents of your Lab1 directory again.
How have the contents of your directory changed?
- Once your program has compiled with no errors, it will produce the
a.out file that contains your
program compiled into machine language.
This is the file you execute (run). To do so, at the Linux prompt
type
- Once that your program is correct, exit vi.
8. More vi Practice
Now we want you to use vi for real. You copied three files called
file1, file2, and file3 that you will edit in any one
of your windows. Important: The
changes you make to these three files will be inspected electronically and
graded automatically by the computer. This means that you must follow the
instructions exactly. You may close the file with :q!
at any time to start over. When you are satisfied, exit with :wq.
- Use the dw or x command of vi to delete every word in the
second line of file1 (leaving a blank line). Next, insert the word "deep" between "In"
and "Hyperspace" on line four. Make no other changes to
file1.
- Use dd to delete the third line
of file2. Use dw to delete the word "little" from line
one. Use x to delete the apostrophe from "he's". Make
no other changes to file2.
- Use the pattern search command /hydrogen to find
"Hydrogen" in file3. Use cw to replace "Hydrogen"
with "Helium" by typing cw Helium <Esc>. Make no other changes to
file3.
- File fortunes contains fortunes that have been
mangled by deleting the last 8 characters of many of the fortunes.
Use as many different vi commands to add words to
the ends of the lines (or elsewhere) to make the fortunes make sense
(perhaps impossible!). Be creative, and have fun.
It makes no difference how you change the file fortunes since it will
not be graded. The point is to take some
time to become more familiar with vi.
9. Punch and Run -- To be Graded
Below is a complete C++ program that is correct in both syntax and semantics.
Use the vi editor to open a file called coins.cpp and type in the code
below exactly as shown. When you are finished and are convinced there
are no typographical errors, save the file with the :w command.
Note: Spacing is ignored by the compiler for the most part, and
comments are always ignored. Don't be overly concerned with the spacing,
but get it close.
/*****
Determine the monetary value of an assortment of coins.
input: number of pennies, nickels, dimes, and quarters,
in order
output: the value of the coins in dollars and cents
*****/
#include <iostream>
using namespace std;
int main()
{
int pennies, nickels, dimes, quarters, cents;
double value; // total amount of money in coins
cout << "\nEnter number of pennies, nickels, ";
cout << "dimes, quarters\n";
cin >> pennies >> nickels >> dimes >> quarters;
/* Compute the total value in cents. */
cents = pennies + 5*nickels + 10*dimes + 25*quarters; // STUDY THIS
/* Convert to dollars and cents, then display. */
value = cents / 100 + cents % 100 * 0.01;
cout << "The value of your coins is $" << value << ".\n";
return 0;
}
|
After saving your changes using the :w command, move to a different window and
compile the program with the command g++ coins.cpp. If error
messages appear on the screen, you have typed something incorrectly.
Read the error messages to help you locate the error, then
make your corrections. Use :w
to save your corrections, recompile, and repeat the process
until the program compiles correctly (seek assistance if necessary).
Once you get the Linux prompt without error messages, run the
program with
the command ./a.out and type in the requested input. Check the
output to convince yourself that the code is correct since it will be
graded electronically. Run it several times with different inputs.
For fun, try negative input values. Do negative values make sense?
What about a value of zero?
Once you are finished, close the file. Return to it later to study the
code. You should understand what it does and how it does it.
10. Write a Temperature Conversion Program
To convert temperatures from Fahrenheit to Celsius and back again, you
use the following formulas:
c = 5 * (f - 32) / 9
f = 9/5 * c + 32
Write a program called temp.cpp which prompts the user for a
temperature, and then outputs two lines. The first line assumes that
the temperature input is in Fahrenheit, and converts it to
Celsius. The second line assumes that the temperature that was input is
in Celsius, and converts it to Fahrenheit.
You should assume that the input and output temperatures are doubles,
but beware:   9/5 is integer division.
11. Robot Kit
You should have received a robot kit from a TA. If you somehow did not,
now would be a good time to ask a TA for one.
Your kit should have the following contents:
- 1 Scribbler Robot
- 1 fluke
- 1 flashlight
- 1 bluetooth dongle
12. Connecting to the Robot
Unpack your robot kit, and plug in the USB bluetooth dongle.
Then plug the fluke into the port serial port on the robot and turn the
robot on. In a terminal type
rfcomm connect /dev/rfcommKitNumber
,your robot kit should be number, so (KitNumber) should be replaced with
the number on your robot kit. You should be prompted with a pop up
in the upper right corner requesting a pass key for pairing with
the robot. Click the button that says enter pass key, and type
and hit enter. Your robot should now be paired with the computer you
are using, and capable of writing programs to use it. You may be required
to pair the robot multiple times through out the semester, so remember
this process.
13. Programming the Robot
We are providing some sample code for programming the robot. Type
this command in a terminal:
cp ~cs102/robot_labs/Lab_Skeletons/Lab1/* ~/cs102/Lab1/
Now you should have a file called Lab1Driver.cpp and a file called
makefile. Typing make will compile the Lab1Driver.cpp file
for you. All of your labs will have a makefile included, but don't
forget how to compile programs using just g++.
1. Now open Lab1Driver.cpp with vi.
2. Add the following line of code to the file:
Where Nemo is replaced by any name of your choosing. The name
must be surrounded by quotemarks, e.g., robot->setName(Nemo);
would not work. If you wanted to print your robot's name, you
could use a line of code like this:
cout << "Hello, my name is " << robot.getName() << "!\n";
3. There are several ways to move your robot around. You'll use others
later, but for now try adding some (or all) of the following commands
to the file.
1. robot.forward(amount,seconds) - move forward, stopping any
rotation, for the specified number of seconds.
ex: robot.forward(1, .5);
2. robot.backward(amount, seconds) - move backwards, stopping
any rotation, for the specified number of seconds.
ex: robot.backward(1, .5);
3. robot.turnLeft(amount, seconds) - turn left, stopping any
forward movement, for specified number of seconds.
4. robot.turnRight(amount, seconds) - turn right, stopping any
forward movment, for specified number of seconds.
5. robot.stop() - stops all movement.
Feel free to adjust any of the values to see different behavior.
(Note: in each method, amount should be a value between 0 and 1.
Seconds should be a positive value.) Later, you'll learn to use
the robot's sensors and other functions, but for now, just
experiment with basic movement.
and follow the prompts. Careful: Your lab section will be listed in the
choices; this may be different from the lab number (this is lab number 1).
The first time, you might want to ask one of the TAs for help. For more
information, read the man (manual)
pages for submit. Type man submit to open the manual page for
the submit script in the more (similar to less program. When
you are finished viewing the manual page, type q to quit.
When you are ready to leave the lab, you must log off the system.
It is of utmost importance that you log off properly.
Otherwise, it will be easy for someone else to use your
account, possibly maliciously. To log out properly, you should: