DEV Community

Cover image for Debug and find logical errors in the C program using dev c++ IDE
Madhawa Awishka
Madhawa Awishka

Posted on

Debug and find logical errors in the C program using dev c++ IDE

Debugging is the process of detecting and removing potential logical errors (bugs) in a computer program

Example

Write a c program to input marks earned for the exam and the marks earned for the homeworks to calculate and display the overall course score.Students can obtain 50% percent from their exam and 50% percent from their homeworks.
score1=exam score*(50/100)

score2=homework score*(50/100)

course score=exam score+homework score

This is the c program source code to the above question

/*This program calculates the overall score using given the points
 earned for the exam and homework*/

#include <stdio.h>

int main()

{

int examScore, homeworkScore;

float score1, score2, overallScore;

printf("Please enter the points earned for the exam : ");

scanf("%d", &examScore);

printf("Please enter the points earned for homework: ");

scanf("%d", &homeworkScore);

score1 = examScore * 50 / 100.0;

score2 = homeworkScore * 50 / 100.0;

overallScore = (score1 + score2) / 2;

printf( "Overall course score is %.2f", overallScore );

return 0;

}
Enter fullscreen mode Exit fullscreen mode

Step 01:

open Dev c++ and write the above code

Image description

Step 02:

save and compile the program.

if your program does not have any errors and warnings excute the c program.Run your program with the following data set

The points earned for the exam:90

The points earned for the homework:60

Image description
if you have calculated your overall course score manually,the score should be 75.Here your expected output and program output is diffrent.it means,there is/are logical error/s in our C program.

Step 03:

Now, you can use the debugging option to find logical errors in the program.

Go To Tools →compiler option
Image description
Step 04:

Set breakpoint in C program
A breakpoint is a point in the program where you want the execution to stop temporarily so that you can examine the values of variables.

Image description

Start debugging

Image description

Click on Debug button, then your program will be executed up to line no. 8

Step04:

Add a watch on a variable

Image description

Then a pop up box will appear.You can give a name of a variable that you are going to add a watch.Let’s give “Examscore” variable as a first add watch.Then click “OK” button.Add watches on other variables and check their values. (homeworkScore,score1, score2, overallScore).

Step 05:

input values to the variables

Currently we are in line number 9.The program is executed up to line no. 8.To excute next C statement in the line number 10,click on the next line button(It is next to add watch button).

Then line number 10 will be excuted and you can see a output as “Please enter the points for the exam:”To excute the next line press “Next line” button again.Line number 10 will be excuted and now you can input the points for the exam as 90.Then press Enter button on your keyboard.

Like this you can input value for the homework score as 60.Now, you can see that the variable values of examScore and homeworkScore are changed to 90 and 60.

Image description
Step 06:

Observe outputs

When you excute line number 15 you can see score 1 as 45.

It means that this calculation is correct, and it takes 50% of exam marks.
There is no logical error in this calculation.

To execute next statement in line no. 16, click on Next line button again.
Then, line no. 16 will be executed and score2 value is calculated.
30 is stored inside score2 variable. It means that this calculation is correct, and it takes 50% of exam marks.There is no logical error in this calculation

To execute next statement in line no. 17, click on Next line button again.
Then, line no. 17 will be executed and overallScore value is calculated.
37.5 is stored in overallScore variable. You can see that this calculation is incorrect, and it doesn’t calculate overall score correctly.
So that, there can be a logical error in this calculation in line no. 17.

Image description

Step 07:

Fix logical errors

To fix the logical error, debugging process should be stopped using Stop Execution button.
Image description

When we observe the statement in line number 17

Overallscore=(score1+score2) / 2

Here, we don’t need to divide the addition of two scores by 2 since we need to take overall score out of 100.
That’s the error that we have done. Now, you can modify the statement as bellow.

Overallscore=(score1+score2)

Click on the line number of the relevant statement which includes the break point to remove it.

Compile and run the program and see whether the program works as expected. If you can’t get the expected output, there may be more logical errors.
Then you need to debug your program again to identify those logical errors.

Top comments (0)