Introduction to Computer Programming Lab: Debugging and Piecewise Functions in C - Prof. A, Schemes and Mind Maps of Computer Science

This lab exercise provides a comprehensive introduction to debugging techniques in c programming using codeblocks ide. It guides students through the process of identifying and fixing syntax and logical errors in code. The lab also explores the implementation of piecewise functions in c, demonstrating how to handle different conditions within a single program. Students will gain practical experience in debugging, code optimization, and applying mathematical concepts to programming.

Typology: Schemes and Mind Maps

2021/2022

Uploaded on 10/23/2024

aznia-patrick
aznia-patrick 🇵🇰

3 documents

1 / 19

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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
COMSATS University Islamabad
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13

Partial preview of the text

Download Introduction to Computer Programming Lab: Debugging and Piecewise Functions in C - Prof. A and more Schemes and Mind Maps Computer Science in PDF only on Docsity!

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

COMSATS University Islamabad

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

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.
  1. 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

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 { R = R ***** x ; // multiply R with x and store result in R.

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𝑛, 𝑛 ≤ −

𝑓[𝑛]

𝑛^3 − 50𝑛, 𝑛 ≥ −

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 - 6 - 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 ;

Corrections Made:

Line 8: printf (“ ; if (n<=-10)

Line 9: printf”); output = (nn)-(5n);

Line 10: printf”); else if ((n>-10)&&(n<-2))

Line 12: N”); else if (n>=-2)

Line 13: printf (“ output = (nnn)-(50*n);

Line 14: printf (“ %d\n,”output)

14 22. 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; }