0% found this document useful (0 votes)
13 views19 pages

Ab Report 1

The document outlines a lab assessment for an Introduction to Computer Programming course at COMSATS University Islamabad, focusing on using Code Blocks IDE and MinGW C Compiler for C programming. It includes objectives, tasks for fixing syntax errors, solving piece-wise functions, and debugging code, along with performance evaluations and corrections made. The lab aims to teach students about compiling, debugging, and understanding syntax and logical errors in programming.

Uploaded by

aznia shireen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views19 pages

Ab Report 1

The document outlines a lab assessment for an Introduction to Computer Programming course at COMSATS University Islamabad, focusing on using Code Blocks IDE and MinGW C Compiler for C programming. It includes objectives, tasks for fixing syntax errors, solving piece-wise functions, and debugging code, along with performance evaluations and corrections made. The lab aims to teach students about compiling, debugging, and understanding syntax and logical errors in programming.

Uploaded by

aznia shireen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

COMSATS University Islamabad

Lab: 1
Course: Introduction to Computer Programming (CSC-141)
Instructor: Engr. Imtiaz Ur Rehman
Student:
Reg. Number:
______________________________________
ASSESSMENT
In-Lab Performance: /
Post-Lab:
Data Presentation (4) Data Analysis (4) Writing Style (4)

Total: /

Instructor’s remarks:
Lab # 01 Introduction to Development Tools, Basics of C
Programming and Debugging
Objectives
 Learn to use IDE such as Code Blocks for compiling and debugging computer programs
written in C language.

Software’s Used

 Code Blocks IDE


 MinGW C Compiler
Pre Lab
Get the Code Block IDE and MinGW C Compiler setup files from the lab and install them on
your computer.

Creating a New Project using Code Blocks:


1. File → New → Project
2. Select Console Application
3.Press Next
4.Select option C
5.Enter Project Name and its Path, press Next

6.Press Finish

7.Project is created, double click on Sources and click on main file, now you can modify C code as
per your notion.
Using the Debugger:
1. Debugger tool is employed to check the logical errors (errors occur due to
algorithm transformation to code). They are not detectable by complier.

2. To add a breakpoint, right click on line and click on Add breakpoint. It is the point from
where complier will execute program line by line.
3. Red circle (break point) is added to line

4.To add debugger select View→Toolbars→Debugger

5.Debugger toolbar appears

6.Click on play option it will show yellow triangle, now program is appeared to start
4. Along with a console window appears, it is your output window

5. By clicking on next line in debugger menu output appears line by line. One can easily
detect error.
Introduction to Computer Programming

Performance:

#include <stdio.h>
#include <stdlib.h>

int main()
{
int x, y, r;
printf ("Enter first number:");
scanf("%d" , &x);
printf("Enter second number:");
scanf("%d", &y);
r = x + y;
printf("Sum is : %d/n",r);
return 0;
}
In-Lab
In-Lab Task 1: Fix syntax errors in given C program.
Make a new C project (console application) using CodeBlocks and type the following code in
the main.c file. Build the code, fix the indicated errors, and run it.

#include <stdio.h>
#include <stdlib.h>

int main()
{
int N = 0; // Take a number N.
printf("Enter a number for which you want to find the factorial: \n");
Scanf("%d", &N); // Get input from the user
printf("\nYou entered: %d\n\n", n); // Display what the user entered.

int R = 0; // Take a variable R to hold result


int x = 0; // And another x to count
x = N-1; // Let x equal to N-1
R = N // let R = N
do
{
Performance:

#include <stdio.h>
#include <stdlib.h>

int main()
{
int N = 0; // Take a number N
printf("Enter a number for which you want to find the factorial:\n");
scanf("%d,&N");
printf("you entered: %d\n\n.N");// Display what the user entered
int R = 0; // Take a variable R to hold result
int x = 0; // And another x to count
x = N-1;// Let x equal to N-1
R = N;// Let R=N
do
{
R = R*x;// Multiply R with x and store result in R
x = x-1;// Subtract 1 from x
}while(x>=1);// Repeat above steps from multiplication till x is greater then
equal to 1
printf("The factorial of %d\n N, R\n");// Output R to console
return 0;
}
Corrections Made:
Line 1: int N = 0; //Take a number N
Line 3: scanf (“%d ”, &N); //Get input from user
Line 4: printf (“You entered : %d\n\n,N”); //Displat what the user entered
Line 8: R = N; // Let R = N
Line 12: x = x-1; // Subtract 1 from x
Line 13: }while(x>=1) // Repeat above steps from multiplication till x is greater than or
equal to 1
Line 14: printf (“The factorial of %d\n N, R\n”); // Output R to console

In-Lab Task 2: Solving a piece-wise function


𝑛2 − 5𝑛, 𝑛 ≤ −10
𝑛 + 19, −10 < 𝑛 < −2
𝑓[𝑛] 𝑛3 − 50𝑛, 𝑛 ≥ −2
=
𝗅 𝑛 𝑖𝑠 𝑎𝑛
𝑖𝑛𝑡𝑒𝑔𝑒𝑟
Fill the table using the above equation for values of n from -20 to 20. You will be using this
table to compare the output of the program in the next task and fixing some logical errors.

n f[n] n f[n]
-10 150

-11 176

-12 204

-13 234

-14 266

-15 300

-16 336

-2 92

-3 16

-4 15

-5 14

-6 13

-7 12

10 500

11 781

12 1128

13 1547

14 3444

5 -125

6 -84

7 -7

Performance:

In-Lab Task 3: Finding Logical Errors in Code using Debugger


Type and build the following code in a new CodeBlocks project. Compare the output of the
program with the table in task 1. Find any logical error and write the correct program.

#include
<stdio.h>
#include
<stdlib.h>

int main(void)
{
int range_min = -20;
int range_max = 20;

int n;
Performance:
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int range_min = -20;
int range_max = 20;
int n;
int output;
printf("For the range %d to %d, ", range_min, range_max);
printf("The output of the function is: \n");
for(n=range_min; n<range_max; n++)
{
if(n<=-10)
{
output = (n*n) - (5*n);
}
else if((n>-10)&&(n<-2))
{
output = n+ 19;
}
else if (n>=-2)
{
output = (n*n*n) - (50*n);
}
printf("%d\n", output);
}
printf("\n\n");
return 0;
}
Corrections Made:
Line 8: printf (“ ; if (n<=-10)
Line 9: printf”); output = (n*n)-(5*n);
Line 10: printf”); else if ((n>-10)&&(n<-2))
Line 12: N”); else if (n>=-2)
Line 13: printf (“ output = (n*n*n)-(50*n);
Line 14: printf (“ %d\n,”output)
Critical Analysis / Conclusion

Learned to use IDE, compiling and debugging in C language. I also learned how to write a function, compile it,
and run it to get output. Then I learned what are syntax and logical errors. How can we detect these errors and
correct them to run the program accurately. Also learned how to display or form a program using a piece-wise
function in C language.

Post Lab Task:


Modify the above C program for the following piecewise function and report the

−𝑛 − 4, 𝑛<3
problems you face. Use integer variables for your program.

𝑛2 − 7, 3 ≤ 𝑛 ≤ 10
𝑓[𝑛] 120
+ 𝑛, 𝑛 > 10
= 𝑛

𝗅𝑤ℎ𝑒𝑟𝑒 𝑛 𝑖𝑠 𝑎𝑛 𝑖𝑛𝑡𝑒𝑔𝑒𝑟 𝑣𝑎𝑟𝑖𝑎𝑏𝑙𝑒


n f[n] n f[n]
2 -6

1 -5

-1 -3

-2 -2

-3 -1

3 2

4 9

5 18

6 29

7 42

8 57

9 74

10 93

11 21.9

12 22

13 22.23
14 22.57

15 23

Performance:
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int range_min = -5;
int range_max = 20;
int n;
int output;
printf("For the range %d to %d, ", range_min, range_max);
printf("The output of the function is: \n");
for(n=range_min; n<range_max; n++)
{
if(n<3)
{
output = (-n-4);
}
else if((n>=3)&&(n<=10))
{
output = ((n*n)-7);
}
else if (n>10)
{
output = ((120/n)+n);
}
printf("%d\n", output);
}
printf("\n\n");
return 0;
}

You might also like