Kamran Icp Lab Report

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Introduction to computer programming

LAB REPORT 1

Name KAMRAN AZIZ


Registration Number FA22-BEE-105
Class 2-C
Submitted DR INAM ULLAH
LAB ASSESMENT

Pre- In- Post Lab Total


Lab Lab
Data Data Writing
Presentation
Analysi Style
s
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 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.

In-Lab

Task 1: Fix syntax errors in given C program.


Make a new C project (console application) using Code Blocks and type the
following code in the main.c file. Build the code, fix the indicated errors,
and run it.

Program for addition


#include <stdio.h>

int main()

int num1, num2, sum;

printf("Enter the first number: ");

scanf("%d", &num1);

printf("Enter the second number: ");

scanf("%d", &num2);

sum = num1 + num2;


printf("The sum of %d and %d is %d", num1, num2, sum);

return 0;

PROGRAM WITHOUT ERRORS:

#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

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 than or equal to 1

printf("The factorial of %d is %d\n\n",N, R); // Output R to console

return 0;

PROGRAM BUILT AND RUN:

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]

-20 500 0 0

-19 456 1 -49

-18 414 2 -92

-17 374 3 -123

-16 336 4 -136

-15 300 5 -125

-14 266 6 -84

-13 234 7 -7

-12 204 8 112

-11 176 9 279

-10 150 10 500

-9 10 11 781

-8 11 12 1128

-7 12 13 1547

-6 13 14 2044

-5 14 15 2625

-4 15 16 3296

-3 16 17 4063

-2 92 18 4932

-1 49 19 5909

20 7000

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


Debugger
Type and build the following code in a new Code Blocks 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;

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 ", output);

printf("\n\n");

return 0;

Post Lab:-
Modify the above C program for the following piecewise function and report the
problems you face. Use integer variables for your program.

#include <stdio.h>

#include <stdlib.h>

int main()

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<3)

{ output =-(n+4);

} else if((n>=3)&&(n<=10))
{ output =n*n-7;

} else if(n>10)

{ output = (120/n)+(n);

} printf("%d ", output);

} printf("\n\n");

return 0;

PROGRAM BUILT AND RUN:

Critical Analysis:
First lab demonstrate us about the basics of C Programming,
which includes analysis and running codes on Code Block, we
have learned how to write a simple program on code block,
secondly this lab has improved my knowledge to deal with errors
during compiling a program. We studied about breakpoint and
debugger, which is responsible to identify and fix errors in our
codes. We practiced setting breakpoints, stepping through code,
and inspecting variables in the debugger.

After using all these tools, some errors appeared in my program.


The lines with error were highlighted by a red dot at start. Then
by using standard form, I have corrected the program. In
conclusion we have worked on operation like addition (+),
subtraction (-), multiplication (*), and division (\)of the functions
and I also cross checked the errors in my program.

CONCLUSION: This lab was a useful introduction to


the development tools, basics of C programming, and debugging
techniques. We learned how to set up the development
environment, write basic C programs, and debug code using the
debugger. These skills will be valuable for future programming
projects and will help us to become more proficient in C
programming

You might also like