Fundamentals of Programming 2021[54]_010051
Fundamentals of Programming 2021[54]_010051
PREPARED BY
This Lab manual has been prepared by Lec. Adeela Waqar under the supervision of Dr. Naveed Iqbal Rao,
Head of Computer Software Engineering Department, in the year 2013.
GENERAL INSTRUCTIONS
a. Students are required to maintain the lab manual with them till the end of the semester.
b. All readings/answers to questions/illustrations must be solved in the space provided. If more space is
required, then additional sheets may be attached.
c. It is the responsibility of the student to have the manual graded before deadlines as given by the instructor.
d. Loss of manual will result in resubmission of the complete manual.
e. Students are required to go through the experiment before coming to the lab session. Lab session details will
be given in training schedule.
f. Students must bring the manual in each lab.
g. Keep the manual neat, clean and presentable.
h. Plagiarism is strictly forbidden. No credit will be given if a lab session is plagiarized and no resubmission
will be entertained.
i. Marks will be deducted for late submission.
VERSION HISTORY
Date Updated By Details
August 2013 Lec Adeela Waqar First Version Created
Oct 2017 Lab Engr Marium Hida Exercises Updated
Sept 2019 LE Usman Shafique Updated Exercises and CLOs and Added Lab Rubrics
Nov 2021 LE Sehrish Ferdous Lab Rubrics Updated
At the end of the course the students will be able to: PLOs BT Level*
1. Recognize syntax and semantics of different programming 1 C-2
Languages.
2. Apply basic algorithms for identifying and solving real world 2 C-3
problems.
3. Use the latest IDEs and other supplementary tools to aid 5 P-3
implementation and code management.
9 1-DIMENSIONAL ARRAYS 3 1
10 2-DIMENSIONAL ARRAYS 3 1
13 STRUCTURES 3 1
15 Lab Exam 3 1
16 Project 3 3
MARKS
Grand Total
Learning Objectives
Practice Questions
You can practice the following problems in this lab.
Task 1.1 Declaring Variables Write a C++ program, which declares integer ‘a’, float ‘b’ and character ‘c’.
Ask user for an integer value and store it in ‘a’, for a float value and store it in ‘b’ and for a character value
and store it in ‘c’. At the end, print the values of these three variables on screen. Your output should be as
below. Indent your code and include comments for improving the readability of your code.
Enter Value 1: 50
Enter Value 2: 50
Sum: 50 + 50 = 100
Difference: 50 – 50 = 0
Product: 50 * 50 = 2500
Quotient: 50 / 50 = 1
Remainder: 50 % 50 = 0
MCS
This is a test.
He asked, “How are you doing?”
“Education is the most powerful weapon which you can use to ‘CHANGE’ the world”.
S
Ideally, the values entered by user should not be changed while your program is executing, otherwise your
program’s calculations will be based on modified values and thus, will be considered incorrect. Is there any
way to ensure that your program does not accidently modify the values entered by user? If yes, please write
9 CPS 110 Fundamentals of Programming – C Building Blocks I
appropriate C code to incorporate this feature. If no, explain why it is not possible. Do you think the C
reserved word ‘const’ can be useful here?
Output of your program must be presentable and self-explanatory. You may use different escape sequences
to customize output of your program.
Web Resources
http://www.cprogramming.com/
Learning Objectives
Practice Questions
You can practice the following problems in this lab.
Task 2.1 ASCII Values in C++
Imp Note: Find out the differences between get(), getch(), getche() and getchar() before lab session.
Every character has an ASCII value. Write a C program, which takes a character input from user and prints
its ASCII value on screen. You must use all built-in functions for taking character input from user (like
get(), getch() , getche(), getchar() etc), one by one. ASCII Table is included in Appendix A for your
reference. Indent your code and include comments for improving the readability of your code. Your
program should have the following interface.
Enter a number: 20
20 + 5 = 25
20 – 3 = 17
(20 + 3)- 2 = 21
((20 + 5) *2 / (20 + 3)) = 2
Write a C++ program which prompts the user to enter two integer values, stores them in variables named ‘a’
and ‘b’, calculates the following expressions and displays the results on screen. Output of your program
must be presentable. You must use minimum possible number of parentheses for evaluating these
expressions.
1. (a + b)2 = a2 + 2ab + b2
2. (a - b)2 = a2 - 2ab + b2
3. (a + b)3 = a3 + 3a2b + 3ab2 + b3
4. (a - b)3 = a3 - 3a2b + 3 ab2 - b3
5. a2 - b2 = (a - b) (a + b)
6. a3 - b3 = (a - b)3 + 3 a b (a - b)
7. a3 - b3 = (a - b) (a2 + a b + b2)
8. a3 + b3 = (a + b) (a2 - a b + b2)
9. a3 + b3 = (a + b)3 - 3 a b (a + b)
Web Resources
http://www.cprogramming.com/
Learning Objectives
Practice Questions
You can practice the following problems in this lab.
Task 3.1 ‘if’ Statement
Write a C++ program which takes two numbers from the user and tells which number is greater. Your
output should be as below.
Enter a value: 7
It is an odd digit.
Enter a value: H
It is an alphabet in UPPERCASE.
Enter a value: *
It is a special symbol.
Web Resources
http://math.about.com/od/businessmath/ss/Interest_2.htm http://www.tutorialspoint.com/cplusplus/
Learning Objectives
In today’s lab, you will practice;
1. Using ‘for’ repetition structure.
2. Using ‘while’ repetition structure.
3. Using ‘do/while’ repetition structure.
Practice Questions
You can practice the following problems in this lab.
Task 4.1 ‘for’ Statement
Write a C++ program which takes an integer input from the user, prints all even and odd integers less than
or equal to this number and greater than or equal to1, calculates the sum of these even and odd integers
separately and displays results on the screen. Your program should have the following interface.
Enter a number: 7
Task 4.4 ‘for’ Statement Write a C++ program which takes an integer input from user in the variable
named ‘num’, calculates the sum of all integers from 1 to ‘num’ and displays this sum on screen. Your
program should have the following interface.
Web Resources
http://www.tutorialspoint.com/cplusplus/ http://www.learncpp.com/
Learning Objectives
Practice Questions
You can practice the following problems in this lab.
Task 5.1 Nested ‘for’ Statement
Write a C++ Program which displays hours, minutes and seconds from 01:00:00 to 10:00:00. You can
display the time using a delay of one second. Find out how you can do it and use it in your program. Your
program should have the following interface.
01:00:00
01:00:01
01:00:02
:
:
3 6 9 12 15 18 21 24 27 30 33 36 4 8 12 16 20 24 28 32 36 40 44 48 5 10 15
20 25 30 35 40 45 50 55 60 6 12 18 24 30 36 42 48 54 60 66 72 7 14 21 28 35 42
49 56 63 70 77 84 8 16 24 32 40 48 56 64 72 80 88 96 9 18 27 36 45 54 63 72 81
90 99 108 10 20 30 40 50 60 70 80 90 100 110 120 11 22 33 44 55 66 77 88 99 110 121 132
12 24 36 48 60 72 84 96 108 120 132 144
Sum = 11 + 22 + 33 +……+NN
Enter a number: 4
Sum = 1^1 + 2^2 + 3^3 + 4^4 = 288
Web Resources
http://www.tutorialspoint.com/cplusplus/ http://www.learncpp.com/
Learning Objectives
Practice Questions
You can practice the following problems in this lab.
Task 6.1 Functions with No Arguments/One Argument
Write a function called ‘separator’ in C++ language which prints 15 *s in a single line. Write another
function named ‘multiples’ that takes an integer as an argument and prints its multiples up to ten terms,
separated by spaces, in a single line. Write a C++ program which takes an integer ‘N’ from user and calls
these two functions to generate tables of all numbers between 1 to N. Your program should have the
following interface.
* * * * * * * * * * * * * * * 2 4 6 8 10 12 14 16
18 20
* * * * * * * * * * * * * * * 3 6 9 12 15 18 21 24
27 30
* * * * * * * * * * * * * * *
4 8 12 16 20 24 28 32 36 40
1. http://www.tutorialspoint.com/cplusplus/
EXPERIMENT 7 – C++ LANGUAGE AND FUNCTIONS-II
Learning Objectives
Practice Questions
You can practice the following problems in this lab. Task
7.1 Function Overloading
Write a C program using function overloading. The name of the overloaded function is ‘square’. The
different prototypes of this overloaded function are;
• void square(void); // Used to print a solid square of *s with side length of 5
• void square (char); // Used to print a solid square of the character passed as argument with side
length of 6
• void square(char, int); // Used to print a solid square of the character and the side length passed as
arguments to this function
Make calls to these functions one by one in your program.
Enter a number: 6
The square of 6 is: 36
The cube of 6 is: 216
The values of character variables can either be ‘l’ for left or ‘r’ for right. The first integer after both
characters indicates the number of shifts required and the second one indicates the additive constant to be
added to each digit of the four-digit integer supplied as the fifth input.
Your program should calculate the value and display result on screen. A sample output is given below. You
will have to separate the digits of the fifth input before passing them to circular shift functions.
Web Resources
1. http://www.cplusplus.com/doc/tutorial/
Learning Objectives
Practice Questions
You can practice the following problems in this lab.
Task 8.1 Using Header Files
Repeat Task 6.1, 6.2, 6.3 and 6.4 placing the defined functions in separate header files.
Web Resources
1. http://www.cprogramming.com/
Learning Objectives
Practice Questions
You can practice the following problems in this lab.
Task 9.1 Declaring One-Dimensional Arrays
Write a C++ program, which declares;
• Integer type array of ‘10’ Indexes
• Float type array of ’15’ Indexes
• Double type array of ‘20’ Indexes
• Character type array of ‘25’ Indexes
Your program should calculate and print the size of each array. Your program should have the following
interface.
• Integer type array of size ‘6’. Initialize each index of array with ‘12’ at the Time of Declaration.
• Float type array of size ‘5’. Initialize each index of array with ‘0.5’ at the Time of Declaration.
• Character type array of size ‘4’. Initialize each index of array with ‘a’ at the Time of Declaration. Your
program should display the values stored in these arrays using the following format.
Integer array : 12 12 12 12 12 12
Float array : 0.5 0.5 0.5 0.5 0.5 Character
array : a a a a
The Integer Array values are : 3, 4, 10, 102, 21, 968 The
Float Array values are : 3.5, 4.1, 1.8, 102.4, 21.3
The Character Array values are : s, r, o, i
The average is 38
Web Resources
1. http://www.cprogramming.com/
2. http://www.cprogramming.com/
Practice Questions
You can practice the following problems in this lab.
Task 10.1 Declaring, Initializing and Printing Two-Dimensional Arrays
Write a C program which initializes a 7x7 float type array with ‘0’ and then prints it on screen. Your
program should have the following interface.
Location [0][0] : 1
Location [0][1] : 3
Location [0][2] : 6
Location [0][3] : 7
Enter values for row 1 :
Location [1][0] : 7
Iran
Pakistan
Turkey
The 10 x 10 array is :
8 9 6 4 6 5 7 8 1 2
4 6 3 1 1 2 8 7 4 5
6 7 8 3 4 2 3 2 3 1
2 3 2 1 4 0 6 7 8 4
0 1 9 4 7 6 5 3 2 3
2 1 2 1 3 4 3 7 8 6
6 7 8 1 2 1 3 2 4 1
5 1 2 1 1 1 3 4 7 6
7 8 7 6 4 4 3 1 2 3
4 3 2 1 4 7 8 3 0 1
Minimum Value is : 0
Found at location [3][5]
Found at location [4][0]
Found at location [9][8]
3 instances found.
Maximum Value is : 9
Found at location [0][1]
Found at location [4][2]
2 instances found.
The 10 x 10 array is :
8 9 6 4 6 5 7 8 1 2
4 6 3 1 1 2 8 7 4 5
6 7 8 3 4 2 3 2 3 1
2 3 2 1 4 0 6 7 8 4
0 1 9 4 7 6 5 3 2 3
2 1 2 1 3 4 3 7 8 6
Web Resources
1. http://www.tutorialspoint.com/cplusplus/
2. http://projectsyapa.com/cpp
3. http://en.wikipedia.org/wiki/Tic-tac-toe
Practice Questions
You can practice the following problems in this lab.
Task 11.1 Declaring and Initializing Pointers
Write a C program which creates pointer variables for the data types int, float, char and double, initializes
these pointers with the addresses of variables of respective data types and prints the size of pointer and the
data held by these pointers.
It is displayed as :
I am a student
Web Resources
1. http://en.wikipedia.org/wiki/Tic-tac-toe
2. www.cplusplus.com/doc/tutorial/pointers/
3. www.tutorialspoint.com/cplusplus/cpp_pointers.htm
4. www.youtube.com/watch?v=W0aE-w61Cb8
Practice Questions
You can practice the following problems in this lab.
Task 12.1 Traversing through Strings
Write a C function called ReplaceNot that takes a string as a parameter, searches all three letter words in the
string passed as argument and replaces them with ‘Not’. Apply all the checks and cases. For example, if the
passed string is: “Every boy has a pen”. Your function should modify this string to: “Every Not Not a
Not”. Use this function in your main program to verify that your function works correctly.
Web Resources
1. http://en.wikipedia.org/wiki/Tic-tac-toe
2. www.tutorialspoint.com/cplusplus/cpp_pointers.htm
3. www.youtube.com/watch?v=W0aE-w61Cb8
Practice Questions
You can practice the following problems in this lab.
Task 14.1 Declaring Structures
Write a C program for maintaining the library record of 100 books. Each record is composed of 6 fields
which include book’s library number, book’s name, author’s name, edition number, year of publishing and
price of the book. Choose appropriate data types to represent each field. Your program should prompt the
user to populate 10 records of this database with some values and then print these values on screen. You
must use array of structures in your program. Your program should have the following interface.
1.
Book’s Library Number
2.
Book’s Name
5. Year of Publishing
6. Price of Book
43 –
CPS 110 Fundamentals of Programming Structures
1 Book Found.
Book 1: A Guide to C Language Programming by XYZ, Edition 1
published in 2013. Price 250
Web Resources
1. www.cplusplus.com
2. msdn.microsoft.com/en-us/library/64973255.aspx
3. www.cprogramming.com/tutorial/lesson7.html
44 –
CPS 110 Fundamentals of Programming Structures
45 –
“I hear and I forget,
I see and I remember,
I do and I understand”
Confucius