Debugging
You’ve done a program – compiled…
A bug is an error in a program.
Debugging is the process of finding and removing errors from a program.
(why called a bug?)
1945 - Grace Murray Hopper, working on the Mark II computer, a computer failure was traced to a moth that had become wedged between relay contacts. She taped the moth into the logbook – that was the first case of a bug being found.) (trick – grasshopper)
There are various types of errors that a program might have: compilation, execution, and logic errors.
I Compilation Errors: Errors caught by the compiler while it is translating your program into machine language before running it. Compilation errors caused by mistakes in the syntax of a statement are called syntax errors.
Examples:
a = (b + 1 / 3; // missing parenthesis)
x + 3 = 5; (expression on the left of an assignment statement)
x = 5 // missing semicolon
(for spelled fir)//fir(i=1; i< 5; i++) (misspelled)
Linker Errors – errors discovered in the second phase. Phase is handled by the linker – the program that connects your program with the standard libraries. If you use a library function, the compiler looks for it in the standard library during the link phase of copilation. If it doesn’t find the function (either it’s misspelled or doesn’t exist), error message.
Ex:
Cuot << num; //misspelled
II Execution Errors or run-time errors: Not caught by the compiler. Detected once the program is running. Ex. suppose that your program attempts to divide by a variable whose value is zero. The compiler will not object to the expression, but the computer will not be able to execute it. Your program will normally stop at the error and display and error message.
Execution errors are more serious than compiler errors and can cause your machine to hang or not give off error messages.
III Logic Errors
Hardest to catch. Each statement may be valid in C++ and acceptable to the compiler. The program may run without causing any execution error, but the answer may be wrong.
Ex. initializing a variable to 1 instead of 0, multiplying by 2 instead of 3, or using an incorrect formula. The mistake may be hard to find since it won’t generate an error. You have to catch this kind of error by testing the program on all possible sets of data.
Also, many statements that look like compilation errors are actually valid c++ statements and the compiler won’t think it’s an error:
Ex. x = = x+3;
Even if you meant to use the assignment operator.
Another example is previous program / 3.
Arithmetic Operators
Addition +
Subtraction
Multiplication *
Division / (is integer division if operators are integers)
Modulus % (remainder)
e .g., if the value of
is to be assigned to variable x, it is coded:
x = b + c - d * e / f;
Parentheses may be used. These are evaluated first. e.g.,
x = (b + c - d)* e / f; is evaluated as:
order of operations
( )
* / %
+ -
left to right
ex. z= p*r%q+w/x-y; Order of operations?
Z
|
=
|
P
|
*
|
R
|
%
|
Q
|
+
|
W
|
/
|
X
|
-
|
Y
|
;
|
|
6
|
|
1
|
|
2
|
|
4
|
|
3
|
|
5
|
|
|
Operator Precedence
Operator
|
Associativity
|
Type
|
( )
|
L R
|
parenthesis
|
++ – – + – !
|
R L
|
unary operators
|
* ? %
|
L R
|
multiplicative
|
+ –
|
L R
|
additive
|
<< >>
|
L R
|
insertion
|
< <= > >=
|
L R
|
relational
|
= = !=
|
L R
|
equality
|
&&
|
L R
|
and
|
||
|
L R
|
or
|
?:
|
R L
|
conditional
|
= += –= *= /= %=
|
R L
|
assignment
|
Exercise: For each of the following arithmetic expressions, construct the equivalent C++ expression.
___(a + b) / (c – d)_____________
____(a+b) / (c * c)________________
_____d – (a + b) / (4 * c)_____
______(b*b – 4*a*c) / (2 * a)___________
______a / (b + c / (d – a))_________
A Simple Program Using Stored Data and Arithmetic
// from Schaum's ex 1.26 p. 27
// Prints the sum, difference, etc. of given integers.
#include
using namespace std;
int main(){
int m = 6, n = 7;
cout << "The integers are " << m << " and " << n << endl;
cout << "Their sum is " << (m+n) << endl;
cout << "Their difference is " << (m-n) << endl;
cout << "Their product is " << (m*n) << endl;
cout << "Their quotient is " << (m/n) << endl;
cout << "Their remainder is " << (m%n) << endl << endl << endl;
system(“pause”);
return 0;
}
Trace this program.
The integers are 6 and 7
Their sum is 13
Their difference is -1
Their product is 42
Their quotient is 0
Their remainder is 6
Press any key to continue . . .
Assignment
c = c + 3; same as c +=3;
The += operation adds the value of the expression of the right to the value of the variable on the left and stores the result in the variable on the left.
In general, = op ; can be written as: op = ;
Examples:
c = 4; same as c = c 4;
c *= 5; same as c = c *5;
c /= 6; same as c = c /6;
Can we reverse the order of the double operator? e.g.: c = 4;
No. This simply is the same as the assignment c = 4;
Share with your friends: |