1. Debugging a meaningful code in DevC++:
Single-stepping through the code & Watching variables change values:
Often it is desirable to see what effect each statement in the code creates and whether it is desired or erroneous. If a statement is written using correct syntax but is semantically wrong the code may not function as desired.
1. Debugging:
Such mistakes, often termed bugs, cannot be caught by the compiler because a compiler cannot know the purpose of a statement/task. As you can see such mistakes can even accidentally happen without any malintention, for example, by a typo. The process to locate and rectify such mistakes is known as debugging. It requires indepth analysis of the code and the effect a code section or statement has on the performance of the code (output) and comparing it with the desired effect of a statement.
Most IDEs provide features to help debugging. DevC++ has such features too. We explore two of them here, viz., single-stepping and watch.
In single-stepping we execute one step (statement or line) at a time and see its effect on values of variables in the code through watch window.
2. Set DevC++ for Debugging:
The default setting of the DevC++ IDE does not allow debugging a code since it is set in the release mode. Release mode is meant to create the smallest sized and most optimized exe file for the code. Whereas in debug mode we are not worried about size or optimization, we just wish to be able to tap into the code at various locations and stop the execution wherever we wish to and see how code internals are at that point. For this the compiler has to tap in extra code into our code (not visible to us) to allow this feature.
Do the following steps to enter the debug mode:
1. Click Tools --> Compiler Options:
Select TDM-GCC 4.9.2 64-bit Debug (or 32-bit Debug based on your computer, whether it is 64 bit or 32 bit)
2. In the General tab, check the checkbox 'Add the following commands when calling the compiler'. Write -g in both the editable fields, as shown below.
3. Click Settings Tab --> Click Linker Tab:
Select Yes after Generate debugging information (-g3).
4. Click OK.
Now the default setting of your DevC++ IDE is set to the debug mode. If you close it and re-run, you will be in the debug mode.
3. Debugging:
In order to debug, first you need to set a breakpoint. A breakpoint, as the name suggests, marks the statement that breaks the execution before it gets executed so that you can check the state of the program, before that statement, then single-step through it, and check the state of the program after its execution to find how it impacts the state (variable values).
For the code set the break-point. Either click the line number in the line number bar, right click mouse with cursor on any point on line and click Toggle Breakpoint or right click mouse on line and press F4 key.
Recompile the code to be sure that the latest version of the code has been compiled in debug mode.
Now click Execute -->Debug or press F5 key to run the code in debug mode.
If the debug mode is entered, instead of the black console window, the focus will get to the line of the code with a blue arrow pointing towards the line on the number bar and the whole line background will turn to blue and code text will turn to white.
The execution has stopped just before line is to be executed.
Click Next line (or press Alt + N) in the lower pane of the IDE.
Line background will become red and the blue arrow and blue background will move to the next line. This means line has just been executed.
In order to know what effect line has on the state of the program, note that on line the variables were created and some were assigned a value of 0. Let us check the values of these varaiables. To do this we add them to watch.
In the lower pane click Add watch and enter n in it and click OK.
Add all the variables to watch window by a similar process:
Note the state of the variables again after pressing Next Line few times.
You can continue debugging the whole code and see how it works.
Note that the graphical depictions may show a different filename.
2. Understanding a DevC++ Code through Debugging:
1.1. Input the following code verbatim in a new file and save it aslab03_1.cpp.
#include <iostream>
using namespace std;
int main()
{
int a, n, t, s = 0, i = 2;
cout << "Enter first term of the sequence: ";
cin >> a;
cout << "Enter number of terms (e.g. 5): ";
cin >> n;
t = a;
s = t;
while(i <= n)
{
a = -a;
t = i * a;
s += t;
i ++;
}
cout << "s = " << s << endl;
return 0;
}
|
Now that you know how to interact with a C++ code as well as how to debug it, put these skills to work to determine what the above code does.
You may wish to run the code in debug mode and track the values of the variables to see what inputs get you which outputs. Then looking at the code and the values, reconstruct the arithmetic series which relates how output and input are related mathematically. It is very important to show all your rough work in the space below to get maximum credit.
For your convenience a table/grid has been made below for you to keep track of the variable values as the code proceeds. Note that repetition of the code can be handled by moving to next column. Some of the starting values are filled for your reference. You should practice filling the grid first by dry running through the code and then debugging and verifying.
Variable grid for dry run (complete it for a =3 and n = 5 as shown below):
Line#
|
i1
|
i2
|
i3
|
i4
|
i5
|
i6
|
i7
|
i8
|
i9
|
i10
|
i11
|
i12
|
i13
|
6
|
s=0, i=2
|
s=0, i=2
|
s=0, i=2
|
s=0, i=2
|
s=0, i=2
|
s=0, i=2
|
s=0, i=2
|
s=0, i=2
|
s=0, i=2
|
s=0, i=2
|
s=0, i=2
|
s=0, i=2
|
s=0, i=2
|
8
|
a=3
|
a=5
|
a=6
|
a=7
|
a=8
|
a=9
|
a=10
|
a=11
|
a=12
|
a=13
|
a=14
|
a=15
|
a=16
|
10
|
n=5
|
n=6
|
n=7
|
n=8
|
n=9
|
n=10
|
n=11
|
n=12
|
n=13
|
n=14
|
n=15
|
n=16
|
n=17
|
11
|
t=3
|
t=5
|
t=6
|
t=7
|
t=8
|
t=9
|
t=10
|
t=11
|
t=12
|
t=13
|
t=14
|
t=15
|
t=16
|
12
|
s=3
|
s=5
|
s=6
|
s=7
|
s=8
|
s=9
|
s=10
|
s=11
|
s=12
|
s=13
|
s=14
|
s=15
|
s=16
|
16
|
a=-3
|
a=-5
|
a=-6
|
a=-7
|
a=-8
|
a=-9
|
a= -10
|
a=-11
|
a=-12
|
a=-13
|
a=-14
|
a=-15
|
a=-16
|
17
|
t=-6
|
t=-10
|
t=-12
|
t=-14
|
t=-16
|
t=-18
|
t=-20
|
t=-22
|
t=-24
|
t=-26
|
t=-28
|
t=-30
|
t=-32
|
18
|
s=-3
|
s=-5
|
s=-6
|
s=-7
|
s=-8
|
s=-9
|
s=-10
|
s=-11
|
s=-12
|
s=-13
|
s=-14
|
s=-15
|
s=-16
|
19
|
i=3
|
i=4
|
i=5
|
i=6
|
i=7
|
i=8
|
i=9
|
i=10
|
i=11
|
i=12
|
i=13
|
i=14
|
i=15
|
1.1. Write the following code in a new file and save it aslab03_2.cpp. Understand the code through debugging.
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
float Aside, Bside, Cside;//variables
cout<<"Enter the length of side a " <<endl;
cin>>Aside;
cout<<"Enter the length of side b " <<endl;
cin>>Bside;
cout<<"Enter the length of side c " <<endl;
cin>>Cside;
if (Aside == Bside&&Bside == Cside)
cout<<"Equilateral triangle\n";
else if (Aside == Bside || Aside == Cside || Bside == Cside)
cout<< "Isosceles triangle\n";
else
cout<< "Scalene triangle\n";
return 0;
}
|
If the user enters same value for all sides, then why does the code skips line 15 to 18? Explain.
The statements inside if parenthesis and its body gets executed only when the given condition is true. If the condition is false then the statements inside else if body are check and if all are false then else body is executed ..
Now if all the values are same then the if statement is true thus the statements inside are executed and the rest are ignored.
Thus when all sides are entered the same . 1st condition if is meet and program skips line 15 to 18.
|
1.2. Now understand the following code (save it aslab03_3.cpp)
#include <iostream>
using namespace std;
int main()
{
int Rows, i, j, Space; //i and j are counters use in for loop
cout<< "Enter number of rows: ";
cin>> Rows;
for (i = Rows; i>= 1; --i)
{
for (Space = 0; Space<Rows - i; ++Space)
cout<< " ";
for (j = i; j <= 2 * i - 1; ++j)
cout<< "* ";
for (j = 0; j<i - 1; ++j)
cout<< "* ";
cout<<endl;
}
return 0;
}
|
Notice the values of the variables at line 13 and note those values in table below.
Iteration #
|
Rows
|
i
|
J
|
Space
|
1.
|
15
|
15
|
15
|
0
|
2.
|
15
|
15
|
16
|
0
|
3.
|
15
|
15
|
17
|
0
|
4.
|
15
|
15
|
18
|
0
|
5.
|
15
|
15
|
19
|
0
|
6.
|
15
|
15
|
20
|
0
|
7.
|
15
|
15
|
21
|
0
|
8.
|
15
|
15
|
22
|
0
|
9.
|
15
|
15
|
23
|
0
|
2. Writing Basic Code in DevC++
Print the following lines on the console window:
· It always seems impossible until it’s done…
· Set the goals high, and don’t stop till you get there…
Program-code:
#include<iostream>
using namespace std;
int main( )
{
cout<<"It always seems impossible until it's done...";
cout<<endl;
cout<<"Set the goals high, and don't stop till you get there...";
return 0;
}
No comments:
Post a Comment