Starting from:

$30

CSC3320- Lab 8: Post lab Solved

Purpose: Learn how to use debugger in gdb to debug a program in

Unix.

 Part 1:

You are given a C program “q1.c” as below. But since there are no enough comments in the program, it is hard to find out the feature of the function foo. So let us trace the execution of the program and find out what foo does. Please follow the steps below and answer the questions accordingly.

Compile “q1.c” with –g option so that we can debug the executable using gdb.
$gcc -o q1 -g q1.c

Lauch gdb for “q1”.
$gdb q1

List the source code of “q1.c” from line 1.
(gdb)list 1

Set a breakpoint at the line of statement “while (num > 0)”. Question: Write your command.
1

Run the program until the first breakpoint. Question: Write your command.
Use display to show the value of rev_num and num at each time when program stops.
 (gdb)display rev_num

(gdb)display num

Run the while loop step by step using command n multiple times.
 (gdb)n

Question: check the value of rev_num and num after each iteration and fill in the table below.

When the program terminates, quit gdb using command q.
(gdb)q

 

Question: Now can you tell what the function foo does?

 Part 2:

You are given a C program “q2.c” as below. This program is used to calculate the average word length for a sentence (a string in a single line):

 Enter a sentence: It was deja vu all over again.

 Average word length: 3.4

For simplicity, the program considers a punctuation mark to be part of the word to which it is attached. And it displays the average word length to one decimal place.

2  

4  

 

8  

2

10

11
 
while((character=getchar()) != \n){
12
 
if(character != ' '){
13
 
if(!space){
14
 
words++;
15
 
space=1;
16
 
}
17
 
letters++;
18
 
}else
19
 
space = 0;
20
 
}
21

22
 
printf("Average word length : %.1f", letters/words);
23

24
 
return 0;
25
}
 
 

However, there are multiple errors in the given C program. Please correct complier errors and use gdb to debug the program and find out the errors.

Question: Please write down the line numbers containing the errors and show how  to correct them.

(Note: you do not need to write down the commands you issued in gdb.)

More products