0% found this document useful (0 votes)
168 views20 pages

CC-112L Programming Fundamentals Laboratory 04 Modular Program Development - I

The document provides instructions for a programming lab on modular program development. It discusses break and continue statements, logical and assignment operators, functions, and tasks to complete including using different statements and operators, finding the second largest number, and squaring asterisk patterns.

Uploaded by

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

CC-112L Programming Fundamentals Laboratory 04 Modular Program Development - I

The document provides instructions for a programming lab on modular program development. It discusses break and continue statements, logical and assignment operators, functions, and tasks to complete including using different statements and operators, finding the second largest number, and squaring asterisk patterns.

Uploaded by

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

CC-112L

Programming Fundamentals

Laboratory 04

Modular Program Development – I

Version: 1.0.0

Release Date: 08-08-2022

Department of Information Technology


University of the Punjab
Lahore, Pakistan
CC-112L Programming Fundamentals SPRING 2022

Contents:
∙ Learning Objectives
∙ Required Resources
∙ General Instructions
∙ Background and Overview
o break Statement
o continue Statement
o Logical Operators
o Assignment Operators
o Functions in C
o Microsoft ® Visual Studio
∙ Activities
o Pre-Lab Activity
▪ break Statement
▪ continue Statement
▪ Task 01: Even Numbers
o In-Lab Activity
▪ Logical Operators in C
▪ Assignment Operators
▪ if Statement
▪ Functions in C
– Defining a function
– Function Declaration/Prototype
– Calling a Function
▪ Math Library Functions
▪ Task 01: Find Second Largest Number ▪
Task 02: Square Asterisks
▪ Task 03: Fill in the blank area with code ▪
Task 04: Dry Run Program
▪ Task 05: Find and Resolve Errors
o Post-Lab Activity
▪ Task 01: Reverse Digits
▪ Task 02: Fill in the blank area with code ∙
Submissions
∙ Evaluations Metric
∙ References and Additional Material
∙ Lab Time and Activity Simulation Log

Laboratory 04 – Modular Program Development – I Page 2 of 18


CC-112L Programming Fundamentals SPRING 2022
Learning Objectives:
∙ Using break Statement
∙ Using continue Statement
∙ Using Logical Operators
∙ Using Assignment Operators
∙ Functions in C
∙ Using Math Library Functions
∙ Defining a Function in C
∙ Function Prototypes in C
Resources Required:
∙ Desktop Computer or Laptop
∙ Microsoft ® Visual Studio 2022

General Instructions:
∙ In this Lab, you are NOT allowed to discuss your solution with your colleagues, even not
allowed to ask how is s/he doing, this may result in negative marking. You can ONLY
discuss with your Teaching Assistants (TAs) or Lab Instructor.
∙ Your TAs will be available in the Lab for your help. Alternatively, you can send your queries
via email to one of the followings.
Course Instructors:

Teacher Prof. Dr. Syed Waqar ul [email protected]


Qounain

Teacher Assistants Usman Ali [email protected]

Saad Rahman [email protected]

Laboratory 04 – Modular Program Development – I Page 3 of 18


CC-112L Programming Fundamentals SPRING 2022

Background and Overview:


break Statement: break Statement terminates the loop execution. When it is encountered in a loop,
the loop immediately terminates. It is also used to terminate a switch case statement.

continue Statement: The continue Statement works like the break Statement but instead of
terminating the loop it only skips one iteration of a loop e.g., it forces the next iteration by skipping
any code in between.

Logical Operators: Operators which are used to perform logical operations of given expressions or
variables.
Assignment Operators: Assignment Operators are used to assign a value to a variable. The left-side
operand of the assignment operator is a variable and right-side operand of the assignment operator is
a value. The value on the right side must be of the same data-type of the variable on the left side.

Function in C: A named block of code which performs a specific task. Every C program has at least
one function of main(). You can pass data (parameters) to a function. A function is useful in reusing
a code, which means to write a code once and use it multiple times.

Microsoft ® Visual Studio: It is an integrated development environment (IDE) from Microsoft. It is


used to develop computer programs, as well as websites, web apps, web services and mobile apps.

Microsoft ® Visual Studio supports 36 different programming languages and allows the code editor
and debugger to support nearly any programming language, provided a language-specific service
exists. Built-in languages include C, C++, C++/CLI, Visual Basic, .NET, C#, F#, JavaScript,
TypeScript, XML, XSLT, HTML, and CSS. Support for other languages such as Python, Ruby,
Node.js, and M among others is available via plug-ins. Java (and J#) were supported in the past.

© https://en.wikipedia.org/wiki/Microsoft_Visual_Studio

Laboratory 04 – Modular Program Development – I Page 4 of 18


CC-112L Programming Fundamentals SPRING 2022

Activities:
Pre-Lab Activities:
break Statement:
The break statement terminates a loop at a certain point. Its execution is explained by the following
code.
Fig. 01 (break
Statement)

If we ignore the break Statement in the above code then it displays number from 1 to 10 on
the console. The execution with the break statement is as follows:

∙ Loop executes each iteration and check if variable “i” is equal to value “6” ∙
Then the program displays the value of “i” on Console
∙ On sixth iteration, when the if condition becomes true, the program moves to the line 7 ∙ As the
break statement on line 8 is executed the program terminates the loop and shifts to the line 12
Following is the output of the above program

Fig. 02 (break
Statement)

continue Statement:

The continue statementskips one iteration of a loop at a certain point. Its execution is explained
by the following code.

Laboratory 04 – Modular Program Development – I Page 5 of 18


CC-112L Programming Fundamentals SPRING 2022
Fig. 03 (continue
Statement)

If we ignore the continue statement in the above code then it displays number from 1 to 10 on
the console. The execution with the break Statement is as follows:

∙ Loop executes each iteration and check if variable “i” is equal to value “6” ∙
Then the program displays the value of “i” on Console
∙ On sixth iteration, when the if condition becomes true, the program moves to the line 7 ∙ As the
continue statement on line 8 is executed the program skips an iteration and shifts to the line 4
i.e., “6” is not displayed on the Console
Following is the output of the above program

Fig. 04 (continue
Statement)

Task 01: Even Numbers [Estimated 20 minutes / 20 marks]


Create a C program which:

∙ Iterates a loop from 0 to 100


∙ Displays Even Numbers between 0 to 50 using break statement
∙ Skips the second and third iteration using continue statement
The output should be as follows:

Laboratory 04 – Modular Program Development – I Page 6 of 18


CC-112L Programming Fundamentals SPRING 2022
Fig. 05 (Pre-Lab
Task)

∙ Submit “.c” file named your “Roll No” on Google Classroom

In-Lab Activities:
Logical Operators in C:
Operation Logical Operator Description

AND && It returns true when both conditions are true. (x > 1 && y >
1)

OR || It returns true when at-least one condition is true. (x > 1 || y >


1)

NOT ! It reveres the state of the operand. e.g., if (y > 1) is true,


then it returns false

Following is an example of Logical Operators

Fig. 06 (Logical
Operators)
∙ On line 7, the program checks if both conditions are true. As b is not equal to zero, the program
shifts to line 10
∙ On line 10, the program checks if any condition is true. As a is greater than b i.e., a condition
becomes true. The program will display “Only one condition is true”

Fig. 07 (Logical
Operators)
Now, we add a “NOT(!)” operator with second condition.
Laboratory 04 – Modular Program Development – I Page 7 of 18
CC-112L Programming Fundamentals SPRING 2022

Fig. 08 (Logical
Operators)

Now on line 7 the program checks if both conditions are true. As b is not equal to zero, second
condition is false but the “!” operator makes it true. So, the program will display “Both conditions
are true”

Fig. 09 (Logical
Operators)
Assignment Operators:

Assignment Operators assign value to a variable of same type. Assume int a = 1, b = 2, c = 3, d = 4,


e = 5.
Assignment Expression Explanation Assigned value
Operator

+= a += 5 a=a+5 Assigns 6 to a

-= b -= 1 b=b–1 Assigns 1 to b

*= c *= 3 c=c*3 Assigns 9 to c

/= d /= 2 d=d/2 Assigns 2 to d
%= e %= 3 e=e%3 Assigns 2 to e

Functions in C:

The function in C language is a named group of statements that together perform a task. Every C
program has a function named main() and a programmer can define additional functions to reuse a
group of statements.

Defining a Function:
A function in C programming is generally defined as follows:
return-type function-name (parameter list)
{
body of function

Laboratory 04 – Modular Program Development – I Page 8 of 18


CC-112L Programming Fundamentals SPRING 2022

∙ Return type: A function may return a value. The return-type is the data type of the value which
the function returns. Some functions perform a specific task without returning a value. In that
case, the return-type is the keyword “void”
∙ Function Name: This is the actual name of the function. The function name and the parameter list
together constitute the “function signature”
∙ Parameters: A parameter is like a placeholder. When a function is called/invoked, you pass a
value to the parameter. This value is referred to as parameter or argument. The parameter list
refers to the type, order, and number of the parameters of a function. Parameters are not
compulsory, a function may contain no parameters
∙ Function Body: Function body contains the set of statements which defines what the function will
do
Laboratory 04 – Modular Program Development – I Page 9 of 18
CC-112L Programming Fundamentals SPRING 2022

Following is the source code for a function called “minimum”. The function takes two parameters
num1 and num2 and returns the minimum between the two.
Fig. 10 (Function in
C)
Function Declaration/Prototype:
Function prototype is widely known as Function declaration. Function declaration tells the compiler
about the function name and how to call a function. The body of the function can be defined
separately. A function declaration has following parts:
return-type function-name (parameter list);
For the function above in Fig. 10, the function declaration is: int minimum(int num1, int num2);

Parameter names are not important in function declaration, only their type is required. So, the above
function declaration can be written as: int minimum(int, int);

Calling a Function:
While creating a C function, you give a definition of what the function has to do. To use a function,
you will have to call that function. When a program calls a function, program control is transferred
to the called function. A called function performs defined task, and when its return statement is
executed or when the function ends it returns program control back to the main program.

Laboratory 04 – Modular Program Development – I Page 10 of 18


CC-112L Programming Fundamentals SPRING 2022
Fig. 11 (Function in
C)
The execution of above program is as follows:

∙ Execution starts from the main()


∙ At line 6 & 7, two integers are defined
∙ At line 8 an integer is declared
∙ At line 10, function is called and its value is assigned to an integer
∙ As the function is called program control shifts to line 14
∙ Specified task is performed from line 14 to 21
∙ After execution of line 21, program control shifts to line 11
∙ At line 11, message is displayed on the console

Fig. 12 (Function in
C)
Math Library Functions:
C’s math library functions allow you to perform common mathematical calculations. To use math
library function you have to include a header “math.h” as follows:

#include <math.h>
Function Description

sqrt (x) Square root of x

cbrt (x) Cube root of x

exp (x) Exponential function ex

log (x) Natural logarithm of x


log10 (x) Logarithm of x (base 10)

Laboratory 04 – Modular Program Development – I Page 11 of 18


CC-112L Programming Fundamentals SPRING 2022
fabs (x) Absolute value of x

ceil (x) Rounds x to smallest integer not less than x

floor (x) Rounds x to largest integer not greater than x

pow (x, y) x raised to the power y (xy)

fmod (x, y) Remainder of x/y

sin (x) Sine of x

cos (x) Cosine of x

tan (x) Tangent of x

Use of power function is shown below:

Fig. 13 (Math Library Function)


5
The power function returns the result of 2 .

Fig. 14 (Math Library Function)


Task 01: Find Second Largest Number [30 minutes / 20 marks] Write a C program which:

∙ Reads three numbers as an input from user


∙ Find the second largest number using “Logical Operators”
∙ Displays “Second Largest Number” on the Console
Input Output

234 Second Largest number is 3

89 33 56 Second Largest number is


56

10 33 33 Second Largest number is


10
Task 02: Square Asterisks [30 minutes / 30 marks] Write a C program which:

∙ Reads a number as an input from user


∙ Pass that number to a function
∙ Write a function logic to create square asterisks pattern
∙ Display Asterisks on the Console [Hint: Use void as function return-type]

Laboratory 04 – Modular Program Development – I Page 12 of 18


CC-112L Programming Fundamentals SPRING 2022
Input Output

2 **
**

4 ****
****
****
****

5 *****
*****
*****
*****
*****

Task 03: Fill the blank area [20 minutes / 20 marks] The following program:

∙ Take integer as an input from the user


∙ Call a function in main()
∙ Display square of the integer in the function using Math Library Function

Fig. 15 (In-Lab Task)


Input Output

2 Square is: 4

6 Square is: 36

Task 04: Dry Run Programs. [40 minutes / 30 marks] (a) Dry Run the following programs
and fill up the trace table:
Laboratory 04 – Modular Program Development – I Page 13 of 18
CC-112L Programming Fundamentals SPRING 2022

Fig. 16 (In-Lab
Task)
23

72

0
(b)
99
Input

Fig. 17 (In-Lab
Task)
Value of I (Iteration No) num at end of the iteration

7
9

13

17

Task 05: Find and resolve the errors. [30 minutes / 20 marks] (a):

Fig. 18 (In-Lab
Task)

Laboratory 04 – Modular Program Development – I Page 14 of 18


CC-112L Programming Fundamentals SPRING 2022

(b):

Fig. 19 (In-Lab
Task)
Laboratory 04 – Modular Program Development – I Page 15 of 18
CC-112L Programming Fundamentals SPRING 2022

Post-Lab Activities:
Task 01: Reverse Digits [Estimated 30 minutes / 30 marks]
Write a function that takes an integer value and returns the number with its digits reversed. For
example, given the number 1234, the function should return 4321.
Input Output

23 Reverse Digits: 32

65732 Reverse Digits:


23756
Task 02: Fill in the blank area [Estimated 30 minutes / 20 marks] Complete the following code
in which:

∙ Function finds all the prime numbers between a given interval


∙ Displays the prime numbers on the Console

Fig. 20 (Post-Lab Task)


Input Output

2 13 Prime Numbers: 3, 5, 7, 11

3 18 Prime Numbers: 5, 7, 11, 13,


17

35 No Prime Number

Laboratory 04 – Modular Program Development – I Page 16 of 18


CC-112L Programming Fundamentals SPRING 2022

Submissions:
∙ For Pre-Lab Activity:
▪ Submit the .c file on Google Classroom and name it with your roll no.
∙ For In-Lab Activity:
▪ Save the files on your PC.
▪ TA’s will evaluate the tasks offline.
∙ For Post-Lab Activity:
▪ Submit the .c file on Google Classroom and name it with your roll no.

Evaluations Metric:

∙ All the lab tasks will be evaluated offline by TA’s


∙ Division of Pre-Lab marks: [20 marks] ▪ Even Numbers [20 marks] ∙ Division of
In-Lab marks: [120 marks] ▪ Task 01: Find Second Largest Number [20 marks] ▪ Task
02: Square Asterisks [30 marks] ▪ Task 02: Fill in the blank area [20 marks] ▪ Task 03:
Dry Run Program [30 marks] ▪ Task 04: Find and Resolve Errors [20 marks] ∙ Division
of Post-Lab marks: [50 marks] ▪ Task 01: Reverse Digits [30 marks] ▪ Task 02: Fill in
the blank area [20 marks]

References and Additional Material:


∙ Function in C
https://www.programiz.com/c-programming/c-functions

∙ C break and continue statements


https://www.programiz.com/c-programming/c-break-continue-statement

∙ C Programming Operators
https://www.programiz.com/c-programming/c-operators

Laboratory 04 – Modular Program Development – I Page 17 of 18


CC-112L Programming Fundamentals SPRING 2022

Lab Time Activity Simulation Log:


∙ Slot – 01 – 00:00 – 00:15: Class Settlement ∙ Slot
– 02 – 00:15 – 00:30: In-Lab Task
∙ Slot – 03 – 00:30 – 00:45: In-Lab Task
∙ Slot – 04 – 00:45 – 01:00: In-Lab Task
∙ Slot – 05 – 01:00 – 01:15: In-Lab Task
∙ Slot – 06 – 01:15 – 01:30: In-Lab Task
∙ Slot – 07 – 01:30 – 01:45: In-Lab Task
∙ Slot – 08 – 01:45 – 02:00: In-Lab Task
∙ Slot – 09 – 02:00 – 02:15: In-Lab Task
∙ Slot – 10 – 02:15 – 02:30: In-Lab Task
∙ Slot – 11 – 02:30 – 02:45: In-Lab Task
∙ Slot – 12 – 02:45 – 03:00: Discussion on Post-Lab Task
Laboratory 04 – Modular Program Development – I Page 18 of 18

You might also like