Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 

README.md

4: Errors!

Try to compile those examples and see how the compiler responds.

Try to compile those examples and see how the compiler responds. Try thinking of a few more errors yourself and try those.

Test this program with a variety of values. Print out the values of area1, area2, area3, and ratio. Insert more tests until all errors are caught. How do you know that you caught all errors? This is not a trick question. In this particular example, you can give a valid argument for having caught all errors.

To see what an uncaught exception error looks like, run a small program that uses error() without catching any exceptions.

Our hexagon was regular with 2cm sides. Did we get that answer right? Just do the “back of the envelope” calculation. Take a piece a paper and scribble on it. Don’t feel that’s beneath you. Many famous scientists have been greatly admired for their ability to come up with an approximate answer using a pencil and the back of an envelope (or a napkin). This is an ability – a simple habit, really – that can save us a lot of time and confusion.

Estimate those driving times. Also, estimate the corresponding flight times (using ordinary commercial air travel). Then, try to verify your estimates by using appropriate sources, such as maps and timetables. We’d use online sources.

Find a pair of values so that the precondition of this version of area holds, but the postcondition doesn’t.

Drill

Below are 25 code fragments. Each is meant to be inserted into this “scaffolding”:

#include "PPP.h"

int main()
try{
    <<your code here>>
    return 0;
}
catch (exception& e){
    cerr << "error: " << e.what() << '\n';
    return 1;
}
catch (...) {
    cerr << "Oops: unknown exception!\n";
    return 2;
}

Each has zero or more errors. Your task is to find and remove all errors ineach program. When you have removed those bugs, the resulting programwill compile, run, and write “Success!” Even if you think you have spotted an error, you still need to enter the (original, unimproved) program fragment and test it; you may have guessed wrong about what the error is, or there may be more errors in a fragment than you spotted. Also, one purpose of this drill is to give you a feel for how your compiler reacts to different kinds of errors. Do not enter the scaffolding 25 times – that’s a job for cut and paste or some similar “mechanical” technique. Do not fix problems by simply deleting a statement; repair them by changing, adding, or deleting a few characters.

Cout << "Success!\n";

cout << "Success!\n;

cout << "Success" << !\n"

cout << success << ’\n’;

string res = 7; vector<int> v(10); v[5] = res; cout << "Success!\n";

vector<int> v(10); v(5) = 7; if (v(5)!=7) cout << "Success!\n";

if (cond) cout << "Success!\n"; else cout << "Fail!\n";

bool c = false; if (c) cout << "Success!\n"; else cout << "Fail!\n";

string s = "ape"; boo c = "fool"<s; if (c) cout << "Success!\n";

string s = "ape"; if (s=="fool") cout << "Success!\n";

string s = "ape"; if (s=="fool") cout < "Success!\n";

string s = "ape"; if (s+"fool") cout < "Success!\n";

vector<char> v(5); for (int i = 0; 0<v.size(); ++i) ; cout << "Success!\n";

vector<char> v(5); for (int i = 0; i<=v.size(); ++i) ; cout << "Success!\n";

string s = "Success!\n"; for (int i = 0; i<6; ++i) cout << s[i];

if (true) then cout << "Success!\n"; else cout << "Fail!\n";

int x = 2000; char c = x; if (c==2000) cout << "Success!\n";

string s = "Success!\n"; for (int i = 0; i<10; ++i) cout << s[i];

vector v(5); for (int i = 0; i<=v.size(); ++i) ; cout << "Success!\n";

int i = 0; int j = 9; while (i<10) ++j; if (j<i) cout << "Success!\n";

int x = 2; double d = 5/(x−2); if (d==2*x+0.5) cout << "Success!\n";

string<char> s = "Success!\n"; for (int i = 0; i<=10; ++i) cout << s[i];

int i = 0; while (i<10) ++j; if (j<i) cout << "Success!\n";

int x = 4; double d = 5/(x−2); if (d=2*x+0.5) cout << "Success!\n";

cin << "Success!\n";

Name four major types of errors and briefly define each one.

What kinds of errors can we ignore in student programs?

What guarantees should every completed project offer?

List three approaches we can take to eliminate errors in programs and produce acceptable software.

Why do we hate debugging?

What is a syntax error? Give five examples.

What is a type error? Give five examples.

What is a linker error? Give three examples.

What is a logic error? Give three examples.

List four potential sources of program errors discussed in the text.

How do you know if a result is plausible? What techniques do you have to answer such questions?

How do you test if an input operation succeeded?

Compare and contrast having the caller of a function handle a run-time error vs. having the called function handle the run-time error.

When is throwing an exception preferable to returning an “error value”?

When is returning an “error value” preferable to throwing an exception?

Describe the process of how exceptions are thrown and caught.

Why, with a vector called v, is v[v.size()] a range error? What would be the result of calling this?

What is an assertion?

Define precondition and postcondition; give an example (that is not the area() function from this chapter), preferably a computation that requires a loop.

When would you not test a precondition?

When would you not test a postcondition?

What are the steps in debugging a program?

Why does commenting help when debugging?

How does testing differ from debugging?

What is a random number?

How do we use random_int() and seed()?

Exercise 1

If you haven’t already, do the TRY THIS exercises from this chapter.

The following program takes in a temperature value in Celsius and converts it to Kelvin. This code has many errors in it. Find the errors, list them, and correct the code.

double ctok(double c)   // converts Celsius to Kelvin
{
    int k = c + 273.25;
    return int
}

int main()
{
    double c = 0;   // declare input variable
    cin >> d;   // retrieve temperature to input variable
    double k = ctok("c");   // convert temperature
    Cout << k << '/n' ; // print out temperature
}

Absolute zero is the lowest temperature that can be reached; it is -273.15C, or 0K. The above program, even when corrected, will produce erroneous results when given a temperature below this. Place a check in the main program that will produce an error if a temperature is given below -273.15C.

Do exercise 3 again, but this time handle the error inside ctok().

Add to the program so that it can also convert from Kelvin to Celsius.

Write a program that converts from Celsius to Fahrenheit and from Fahrenheit to Celsius. Use estimation (§4.7.1) to see if your results are plausible.

Quadratic equations are of the form ax2 + bx + c = 0. To solve these, one uses the quadratic formula, There is a problem, though: if b^2- 4ac is less than zero, then it will fail. Write a program that can calculate x for a quadratic equation. Create a function that prints out the roots of a quadratic equation, given a, b, c. When the program detects an equation with no real roots, have it print out a message. How do you know that your results are plausible? Can you check that they are correct?

Write a program that reads and stores a series of integers and then computes the sum of the first N integers. First ask for N, then read the values into a vector, then calculate the sum of the first N values. For example:

Please enter the number of values you want to sum: 3
Please enter some integers (press ’|’ to stop): 12 23 13 24 15 |
The sum of the first 3 numbers ( 12 23 13 ) is 48

Handle all inputs. For example, make sure to give an error message if the user asks for a sum of more numbers than there are in the vector.

Modify the program from the previous exercise to write out an error if the result cannot be represented as an int.

Modify the program from the previous exercise to use double instead of int. Also, make a vector of doubles containing the N-1 differences between adjacent values and write out that vector of differences.

Write a program that writes out the first N values of the Fibonacci series, that is, the series that starts with 1 1 2 3 5 8 13 21 34. The next number of the series is the sum of the two previous ones. Find the largest Fibonacci number that fits in an int.

Implement a little guessing game called (for some obscure reason) “Bulls and Cows.” The program has a vector of four different integers in the range 0 to 9 (e.g., 1234 but not 1122) and it is the user’s task to discover those numbers by repeated guesses. Say the number to be guessed is 1234 and the user guesses 1359; the response should be “1 bull and 1 cow” because the user got one digit (1) right and in the right position (a bull) and one digit (3) right but in the wrong position (a cow). The guessing continues until the user gets four bulls, that is, has the four digits correct and in the correct order.

The program is a bit tedious because the answer is hard-coded into the program. Make a version where the user can play repeatedly (without stopping and restarting the program) and each game has a new set of four digits. You can get four random digits by calling the random number generator random_int(0,9) from PPP_support (and §4.7.5) four times. You will note that if you run that program repeatedly, it will pick the same sequence of four digits each time you start the program. To avoid that, ask the user to enter a number (any number) and call seed(n), also from PPP_support, where n is the number the user entered before calling random_int(0,10). Such an n is called a seed, and different seeds give different sequences of random numbers.

Read (day-of-the-week,value) pairs from standard input. For example:

Tuesday 23 Friday 56 Tuesday −3 Thursday 99

Collect all the values for each day of the week in a vector<int>. Write out the values of the seven day-of-the-week vectors. Print out the sum of the values in each vector. Ignore illegal days of the week, such as Funday, but accept common synonyms such as Mon and monday. Write out the number of rejected values.