Skip to main content

Section 16.5 Debugging Practice

Please try to fix all three of the below problems. While these will run in the code window, it may be easier to debug these when running and compiling on the server. Your choice!

Subsection 16.5.1 Fix Me: 1

Activity 16.1.

What is wrong with this program? Add appropriate debug statements to the code to find out.

Compile and run the code below in your own C environment.

#include <stdio.h>

int main (void)
{
   int i;
   int total = 0;
    
   for (i=0; i<=100; i--) {
          total +=i;
   }
   printf("The sum of the numbers from 0 to 100 equals %d.\n", total);

   return (0);
}

Subsection 16.5.2 Fix Me: 2

Activity 16.2.

Whatโ€™s wrong here? Add appropriate debug statements to the code to find out.

Compile and run the code below in your own C environment.

#include <stdio.h>

int main (void)
{
    int total = 0;
    int i = 0;
     
    while (i<=100) {
        total +=i;
    }
    printf("The sum of the numbers from 0 to 100 equals %d.\n", total);

    return (0);
}

Subsection 16.5.3 Fix Me: 3

Activity 16.3.

What is wrong with the following program? Add appropriate debug statements to the code to find out.

Compile and run the code below in your own C environment.

#include <stdio.h>

int main (void)
{
   float sum;
   int i = 0;
    
   sum = 0.0;
   while (sum != 1.0) {
            sum = sum + 0.1;
            i++;
   }
   printf("At the end of the loop the value of i equals %d.\n", i);

   return (0);
}