COMP2113 – ENGG1340 Computer Programming II Solved

$ 29.99
Category:

Description

COMP2113 Programming Technologies Module 9 Checkpoint Exercises

Name: Shaheer Ziya

Checkpoint 9.4

To answer all the questions, you need to read the external links in the lecture notes first.

1. Suggest two ways to debug a program.

i. Print useful information (with cout or printf)
ii. Use a debugger like GDB

2. A student wants to use the GDB debugger to debug a C++ program but the debugger fails to tell which line of the code causes the error. Identify the problem and suggest a solution to fix it.
$ g++ -std=c++11 main.cpp -o main
$ ./main
Segmentation fault (core dumped)
$ gdb main
(gdb) r
Starting program: /home/research/ra/1801/cklai/main

Program received signal SIGSEGV, Segmentation fault. 0x00005555555547d2 in main ()

A segmentation fault means that an illegal memory location was attempted to be accessed. This has probably something to do with uninitialized variables or NULL pointers. Setting up breakpoints in the program or just using the backtrace command to pin-point where the error occurs can help debug the issue.

3. Give one advantage and one disadvantage of inserting cout statements to debug a program.

i. Simple & easy to do especially if it’s a one-time thing
ii. Makes the program cluttered and can forget to remove the cout statements.

4. What is a breakpoint in the GDB debugger? How do you set a breakpoint to a program?

It is a point at which the programs halts/pauses program execution. It can be set by typing b or break followed by the line number at which you want to set the breakpoint on, in the GDB. For example break 8 will set the breakpoint at line 8 in the program.

5. GDB debugger is used to debug the following program. Suppose a breakpoint is set to line 12. When the program runs, it will pause at line 12. What are the values of n1 and n2 when the program pauses at the first time? Fill in the blank.

$ cat gcd.cpp #include <iostream> using namespace std;

int main()
{ int n1 = 32; int n2 = 8;

while(n1 != n2)
{
if(n1 > n2)
n1 -= n2; // This line is line 12 else
n2 -= n1;
}
cout << “GCD = ” << n1; return 0;
}

$ g++ -g gcd.cpp -o gcd
$ gdb gcd
(gdb) b 12
Breakpoint 1 at 0x870: file gcd.cpp, line 12.
(gdb) r
Starting program: /home/research/ra/1801/cklai/gcd

Breakpoint 1, main () at gcd.cpp:12
12 n1 -= n2;
(gdb) p n1
$1 = 32
(gdb) p n2
$2 = 8

6. Suppose a breakpoint is set to a certain line in a for loop in a program. The program pauses at that breakpoint when it runs on the debugger. Instead of entering the ‘n’ command multiple times, what command allows the program to resume execution and to pause at the same breakpoint in the next iteration?

The continue command or just c in the gdb console with resume execution until the next breakpoint is hit. In a loop the execution starts from the top of the loop in each iteration, so the continue command will execute the code until the same breakpoint is hit in the loop.

7. The following C program is to find the sum of Natural Numbers but it contains error(s). Fix the error(s) by modifying the code.
#include <stdio.h>
int main() { int n; int i; int sum = 0;

printf(“Enter a positive integer: “); scanf(“%d”, &n);

for (i = 1; i <= n; ++i) { sum += i;
}
printf(“Sum = %d”, sum); return 0;
}

Reviews

There are no reviews yet.

Be the first to review “COMP2113 – ENGG1340 Computer Programming II Solved”

Your email address will not be published. Required fields are marked *