Introduction to Unix and C++
CS 210 In-Lab 1
January 21, 2004
To do
this assignment, you should be signed onto your esus account. Your TA will explain
at the beginning of class how he wants you to submit your materials to him.
Purpose
The purpose of this lab is to make sure that everyone can:
- Get up and running using your account on the Linux
operating sytstem on a computer in EPS254,
- Answer questions about the use of basic Linux (or Unix) commands.
- Use an text editor (emacs, vi, or pico) on Linux.
- Write and compile a simple C++ program.
What to do
- To begin:
- Each person should work individually on this assignment.
- Be sure your computer is running Linux. If not, reboot into Linux.
- Log in to your account
- First, you will experient with text editors, and decide which
one you want to use.
- Next, you will run a Unix tutorial, then answer some questions, and
experiment with Unix.
- Finally, you will use the text editor to write a simple C++
program, compile it, then run it.
Text Editors
Unix has several text editors you may use. The simplest to use is probably
pico but emacs and vi are more powerful, although
may take longer to learn to use.
Start up the editor of your choice, and use the help feature to learn to use it.
- $ emacs & (for the emacs editor)
- $ vi & (for the vi editor)
- $ pico & (for the pico editor)
(The emacs family has some nice features for programming, but you can
use another text editor if you like.)
You will use the editor later in the assignment, first to answer some
questions, then to write a simple C++ program.
Unix
There are many Unix tutorials available on the World Wide Web. Go
to this one, bookmark
it, and then take some time to see what is available, then read
at least the first two sections and then answer the following questions.
- Type ls. What happens and why?
- How is ls -l different than simply ls?
- Explain how to make a directory called cs210. (Do it.)
- Explain how to make cs210 your working directory. (Do it.)
- From your cs210 directory, describe three separate ways to go back to your
home directory.
- From your cs210 directory, show the exact directory structure from the
root of esus to your 210 directory.
- Navigate our Unix system enough to estimate the number of students with
esus accounts. Explain how you derived your estimate.
Write, compile and run a C++ program
- When you have completed the above exercises, go to your
cs210 directory and create a subdirectory called inlab1.
- Go to the inlab1 subdirectory and use a text editor to create a file called
lab1.cpp. Write a C++ program that prompts the user for the width,
height, and depth of a box and then prints out its volume. Assume that all
numbers are integers. A sample run is shown below.
Box volume program.
Enter the width of the box: 2
Enter the height of the box: 3
Enter the depth of the box: 4
The volume of the box is 24.
- Compile your program by typing g++ lab1.cpp
- Run your program by typing a.out.
Using Vectors and File I/O
CS 210 Lab 2
January 28, 2004
Lab due in one week: at the beginning of lab February 4, 2002
Purpose
The purpose of this lab is to gain experience
- Writing functions
- Using strings and the vector container,
- Getting input from a file, and sending output to a file.
What to do
Your assignment is to do #7 on page 34 of the Weiss text, inputting
the strings from a file you will create. Specific instructions
are below.
Using vectors
- Write a function that accepts a vector of strings as an argument
and returns a another vector that contains only the strings that have the longest length.
- In other words, if there are ten strings that are
tied for being the longest length, the return value is a
vector of size ten containing those longest strings.
- Then test your function in main( ) by reading in an abitrary
number of strings(from a file), call the
function, and output the strings returned by the vector.
Using file input/output
There are notes with examples on the lecture
from Tuesday, January 27, but here are the important points.
- For input from a file you must create an object to use with the
extraction operator instead of cin
which accepts input from the
standard input device (usually the keyboard). To create this object
- Put #include < fstream>
at the beginning of your file
- Declare an ifstream file object to connect to the disk file
with this statement
ifstream fileIn ("c:\\filename.dat")
- fileIn can be any variable
name, and the string inside the parentheses is the name and
location on disk of the data file.
- The ifstream object is then used just exactly like
cin except the input will
come from a file, not from the keyboard.
- For output to a file you must create an object to use with the
insertion operator instead of cout
which writes to the screen. To create this object
- Put #include < fstream>
at the beginning of your file
- Declare an ofstream file object to connect to the disk file
with this statement
ofstream fileOut ("c:\\filename.dat")
- fileOut can be any variable
name, and the string inside the parentheses is the name and
location on disk of the data file.
- The ofstream object is then used just exactly like
cout except the output will
be to a file, not to the screen
- Your program should:
- have a function that creates the data file.
- have another function that reads from the data file and puts
the strings into the first vector.
- have the function that accepts a vector of strings as an argument
and returns a another vector that contains only the strings that have
the longest length.
- These three functions should be called from main( ). Use prototypes
so that the first function in your program is main( ).
Hand in:
- Have your source code ready to hand in at the beginning of lab next week.
- Be ready to demo your program at the beginning of lab next week.
Working with Linked Lists
CS 210 Lab 3
February 4, 2004
Lab due in one week: at the beginning of lab February 11, 2004
Purpose
The purpose of this week's lab is to gain experience working with pointers.
First you will read through some C++ code until you understand what it is doing,
drawing diagrams to help you understand, then modify the code.
What to do
You will be working with a
linked list class. The linked list is a circular, doubly linked list with
a dummy header node. Before beginning the lab, download the following
file List3.cpp. Open it, and
familiarize yourself with the code in the file. Then do at least the first part
in the lab today. You should have the TA or a consultant check your diagram
before you leave the lab.. Do not be concerned if it takes you a long time to
understand the code.
It is not especially easy, and is really difficult without diagrams.
When you finish with your diagrams, start on the modifications
Understanding the code
- On a sheet of paper, walk through Initialize( ), drawing the new
node created and the pointers.
- Then add each node using Insert( ),
reassigning the pointers as directed by the code.
- Draw a new diagram for each new insertion. You should have
four diagrams, each with one more node than the former. Keep this paper to hand
in; it should be reasonably neat, showing how four nodes in the linked
list are connected.
Modifying the code
- Change the Insert( ) method so that it does not allow an element
that is already in the linked list to be inserted again.
- In this case an appropriate error message should be printed.
- You will have to change main to add the new elements. You can hardcode
the input for this exercise if you want, or you can make it more elegant.
- Change the Insert( ) method so that it keeps the elements in
sorted order as they are inserted
- Add an method called Print_Backwards( ) that prints the elements
in the linked list in reverse sorted order. Make sure that you don't print
out the dummy header record!
- Add an method called Remove ( ) that removes an element (if it
exists) from the linked list. Remove should print a message stating whether
or not the removal was successful.
Hand in:
- Be ready to demo your code in lab next week.
- Hand in a printout of your modified code, and your diagrams.
Working with Arrays
CS 210 Lab 4
February 11, 2004
Lab due in two weeks: at the beginning of lab February 25, 2004
Purpose
The purpose of this lab is to gain experience working with double-subscripted arrays,
and arrays of C-strings.
What to do
Download the code for the card shuffling program that we went over
in lecture. Compile and run it so that you can see what it does.
As we said in class, this code is quite inefficient. You will
need to modify the shuffle and deal functions to make them more
efficient, then deal and alalyze some poker hands.
Modify the shuffle function
- As it now stands, the shuffle function can lead to indefinite postponement.
- It may take almost forever to find the empty spot to put the 52nd card.
- A better way
- Initialize the deck so that it is initialized as shown below.
|
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
| 0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
| 1 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
| 2 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
| 3 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
- Modify the shuffle function to loop through every element in the array,
swapping with a randomly selected element of the array.
- Test the resulting array to be sure your shuffle function is working well
- You may want to shuffle several times before dealing
Modify the deal function
- As it now stands the dealing function locates and deals a card, then it
continues searching though the remainder of the deck.
- This is very inefficient.
- A better way:
- Modify the function so that once a card is dealt, no further
attempts are made to match that card number, and the
function immediately proceeds with dealing the next card,
if one is to be dealt.
Deal and analyze some poker hands
- Modify the program so that the card dealing function deals a five-card poker hand.
- Then write functions to accomplish each of the following:
- Determine if the hand contains a pair
- Determine if the hand contains two pair
- Determine if the hand contains three of a kind
- Determine if the hand contains four of a kind
- Determine if the hand contains a flush (all five cards one suit)
- Determine if the hand contains a straight (five cards of consecutive face values)
- Modify main to deal a hand, then output the cards in the hand,
and tell what it contains (i.e. a pair, etc.)
Extra credit
- Modify the program so that it deals two five-card poker hands,
evaluates each hand, then determines which hand is better
- Output all the necessay information so that the user can
see what the hands are, and if the evaluation is correct.
Hand in
- Have your source code ready to hand in at the beginning of lab February 25.
- Be ready to demo your program at the beginning of lab February 25.
Design, implement and test a fraction class
The driver (client) of your Fraction class
The client, or driver program should present the user with a menu asking what
they want to do. (The menu itself will depend on how much you decide to implement.)
It should then prompt the user for the numerator and denominator for the fractions.
Then it should echo the input, and output the answer. The answer should be a fraction
reduced to lowest terms.
The sum of 1/2 and 1/3 is 5/6
Extra Credit
- After everyting above is working correctly, you may want to add to your program.
- In main( ), declare an array of ten objects of type Fraction.
- In a loop, ask the user to input values into the ten fractions.
- Find the sum of the ten fractions, and output it in reduced form (you may
use an improper fraction, or a mixed number; your choice).
- Find the largest of the ten fractions in the array. One way to do this
would be to overload the > operator in the Fraction class.
- Your menu will have to be modified to allow the user the choice of using
this part of your program.
Design and implement a phone & address book
Due dates:
March 24: In lab next week you will be expected
to hand in and discuss with the TA (or helper)your design
for the solution of this problem and any problems you are having with
the implementation.
March 31: The implementation will be
due at the beginning of lab. You should be ready to demo your program.
The address book
You should start this assignment by thinking about what features would be nice in
an electronic address book. At a minimum, you should be able to type in a name,
and the program responds with the address and phone number of that person. Also
necessary is the ability to save the names, addresses, and phone numbers etc. to a
disk file, so the names in your address book grows over time. You should think
of other functionality.
What to do
- Before you leave the lab today, you must have the TA or consultant check off
that you have shown them
a list of the functionality you would like to include in your address
book. Discuss with them the pros and cons of different operations.
You can alter this list of functionality later if you want, but you must
start knowing what you want your program to do.
- Design your program. The choice of data structures to use in your program
can make implementation easy or a nightmare, so you need to think about it
carefully, and discuss it with others so you are sure to consider several
options. I am going to make one requirment, and several suggestions.
Requirment:
- You must create a Person class that will encapsulate
- the data members (name, address, phone, etc.) of each object
of type Person in your address book and
- the operations on an object of type Person.
Operations might include outputting, ability to do comparisons
(overload the > operator), accessors and mutators, etc.
Suggestions:
- You are going to need a container to hold the objects of type Person.
You have quite a few choices, among them:
- arrays
- linked lists
- vectors
- other STL containers, if you want to read ahead.
- If you want to output the entire address book, the names should
be listed alphabetically.
- Implement the program.
- Separate your interface and implementation files
- Use exceptions to handle errors.
- Make the menu that allows the user to choose what they want to
do user friendly.
- If you find that you have many functions that deal with manipulating
the array of Persons (or vector, or linked list, or whatever) you
may want to encapsulate them with the array in a class. I
will talk more about this in class, after you get started on the
problem
As you would expect, the more functionality you include in your address
book, and the more friendly the user interface, the better your grade will be.
Each person must work independently, but you definately should discuss with
your classmates different aspects of this program.
Using a Debugger
Due date:
This is a one week lab.: It is due at the
beginning of lab on April 7, 2004
You will be asked to show that
the little program given in this lab works correctly. You can do that either this week,
or at the beginning of lab next week. You should use the debugger to find and correct the errors.
Objectives and Guidelines:
- Runtime errors are notoriously hard to find. A debugger can help you with this
- This lab has two parts: learn about GDB, the GNU debugger, and use the debugger.
Learn how to use GDB
There are at least two sources for you to learn to use the debugger:
-
The GDB users manual
- The help feature within the debugger itself
- You could also do a search on the web to see if you find information more to your liking.
With the debugger manual:
- Read the summary in the GDB users manual, and skim some of the other information.
- The sample GDB session referred to in the manual is debugging a C program.
I will give you instructions below to debug a C++ file.
- Answer the first??? questions below
- Keep the debugger manual window open so you can refer back to it as you
run the debugger
Use GDB
As you work with the debugger, use the GDB manual and the help feature to answer the questions below.
- Copy the file debugMe.cpp.
- Compile the file for debugging by typing:
g++ -g debugMe.cpp -o debugMe at the system prompt.
Make sure you use the "-g" flag for all the program modules.
- Invoke the "gdb" debugger by typing: gdb debugMe at the system prompt
- Run the program.
- Once you invoke the debugger, you can run the program
using the command "run"
- If no breakpoints have been set, it will just run the program until
the program exits, or stops for input.
- Set some Breakpoints
- A break point is a command for the debugger to stop the execution of the program
before executing a specific source line.
- Break points can be set using two methods:
- Specifying s specific line of code to stop in: break debugMe.cpp 9
- Specifying a function name, to break every time it is
being called: break main. This will set a breakpoint
right when starting the program since execution always starts at main
with C++ programs
- After you have invoked the debugger, set a break point at main, then type
run. Now you can try out some other commands, and also use help.
- Stepping a command at a time
- To run the program slowly, step by step, there are two options:
- next
- step
- Use help or the manual to find out what the difference is. There is a question
that asks about it.
- Printing Variables and Expressions
- Type help print to see how to print out the
value of variables at any time as you step through the program
- Find and correct errors in the program
- Use the debugger to find and fix the program so that it runs as the
comments indicate.
Questions to answer
Copy these questions into a text editor, then as you find the answers type them in. You will find
the answers in eithe the debugger manual or using the help function within gdb or both.
- If you want to debug a file named "myErrors.cpp", what do you type to compile it so it can bedugged?
- What do you type to start the debugger on the file above?
- What are breakpoints?
- How can breakpoints be set?
- How can you ask the debugger to let you know the value of variables?
- What is the difference between "next" and "step"?
- What does the list command do?
- What happens if you press "Enter" without entering a command?
- What does the info command do?
- What is a watchpoint?
- How do you delete breakpoints?
- What interesting feature(s) have you found that are not mentioned here?
- How do you exit gdb?
Write a Thesaurus
Due: in two weeks (April 21) at the beginning of lab.
Description
A thesaurus is a dictionary of synonyms. For example, here is a small thesaurus,
with each word followed by its synonym(s):
close near confined
confined cramped
correct true
cramped confined
near close
one singular unique
singular one unique
true correct
unique singular one
The problem you are to solve:
- Write a class that will construct an empty thesaurus using a multimap.
You will need to use the pair class.
Your class should be able to:
- Read data into the thesaurus from a file
- This is a file to start your thesaurus
- This data file has the key word first, then the synonyms
on the same line; all are separated by spaces.
- Let the user add an entry to the thesaurus (an entry is a word and its synonyms)
- Return the synonyms for a word the user types in.
- Add synonyms to a word already in the thesaurus
- Save the modified thesaurus to a file when the user exits
- Write a driver class test all the functionality of your thesaurus. It should
have a menu to let the user decide what to do.
- Using a maltimap will keep this efficient. Assume at some point your
thesaurus may have thousands of entries.
- You may use any of the STL.
C Programming
Due date: next week in lab: April 28, 2004
Objective:
The objective for this week's lab is to get some practice using two C constructs
that are different than the C++ we have been using all semester. These two are:
- C I/O using scanf and printf
- the C string library functions
What to do
Write functions to accomplish the specifications below. You can just put
all this code in main in you want.
C Input and Output
Using C strings
Below are listed six possible functions for you to write. Select two of
the functions, and implement and test them You must use C strings; you can use any of the
library functions from the < string.h> library.
- The most commonly used
functions from that library are strcat, strncat, strcpy, strncpy, strcmp strncmp, strlen.
- Most of these I have talked about in lecture, or you can
look up their functionality and examples on the web.
Write at least two of these functions
- Write stoi (for string to integer), a function that takes a null-terminiated
string containing all digit characters and converts the characters into an integer.
- Write itos, a function that takes an integer and a character array
and converts the integer into characters and places them into a C string.
- Using pointers and avoiding unnecessary local variables, write a function
rmchr, that takes a string and a character as arguments, removing
all occurrences of the character from the string (watch out for sequential occurences
of the character). rmchr should not leave holes in the string.
- Write a function rmstr, that takes two strings as arguments, removing all
occurrences of any characters in the second string from the first string.
Like rmchr, rmstr should not leave holes in the string.
- Write a function strrev to reverse a string in place.
- Write the function stringUpper that takes a string as an argument and converts all of its charactersinto uppercase characters.
Extra credit for writing more than two of these functions.
If you only write two, you cannot use both 1 and 2.
Last lab
What to do
- Fill out a course and instructor evaluation.
- This semester we are using an on-line evaluation system.
Click on the link above, and follow directions. The
instructor evaluation is separate from the course evaluation;
there is a link to it after you submit the course evaluation.
- . We would like to have 100% of the students fill out the form.
It can really help us improve the course in the future, as well as
helping the instructor know what students think they do well, and what could be improved.
- Demo and/or hand in the last lab.