Computer Science 1621
Computer Science I

Laboratory Assignment 7
Debugging C programs (5 points)
Due at the end of Lab

Introduction

The purpose of this lab assignment is to familiarize you with debugging programs.

First we will see how to debug programs by use of the printf statement. Then we will look at debugging with the Turbo C++ debugger.

The Program

Enter and compile/run the following C program:


#include <stdio.h>

int main() {
  int maximum(int x, int y);
  int minimum(int x, int y);
  void prn_info(void);
  float div(int max, int min);
  int max,min,x;
  float result;

  prn_info();

  printf("Input 4 integer numbers: ");

  scanf("%d",&x);
  /* Check x */
  /* printf("Integer 1 = %d\n",x); */
  /* Insert breakpoint 1 after this line  */
  max = x;
  min = x;

  scanf("%d",&x);  
  /* Check x */
  /* printf("Integer 2 = %d \n",x); */
  /* Insert breakpoint 2 after this line  */
  max = maximum(max,x);
  min = minimum(min,x);
  /* Check max, min */
  /* printf("Current max = %d\n",max); */
  /* printf("Current min = %d\n",min); */
  /* Insert breakpoint 3 after this line */

  scanf("%d",&x);  
  /* Check x */
  /* printf("Integer 3 = %d \n",x); */
  /* Insert breakpoint 4 after this line  */
  max = maximum(max,x);
  min = minimum(min,x);
  /* Check max, min */
  /* printf("Current max = %d\n",max); */
  /* printf("Current min = %d\n",min); */
  /* Insert breakpoint 5 after this line */

  scanf("%d",&x);  
  /* Check x */
  /* printf("Integer 4 = %d \n",x); */
  /* Insert breakpoint 6 after this line  */
  max = maximum(max,x);
  min = minimum(min,x);
  /* Check max, min */
  /* printf("Current max = %d\n",max); */
  /* printf("Current min = %d\n",min); */
  /* Insert breakpoint 7 after this line */

  /* step over till next line and then trace into function div*/	
  result = div(max,min);
  printf("Result %d\n",result);
  return(1);
}

int maximum(int x, int y){
  if (x > y) 
    return x;
  else 
    return y;
}

int minimum(int x, int y){
  if (x < y) 
    return x;
  else 
    return y;
}

float div(int max, int min){
  float result;

  result = max / min;
  /* Check result */
  /* printf("div result = %f\n",result); */
  /* step over to next line and display the value of result */
  return(result);
}

void prn_info(void) {
  printf("This program reads in 4 integer values, then determine the max and\n");
  printf("min values of the numbers entered.  Finally it divides the maximum\n");
  printf("by the minimum and prints the result.\n");
}
  

Running the program

When you run the program type in the 4 integers 6, 15, 3 and 2. You won't get the right answer.

Debugging with printf statements

Now lets look at what the problem is. We can begin by looking at the variables we read in like the two integers to see if we read them in correctly. We could insert printf statements in to see what the variables are. Remove the comment marks in the printf statements under Check x and recompile and run the program with the same input. You see that the values read in are correct. You can comment out these printf statements as we know the problem is not in reading the data.

Next we should see whether the max and min values are being computed correctly. So again we could use printf statements to show us the values of max and min. Remove comment marks in the printf statements under Check max, min and recompile and run the program with the same input. The max and min values printed out are what you expect. So the program runs correctly till here. Comment out these printf statements.

So now we know that the problem is in the calculation and printing out of the result. Remove the comment marks in the printf statement under Check result and recompile and run the program with the same input. At this point we see a problem, result is wrong. The problem is we are dividing integers and storing the result in a float and losing the fraction. Change the line

max / min
to
(float) max / min
Fixing this the result printed out during div is correct, but the final result is still wrong. Since the result in div is correct the problem must occur while trying to print the result out (the only thing left). We notice that we are trying to print out the result as integer rather than as a floating point value. So we change the %d to %f. Recompile and run the program. Now you get the correct result.

Debugging with the Turbo C++ debugger

Now reintroduce the errors we corrected and try debugging with the Turbo C++ debugger. Start by running the debugger.

Setting Breakpoints

To control where your program stops running you will have to set breakpoints. The simplest way to set a breakpoint is with Debug-Toggle-Breakpoint or F5 key. Insert a breakpoint after the line labeled Insert breakpoint 1 by pressing F5. Turbo debugger highlights the line, indicating there is a breakpoint set on it. Now select Debug-Run to run your program. The program stops at the highlighted line.

Displaying the values of the variables

You can display the values of the variables as the program is running. This can be done by choosing Debug-Add-Watch and typing in the name of the variable. Type in x. You see that the value is correct for x. Terminate the program and remove the breakpoint by moving the cursor to that and choosing Debug-Toggle-Breakpoint. Now insert a breakpoint after the line labeled Insert breakpoint 2. Choose Debug-Run to run the program again. The program stops at this line. To see the value of the second value for x choose Debug-Add-Watch and display the value as before. It has the correct value too. Next add breakpoints after all of the lines labeled Insert breakpoint. After each breakpoint examine the values of the variables x, min, and max. Once you have checked the results at a breakpoint choose Debug-Step-Over to move on the next breakpoint.

Tracing Into Functions

To check the computation of the variable result we have to trace into function div. Set a breakpoint at the call of function div. Now use the feature Debug-Trace Into and continue the program. The debugger will stop at the first line of div. Set a breakpoint in this function after result is calculated. Now display the value for variable result. It comes out incorrect again. Make the change we made before and recompile. Now the result during div is correct but the final result is still wrong. Remove all of the other breakpoints by toggling the breakpoints and add a breakpoint when result is printed out in main. Checking the value of result we see it is correct, but prints out incorrectly. Correct the error in the printf statement as before and recompile and run the program again. The result is now correct.

Experimenting with the Turbo C++ debugger

Try using different inputs for the program and displaying variable values with the debugger. Also try tracing into functions and displaying the values.

What to turn in

Turn in hard copies of the source code with all errors corrected, the values of the variables you got when you displayed them during debugging and hand in the final output of the debugged program. Also hand in outputs of the debugger displaying variable values when you ran other test cases of input data.