CC-112L Programming Fundamentals Laboratory 04 Modular Program Development - I
CC-112L Programming Fundamentals Laboratory 04 Modular Program Development - I
CC-112L Programming Fundamentals Laboratory 04 Modular Program Development - I
Programming Fundamentals
Laboratory 04
Version: 1.0.0
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
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:
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 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
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.
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)
In-Lab Activities:
Logical Operators in C:
Operation Logical Operator Description
AND && It returns true when both conditions are true. (x > 1 && y >
1)
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:
+= 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
∙ 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.
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
2 **
**
4 ****
****
****
****
5 *****
*****
*****
*****
*****
Task 03: Fill the blank area [20 minutes / 20 marks] The following program:
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)
(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
2 13 Prime Numbers: 3, 5, 7, 11
35 No Prime Number
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:
∙ C Programming Operators
https://www.programiz.com/c-programming/c-operators