lab1_report
Post on 08-Apr-2018
214 Views
Preview:
TRANSCRIPT
-
8/7/2019 lab1_report
1/14
Laboratory course Programming language C++
LABORATORY ASSIGNMENT 1
Report
Lab Objectives
To gain experience with
the activity of programming
using your compiler
the debugger
strategies for effective debugging
Lab check
Please check your status of assignment:
Title Content Cant do it Finished
Pre-lab assignments The Pascal triangle OK In-lab assignments 1. Compiling your file
Debugger
Post-lab assignments Your IDE
1 | P a g e
Name: L HU DUYClass: 08DT2Group: 15BNo.: ????Session: Wed
-
8/7/2019 lab1_report
2/14
Laboratory course Programming language C++
Pre-Lab.
The Pascal trianlge
Entries in Pascal's triangle are indexed by integers. n is the number of the row and k is
the position from the leftmost member of the row. The indexes in both directions start at
zero (0), so in the fifth row listed above, C(4,0) = 1, C(4,1) = 4, and so on.
The values themselves are computed by the formula C(n, k) = n! / ( k! (n-
k)!), which is called a combination. n! denotes afactorial, that is, the product n(n-
1)(n-2)...(2)(1). The combinations can be interpreted as the number of ways to
choose k elements from a collection containing n elements. When described, it is
customary to say "n choose k", for instance '4 choose 3 is 4' ".
If 4 objects are numbered 1 through 4, how many ways can you select 2 of them ? Show
all the possible pairings here. Then compute C(4, 2) by using the formula. Does it match
the number of selections?
Your answer
From {a,b,c,d} we have : {a,b} , {a,c} , {a,d} , {b,c} , {b,d} ,
{c,d}
As you will discover with the help of your debugger, the program to compute Pascal's
triangle has some mistakes. Each provides an opportunity to learn a debugging concept in
context of its use.
2 | P a g e
-
8/7/2019 lab1_report
3/14
Laboratory course Programming language C++
In-Lab 1.
Compiling and running programs from within your development environment
Frequently in these labs you will be asked to compile a sample program. Below is a copyof a C++ program that displays a drawing. Copy and paste it into your compiler's editor,
and from there save it as art.cpp.
Describe what you did.
Your answer open Code::Block ->file->New->File-> C/C++ emptysource -> next ->c++ ->next -> enter file diriection: D:\project\C++\LAB\art.cpp -> next. Paste codes. SAVE.
#include #include using namespace std;int main()/* PURPOSE: Display an 'art' drawing*/{ string s1 = " * * * * * * ";
string s2 = " * * * * * ";string s3 = "__________________________________\n";string s4 =
"_________________________________________________________\n";
cout
-
8/7/2019 lab1_report
4/14
Laboratory course Programming language C++
environment. Find out the steps for your computer system, then go ahead and
compile art.cpp to an executable file.
Describe what you did.
Your answer. Press build button (ctrl +F9)
Finally, execute the program. Once again, the steps depend on your computer system.
Describe what you did to execute the program.
Your answerPress Run ( Ctrl + F10)
Describe what happened when the program executed.
Your answer
In-Lab 2.
The Pascal trianlge
The following portion of the lab is designed to have you practice some of the basics of
debugging. Copy the following program listing into a new file called pastri.cpp on your
hard drive.
4 | P a g e
-
8/7/2019 lab1_report
5/14
Laboratory course Programming language C++
/******************************************************************** PROJECT: Lab assignment on debugging* FILE: pastri.cpp* PURPOSE: To compute Pascal's triangle and illustrate* the use of the debugger.* PROGRAMMER: Leslie Foster
* REMARKS: The code below is chosen to illustrate the debugger.* It is neither the most efficient nor the mostreliable* way to solve this problem.*************************************************************************/
#include #include using namespace std;
/*------------------------------------------------------------------*/
void skip(int n)/* PURPOSE: To skip n spaces on a line
RECEIVES: n - the number of spaces to skipREMARKS: n should be non-negative
*/{ int i; /* a counter */
for (i = 0; i
-
8/7/2019 lab1_report
6/14
Laboratory course Programming language C++
/* PURPOSE: to calculate the number of combinations of n things takenk at a time (n choose k)
RECEIVES: n - the number of items to choose fromk - the number of items choosen
RETURNS: n choose kREMARKS: n and k must be non-negative and k nrows;cout
-
8/7/2019 lab1_report
7/14
Laboratory course Programming language C++
The program's purpose is to display Pascal's Triangle, a sequence of integers that arises in
numerous areas of math and computer science, especially combinatorics and probability.
When completed, the program's output should be:
Enter the number of rows (
-
8/7/2019 lab1_report
8/14
Laboratory course Programming language C++
Then set breakpoint at the line int main(void)
Use the Step Into command to restart the program from the very beginning.
Now use the Step Into command several times, until the active line is
cin >> nrows;
Select Step Into again
What happens with the program? Does the Step Into take effect on the following line of
the program?
Your answer : The program permit user to enter the number of rows
Type 5 (followed by the Enter key) for the input. Press Step Into again.
On what line is the cursor positioned now ?
Your answer At D:\project\C++\LAB\pastri.cpp:74
Select your compiler's Memory examine in Debug Windows.
Inspect the value of nrows
How can you do to see value of nrows?
Your answer: debug/debuging windows/Memory examine
What is the value fornrows shown in the dialog box?
Your answer0x22ff0c: 05 00 00 00 70 34 41 00|68 24 2a 00 30 00 00 00....p4A.h$*.0...0x22ff1c: 00 a0 fd 7f 00 a0 fd 7f|00 00 00 00 68 ff 22 00
More stepping
Select Step Outseveral times.
Look at the output for Pascal's triangle.
Does the output have the title: TABLE 1: THE FIRST 5 ROWS OF PASCAL'S
TRIANGLE ?
8 | P a g e
-
8/7/2019 lab1_report
9/14
Laboratory course Programming language C++
Your answer Nothing appears
That is strange. The program is supposed to print three newlines, then the title. Let's find
out why the title doesn't appear.
Select Start the to start the debugger once again.
Select Step Into
Then select Step Outuntil you reach the line prompting for input.
Enter 5 for nrows
Select Step Out one more time. This should skip over the
line cout
-
8/7/2019 lab1_report
10/14
Laboratory course Programming language C++
Does the title now show up on the screen?
Your answer YES
How many rows of Pascal's triangle are displayed?
Your answer 3
To find out why there are not five rows, return to the source code by pressing
enter.
Select Watches
The variable n is undefined (a random number) because you haven't yet started the
program.
Select Step into several times.
How many lines are now in the watch window?
Your answer : Local variables ; Function Argument;
Resetand Step Into.
Select Step Outuntil you reach the line
skip(spaces_to_skip); /* space to make a triangle */
What values of n and k are in the watch window? (If the watch window isn't presentselect View Watch.)
Your answer n= 0k=4273328
Keep selecting Step Outuntil you are again at the
line skip(spaces_to_skip); /* space to make a triangle */
What are the values of n and k?
Your answer n=2k=1
10 | P a g e
-
8/7/2019 lab1_report
11/14
Laboratory course Programming language C++
Keep selecting Step Outuntil you exit the for loops and arrive at the line
cout
-
8/7/2019 lab1_report
12/14
Laboratory course Programming language C++
What value does producthave in the inspect window?
Your answer product =4
The reason that productin the inspect window is incorrect is that the line product = product + i;
should be
product = product * i;
Make the above change in your editor.
Recompile
Start andRun the program again with input of 5. Oh no! the triangle is still
wrong!
Setting breakpoints
It is tedious to have to keep stepping to get inside a program. A more efficient way is to
set a breakpoint at a line of interest. Then the program runs at full speed until a
breakpoint is reached.
Startand make sure that n and k are still in the watch window.
Select the line comb = combination(n,k) ;
SelectAdd Breakpoint.
SelectRun until the program stops at the breakpoint. Repeat selectingRun until the watch window displays
n: 3
k: 1
Then Step Into once
and then Step Outuntil you are at the line: return comb;
Select Watches
and follow combfor the expression in the dialogue box.
What is the value listed forcomb?
Your answer
The true value for the combination of 3 things taken 1 at a time is 3! / 1!2! = 6 / 2
= 3. So, the above value ofcombis wrong.
12 | P a g e
-
8/7/2019 lab1_report
13/14
Laboratory course Programming language C++
Something must be wrong with our combination calculation. In fact, the
programmer has omitted parentheses in the denominator for the line: comb = factorial(n) / factorial(k) * factorial(n-k) ;
Change the line to be: comb = factorial(n) / (factorial(k) * factorial(n-k)) ;
Recompile and selectResetandRun
Is your Pascal's Triangle correct (for nrows = 5)?
Your answer
Finally
Select the line comb = combination(n,k) ;
Select Breakpoint.
SelectRun
Do the results look OK when you input 13 for the number of rows?
Your answer
They should now. Exit, saving any files you might wish to keep.
13 | P a g e
-
8/7/2019 lab1_report
14/14
Laboratory course Programming language C++
Post Lab.
Menu commands and shortcut keys for debuggers
The following table lists commands for debuggers, find out your debuggers commands
and fill in the table to the right.
Feature Button or short keys
Run Ctrl+F10
Step out Shift+Ctrl+F7
Step into Shift+F7
Output screen
Start/ResetWatches
View watches
Breakpoint
14 | P a g e
top related