CSE 303: Autumn 2006, Assignment 5
Due: Wednesday 15 November 2006, 2:00pm
Updates
- Thursday, November 9: Made a small change to Problem 3 that adds a new way to fix bug2.cpp.
Assignment Overview
This is an individual assignment in which you will write some simple C++ programs, create a C++ class, and debug C++ programs. Begin by downloading hw5.tgz and unpacking it with tar -xvzf hw5.tgz. This contains all the files referred to in this assignment.
1. Vectors, Strings, and Streams
Write the following programs in C++. Whenever possible, you must avoid the use of C style arrays, strings, and output in favor of the vector template, the string class, and cout. You do not need to define any classes to write these programs.
- In a file called repeated.cpp, write a program that examines all of its command line arguments (not including the command itself) to see if any arguments are repeated. If an argument is repeated, print it on standard output on a line by itself. If an argument is repeated more than once, it should be printed only once. The sample solution to the problem is 27 lines of code, not including blank lines. Hint: keep a vector of strings that have been seen, and search through it with each successive argument. You may also wish to create a vector that contains all the arguments, though this is not necessary.
- In a file called matrixcheck.cpp, write a program that reads a 9x9 matrix of integers from standard input and checks that it is in the correct format.
- If input appears to be in the correct format, your program should output nothing and exit with code 0. An example of the correct format can be found in the file matrix9x9.txt that is inluded with the assignment.
- If your program finds a number that is not between 1 and 9 (inclusive), or if your program does not read exactly 81 integers, then it should print an appropriate message to standard error and exit with a non-zero error code. You do not need to check that each line contains 9 integers. Several examples of input with errors that should be caught are also included in the assignment.
- Hints: The sample solution to the problem is 19 lines of code, not including blank lines. The file io.cpp shown in Lecture 16 is very helpful for this problem.
2. Point Class
The file Point.h defines a Point class that will be used in the forthcoming assignments and is a pattern for other classes we will make. The file Problem2Main.cpp makes use of this class. Your task is to create the file Point.cpp that implements the Point class. If you compile your class with the following command...
g++ -g -Wall -o Problem2 Problem2Main.cpp Point.cpp
... then running the command "./Problem 2" should produce the following output...
Distance between points 1 and 2 is 4.47214
Points 0 and 3 are Equal
(0,0)
(1,2)
(-1,-2)
(0,0)
(3,4)
You may implement this class in any way you wish, as long as it meets the specification in Point.h and produces correct output. The following is a description of the 11 methods or functions that you must implement.
- Point() and
Point(int x, int y): The constructors for Point.
- GetX() and GetY(): Public methods for accessing the private member variables x and y.
- SetX(int newX) and SetY(int newY): Public methods for mutating this Point.
- Add(const Point &rhs):Adds a Point to the current Point (modifying the current Point and returning a reference to it). Points are added by adding their x and y member variables.
- Distance(const Point &rhs): Computes the distance between the current Point and another Point. If you don't remember how to compute the distance between two points, look here. You'll probably need to use the square-root function (sqrt) which can be included from the file <cmath>.
- operator==(const Point &rhs): Returns true if the x and y member variables of the current point and another point are the same. This is equivalent to the Java method Equals(), but it can be invoked with "p1 == p2".
- Print(ostream &out = cout): Prints the current Point to an ouptut stream in the format "(x,y)".
- operator<<(ostream &out, const Point &p): This uses the above Print method and does the same thing. It is defined outside of the class so that you may print a point to an output stream with expressions like "cout << p1". (Print is necessary because this method cannot access private member variables.)
This is functionally equivalent to the Java "toString()" method.
Advice
The sample solution to Point.cpp is 70 lines long, including 25 lines that are either blank or contain nothing but comments. In fact, 25 lines in the sample solution are nearly equivalent to lines in Point.h, and it is very reasonable to start working by copying Point.h into Point.cpp and working from there.
3. Debugging C++ Programs
The following four C++ programs, included with your assignment, are all supposed to have the same behavior. All programs should produce no errors or warnings when compiled on attu with "g++ -Wall". All should run on attu, producing no output and returning with error code 5. Each has a flaw, however, that prevents it from doing this. You must identify the flaw and describe it in readme.txt (under the heading "Problem 3" and a sub-heading which is the name of the program). You must also fix the program and turn it in.
- bug1.cpp: This program does not compile. Explain why and fix by making a small change to the function main. Hint: explicit.
- bug2.cpp: This program also does not compile. Explain why and fix either by adding a word to class A or by adding a line that is not contained in main or in the definition of class A.
- bug3.cpp: This program also does not compile. Explain why and fix by changing class B. Hint: Default constructors.
- bug4.cpp: This program crashes when runs with long error message. Explain why and
add two methods to A to fix the problem. Hint: remember the "big three."
4. Extra Credit
Remember the course policy on extra credit. You may do either or both of the following problems.
- The program bugec.cpp, included in this assignment, is similar to those in problem 3, but it is supposed to print "5" by itself on Standard Output and exit with return code 0. There are four nasty bugs in this program that prevent it from doing this. Under the heading "Extra Credit A" in readme.txt, describe these four bugs. Modify the file bugec.cpp to fix these four problems by adding or removing as little code as possible.
- Bug 1 Hint: Add one line outside main or the definition of class A.
- Bug 2 Hint: Add two characters to the A's constructor.
- Bug 3 Hint: Remove one word from class A.
- Bug 4 Hint: Remove one word from the main function.
- This is a very hard problem that can increase your awareness of when objects are created or copied. Create a modified version of the Point class called Pointec, placing the modified version in Pointec.h and Pointec.cpp. Pointec is just like Point, except that it prints messages to standard error when instances of the class are created or copied (as described below). Your primary task is to create a modified version of Problem2Main.cpp called ECMain.cpp that produces the same output but results in far fewer calls to Pointec constructors and copy methods. (You do not need to exercise all the methods of Pointec as Problem2Main.cpp does.) ECMain.cpp should produce exactly the same standard output as Problem2Main.cpp, but the output on standard error should be as follows.
- "Pointec()" should be printed to standard error every time the Pointec Default constructor is called. Problem2Main.cpp would print this twice. Try to modify ECMain.cpp so that this is printed only once.
- "Pointec(x,y)" should be printed to standard error every time Pointec's two-argument constructor is called. (Just print "x" and "y", not the actual values in the Point). Problem2Main.cpp would print this twice. In ECMain.cpp, try to avoid printing this any more times.
- "Pointec(pt)" should be printed to standard error every time the Pointec Copy constructor is called. Problem2Main.cpp would print this 12 times. Try to modify ECMain.cpp so that this is never printed.
- "operator=" should be printed to standard error every time Pointec's assignment operator is called. Problem2Main.cpp would not print this. In ECMain.cpp, you should also try to avoid printing it.
Assessment
Your solutions should be
- Correct C++programs that compile without errors or warnings using g++ -Wall on attu.cs.washington.edu
- In good style, including indentation and line breaks
- Of reasonable size
Turn-in instructions
Use the standard turn-in instructions described here. For problem 1, you should turn in repeated.cpp and matrixcheck.cpp. For Problem 2, you should turn in Point.cpp. For Problem 3, you should make entries in readme.txt and turn in bug1.cpp, bug2.cpp, bug3.cpp, and bug4.cpp.If you do this first extra credit problem, you should turn in bugec.cpp. If you do the second, you should turn in ECMain.cpp, Pointec.h, and Pointec.cpp.