CS 112b
Spring 2004
Ileana Streinu
Lab 1
Introduction to C++
This lab will be spread over the week on Tu and Th, during the lecture and lab periods.
Read the whole lab before starting working on it, so that you
know what you are required to do by the end of the lab.
Overview
Today you learn the basic routine for writing C++ programs,
compiling them, executing and documenting the execution. You
will do this for every homework and for many labs throughout
the semester.
Make sure you know how to carry through each of these steps, otherwise
seek help from me.
- Log on to Novel, then log on the Unix machine beowulf under Windows,
using ssh, or: Reboot the lab computer to Linux and log on to the class account
directly from there.
- Write your first C++ program, plus: Input/Output in C++, a small exercise
- Use script to record a Unix session and show the
compilation and execution of your program
- Submit your work
- Optional: Pipe in input data to your program from a data file.
Getting Started
This section will help you connect to the Linux server.
In Windows
- Login to the Novell Tree using your student Novell account.
- Launch an ssh window. You do this the way you learned in CSC111, or ask me if
you do not know how. Connect to beowulf.csc.smith.edu.
- When the login prompt to beowulf shows up, enter your 112b-xx
account and password.
- Change the password to something you remember easily and
write it down. I will not be able to help you if you forgt
your password.
- Check your email: you should have received an email from the
class account 112b. If not, please email me at 112b "at" cs.smith.edu
to let me know that you should be added to the class mailing
list.
In Linux
- Turn off all programs and reboot your computer into Linux
mode.
- Log on to the system using your 112b class account.
- Then
use the shell windows to check your email using pine.
If this works, you will have the option of logging on and doing
your work under
any of the two environments (Windows or Linux). I strongly
recommend that you get familiar with working directly under Linux.
Create a directory for each lab and homework
From now on, you will do your work for each lab or homework
in a separate directory (or else things will get VERY complicated
pretty soon). To get started, create NOW a directory named
Lab1 in your class account. In the future, as well as when
doing your homework, do this automatically (i.e. create a new
directory named Labx or Hwx, where x
is the number of the lab or
homework, and work in it). Then change your
working
directory to Lab1.
You do this by using the Unix commands:
mkdir Lab1
cd Lab1
Your first C++ program
Enter your first C++ program
with emacs.
- At the Unix prompt type
emacs first.cpp
- Enter the following text
// Program 1
// simple input/output program.
// Your name here
#include <iostream.h>
main() {
int apples, students;
cout << "How many apples do you want to share? ";
cin >> apples;
cout << "How many people will share these "
<< apples << " apples?" << endl;
cin >> students;
cout << "Each person will get "
<< apples/students << " apples" << endl;
}
- Once the program is typed, type ^X^S to save the file.
(^X^C would save and terminate the editor, but we want to keep
it active.)
- Open another window and compile the program:
g++ first.cpp
- Run it (assuming you didn't have any compilation errors).
a.out
EXERCISE 1:
Once your program works, modify it so that it displays also
the number of apples left over. For example, if you have 10
apples and 3 students, each student should get 3 apples, and there
will be one left over.
Name your source file ex1.cpp.
Skip the second exercise for the time being, and do the Create
a typescript and submit part. I want to make sure everybody
knows
how to create a typescript and that submit works. Then
come back and finish the second exercise.
C++: Reading strings of characters
To read a string of characters, use
cin.get(string,length,delimiter), where length is
the maximum number of characters that can fit in the string, not
including the '\0' terminator, and delimiter is the
character that marks the end of the input.
Here's an example that reads a string and prints it back.
char name[20];
cout << "enter your name: ";
cin.get(name,19,'\n');
cin.get();
The second call to cin.get() reads only one character,
the end-of-line marker that the first call to cin.get() stopped
on, but didn't remove from the input stream.
EXERCISE 2:
Write a program that asks the user for her first
name, her middle initial, and then her last name, in three
different input operations, and then outputs all three of them on
the same line, as in this example:
First name? Marge
Middle Initial? F
Last name? Philips
Hello Marge F. Philips!
Use two strings and a character to store the three pieces of
information.
You will have to be careful when you get the middle initial and
see if you need to get the end of line (eoln) marker
after it or not. You may
have to try
different options and see which one is the solution.
Creating a typescript and submitting
When you are done with these two exercises, use script to
save a copy of the execution.
- At the Unix prompt, type script
- From this point on, until you type exit, everything
that you type in, or that Unix or your programs output to the
terminal screen, will automatically go into a file named typescript
in the current directory.
- You can rename the typescript file, either by giving a
different name to start with, by typing
script -o type1
or by using the Unix command mv to rename afterwards, as
in:
mv typescript type1
- What to do:
after you started script, compile and
execute one program, then exit. Look into the typescript
file with emacs to make sure it looks fine and documents the
compilation and execution of your programs. Name it type1 for ex1
and type2 for ex2.
- When done, submit the programs and the typescripts by
typing:
submit Lab1 filename
where filename is the file you submit (e.g. ex1.cpp or type1).
You are required to submit:
- Two source files ex1.cpp and ex2.cpp
- Two typescript files named type1 and type2
showing the compilation and execution of ex1.cpp and ex2.cpp.
- Note: you will have to type submit four times,
once for each file you submit.
Wrap Up
Check that you have submitted your work. A first step is to look into the
directory where all the files have been copied to. You can do
this from your class account with the command:
ls ~112b/handin/Lab1/112b-xx/
where 112b-xx is your hermite account. You will not be
able to look inside the directory to see what files are in,
but at least you can check
that the directory is there.
The ONLY WAY to make sure that you have submitted all the
requested files is to have make a list and check each item after
you submitted it successfully. If you got an error after submit,
your file may not have been submitted, so you'll have to do it
again until you got no error.
You can always resubmit (by the deadline). The submit
program will ask you if you want to overwrite the old file, so
you have a chance to change your mind.
As a general strategy from now on, I recommend that you
submit the first version that runs satisfactorily (to make sure
you do the submission in time), then if you improve it later,
resubmit.
Optional: Pipe in input data to your program from a
data file
If you are done and want to learn one more trick, then do this
part.
- Run your first program again, and write down all the keys you
pressed to enter input data for it. For example, you might have
entered something like:
6 3
or perhaps:
6
3
- Create (in emacs) a file named data containing EXACTLY these
characters. Check if both formats work (i.e. one line or two lines).
- Now run your program again with the input piped in from this
file, as follows:
a.out < data
- It should work exactly as it did before, when you entered the
data directly from the keyboard. If it doesn't, ask me to look at
it.
- If you did this part, create a third typescript named
type3 showing the execution with the data file piped in,
and submit it together with the other files.