Lab 11

Debugging with the IDE

This lab covers basic fundamentals of the debugging procedure. It describes how to debug an application using three IDEs: Visual Studio(VS), Xcode and Dev-C++.

You will be given a program with different kinds of defects in it. The objective of this lab is to use the debugging techniques to identify the root cause of the defects, and fix the defect to make the program behavior as expected. 

Tutorials for debugging techniques with Visual Studio(VS), Xcode and Dev-C++ are listed. Before the lab, you are expected to read the tutorial on the IDEs. During the lab, take screenshots of the IDE when you first realize where the defect lies using the debugging techniques ( e.g. debugging with Breakpoints, using Watch Windows to check the value of local variables and so on ). When you check out, show your screenshots and your bug-free program to the TA.

To check out your lab, submit this form with your name and UIN information.

For all tasks, try to make as few modifications as possible to make the program work. 

Task 1: The program tries to read a string of numbers from the user input and prints out the number as an integer. 

#include <stdio.h>  int main(void) { int sum = 0, i = 0; char input[5];  while (1) { scanf("%s", input); for (i = 0; input[i] != '\0'; i++) sum = sum*10 + input[i] - '0'; printf("input=%d\n", sum); } return 0; }

Expected output is

123 input=123 67 input=67

while we got:

123 input=123 67 input=12367

Task 2: The program initializes the variable str as "hello". Then, it tries to initialize a variable reverse_str with "" which has the same length as str. The program prints out str, stores characters in reverse order in reverse_str and prints out reverse_str. 

#include <stdio.h>  int main(void) { int i; char str[6] = "hello"; char reverse_str[6] = "";  printf("%s\n", str); for (i = 0; i < 5; i++) reverse_str[5-i] = str[i]; printf("%s\n", reverse_str); return 0; }

Expected output is:

hello olleh

While we got:

hello

Task 3: The function add_range sums up the value from low to high and in the main function, the program tries to calculate the sum from 1 to 10 and from 1 to 100 respectively. Two values calculated are stored in an array. 

#include <stdio.h>  int add_range(int low, int high) { int i, sum; for (i = low; i <= high; i++) sum = sum + i; return sum; }  int main(void) { int result[100]; result[0] = add_range(1, 10); result[1] = add_range(1, 100); printf("result[0]=%d\nresult[1]=%d\n", result[0], result[1]); return 0; }

Expected outputs are:

result[0]=55 result[1]=5050

While we got:

result[0]=55 result[1]=5105