Overall Tips

Problem 1

Tips

Problem 2

Explanation

Tips

Problem 3

Problem 4

Overall Tips

  • All programs should use or add exception handling as specified on page 150 in Chapter 5 section 5.6. (You may add additional catch clauses if you wish.)
  • Photo for Exercise 12 on page 169
  • Initialize variables when defining them.
  • Name your file in format hw*pr*.cpp

Problem 1

If you get no clue how to do this, please refer to these two lectures and the Chapter 7 in your book. But you'd better note you have refer to these materials. There is no difference in grading policy whether you read these materials or not.

If you really want to challenge yourself, you can try through the following steps.

  • Firstly, you should read through the code. List all the structure, classes and functions. Understand the dependency between functions and understand what each function and each line is doing.
  • This might not be done at one time. You might be puzzled at a few point, you need to make assumption and then check whether the assumption holds.
  • When changing the code, you should comment the original code. Because you might misunderstand the code and change the code in a wrong direction.
  • When fixing the compiling error, you should check the error in the front firstly. The later error might be fixed by correct the front errors.
  • After you can compile the file into executable files, you will try to use it. You can try the following operation:
  • operation of integers on +, -, *, /, %
  • operation of double on +, -, *, /, %
  • parentheses ()
  • variable definition and use
  • quit
  • If some operation failed, you need to follow the code (as the code is running) based on the input and find the line with logic error.

Tips

For problem 1, it is suggested that you write detail steps about how you debug the program (What are problems met, how you fix it).

Problem 2

Explanation

This problem will give you a brief understanding of testing. You have a piece of code for a goal. You will treat this part of code as a black box and test whether the code will give right output given any input test case.

A simple example, I have a piece of code for calculating the area of rectangle

double area = width+height;// This is not right, area should be width*height

What I expect to get from given input is

w h a

3 3 9

3 4 12

4 3 12

4 4 16

But I will right a program to test whether the results I get from the testing code meet my expectation

int main()

{

double width=0;

double height=0;

for(auto width:{3,4})

{

for(auto height:{3,4})

{

double area = width+height;// This is not right, area should be width*height

cout < width < '\t' < height < '\t' < area < endl;

}

}

}

The results I get is

3 3 6

3 4 7

4 3 7

4 4 8

At this point, I will know this code is not working rightly.

This corresponds to the requirement of the problem:

For input 0,0
a and b are not logically equal
For input 0,1
a and b are logically equal
For input 1,0
a and b are not logically equal
For input 1,1
a and b are logically equal

Tips

the return value of assignment operation is the value of b, which will be used for the bool check of if statement

Problem 3

Error : Unable to open X server.If you are on Windows, you should install Xming and start it firstly, then Putty. Check the "Enable X11 forwarding" box on Putty.

Problem 4

  • The problem requires using vector.
  • I suggest you use the rand function to initialize the four digits.

Use the randint function defined at the end of std_lib_facilities_4.h and on page 879. Be sure you get four random numbers between 0 and 9 inclusive to put into your vector, not 0 to 8 or 0 to 10. To allow different games to be played instead of the same four numbers every time, add the following code at the beginning of main:

cout< "Input seed: ";

unsignedint s;

cin> s;

srand(s);

If you enter the same number (say, 1) then you will get the same four random numbers, but if you enter another seed (say, 13579) then you will get a different set of numbers.