Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Fundamentals of Programming
(CPS-110)
Fall Semester
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 plagiarised 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
9 1-DIMENSIONAL ARRAYS-I 3
10 1-DIMENSIONAL ARRAYS-II 3
11 2-DIMENSIONAL ARRAYS-I 3
12 2-DIMENSIONAL ARRAYS-II 3
STRUCTURES 3
Grand Total
Learning Objectives
In todays lab, you will practice;
1. Indenting your code.
2. Writing comments in your code.
3. Declaring variables (int, float and char).
4. Using two of the C++ languages most commonly used built-in functions namely cin and cout
with/without using variables.
5. Using basic arithmetic operators available in C++ ( +, - ,* , / ).
6. Using escape sequences in cout statements.
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 Integer Value: 448
Enter Float Value: 32.65
Enter Character Value: A
***** You have entered the following values *****
Integer Value is: 448
Float Value is: 32.65
Character Value is: A
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
MCS
MCS
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
programs 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
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.
http://www.cprogramming.com/
http://www.mycplus.com/featured-articles/top-10-applications-written-in-c-cplusplus/
http://www.cplusplus.com/doc/tutorial/program_structure/
http://www.cplusplus.com/doc/tutorial/variables/
http://www.cplusplus.com/doc/tutorial/constants/
http://www.cs.wustl.edu/~schmidt/C++/
Learning Objectives
In todays lab, you will practice;
1. Taking a single character input from user using different built-in functions.
2. Displaying the ASCII values of different characters.
3. Evaluating arithmetic expressions using rules of precedence.
4. Using increment and decrement operators.
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. Your 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.
Solution :
#include <iostream>
using namespace std;
int main ()
{
char f;
cout<<"Enter a character and i will tell you its ascii value: ";
cin>>f;
return 0;
}
Solution :
#include <iostream>
using namespace std;
int main ()
{
float x,y;
cout<<"enter the temperature in centigrade: ";
cin>>x;
y=(x*1.8)+32;
cout<<x<<" degrees is equal to "<<y<<"fahrenheit. ";
return 0;
}
#include <iostream>
using namespace std;
int main ()
{
int x;
cout<<"Enter a three digit number: ";
cin>>x;
cout<<"\tThere are\t: "<<x/100<<"\tHundreds\t"<<(x%100)/10<<"\tTens
and\t"<<x%10<<"\tOnes in the number\t: "<<x<<".";
return 0;
}
#include<iostream>
using namespace std;
int main()
{
float x,y,z;
cout<<"Enter First Number: ";
cin>>x;
z=(x/y)*100;
return 0;
}
Enter a number : 20
20 + 5 = 25
20 3 = 17
(20 + 3)- 2 = 21
((20 + 5)*2 / (20 + 3)) = 2
Web Resources
http://www.cplusplus.com/doc/tutorial/
Learning Objectives
In todays lab, you will practice;
1. Using one-way selection structure (if).
2. Using two-way selection structure (if/else).
3. Using multiple selection structure (switch).
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 : f
It is an alphabet in lowercase.
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 todays 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
Odd numbers less than or equal to 7 are :
1 , 3 , 5, 7
Even numbers less than or equal to 7 are :
2 , 4 , 6
Web Resources
http://www.tutorialspoint.com/cplusplus/
http://www.learncpp.com/
Learning Objectives
In todays lab, you will practice;
1. Using nested for repetition structure.
2. Using nested while repetition structure.
3. Using nested do/while repetition structure.
4. Using nesting of different control structures.
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
:
:
2 4 6 8 10 12 14 16 18 20 22 24
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
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
In todays lab, you will practice;
1. Writing functions which take no arguments.
2. Writing functions which take one or more arguments.
3. Writing functions which do not return a value.
4. Writing functions which return a value.
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.
Web Resources
1. http://www.tutorialspoint.com/cplusplus/
2. http://www.learncpp.com/
Learning Objectives
In todays lab, you will practice;
1. Writing overloaded functions.
2. Passing arguments by reference.
3. Writing macros.
4. Placing functions before and after main().
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.
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
In todays lab, you will practice;
1. Writing functions in separate header files and including them in your main file.
2. Using recursive functions.
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
In todays lab, you will practice;
1. Declaring and initializing one-dimensional arrays.
2. Inserting values into and printing values out of one-dimensional arrays.
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
Web Resources
1. http://www.cprogramming.com/
Learning Objectives
In todays lab, you will practice;
1. Traversing through one-dimensional arrays.
2. Passing one-dimensional arrays to functions.
Practice Questions
You can practice the following problems in this lab.
Task 10.1 Searching through One-Dimensional Arrays
Write a C++ program, which;
Declares and initializes an integer array of 15 indexes
Prompts the user to enter a number (integer value)
Searches through the array to find this number and
Prints the respective index numbers where the user specified number has been found
Your program should have the following interface.
Web Resources
1. http://www.cprogramming.com/
2. http://www.dreamincode.net/
3. http://www.learncpp.com/
Learning Objectives
In todays lab, you will practice;
1. Declaring and initializing two-dimensional arrays.
2. Inserting values into and reading values out of two-dimensional arrays.
3. Traversing through two-dimensional arrays.
Practice Questions
You can practice the following problems in this lab.
Task 11.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.
The 7 X 7 array is :
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
Web Resources
1. http://www.tutorialspoint.com/cplusplus/
2. http://www.learncpp.com/
3. http://projectsyapa.com/cpp
Learning Objectives
In todays lab, you will practice;
1. Traversing through two-dimensional arrays.
2. Passing two-dimensional arrays to functions.
Practice Questions
You can practice the following problems in this lab.
Task 12.1 Searching through Two-Dimensional Arrays
Write a C++ program, which
Populates a two-dimensional array of size 10 x 10 with random numbers between 0 and 9 (Hint: C++
has a built-in function for random number generation)
Prompts the user to enter a number
Searches for this number in the 10 x 10 array and
Displays location where the number is found in the array
Your program should display Value not found if the users specified number is not found in the 10 x
10 array. Your program should have the following interface.
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
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
Web Resources
1. http://en.wikipedia.org/wiki/Tic-tac-toe
2. http://www.tutorialspoint.com/cplusplus/
3. http://www.learncpp.com/
Learning Objectives
In todays lab, you will practice;
1. Declaring and initializing pointers.
2. Using pointer arithmetic.
3. Passing pointer variables to functions.
Practice Questions
You can practice the following problems in this lab.
Task 13.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
Learning Objectives
In todays lab, you will practice;
1. Traversing through strings.
2. Passing pointer variables to functions.
Practice Questions
You can practice the following problems in this lab.
Task 14.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.cplusplus.com/doc/tutorial/pointers/
3. www.tutorialspoint.com/cplusplus/cpp_pointers.htm
4. www.youtube.com/watch?v=W0aE-w61Cb8
Learning Objectives
In todays lab, you will practice;
1. Declaring structures.
2. Traversing through structures.
Practice Questions
You can practice the following problems in this lab.
Phase 1 Declaring Structures
Write a C++ program for maintaining the library record of 100 books. Each record is composed of 6 fields
which include books library number, books name, authors 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 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
Write a C++ program to implement a traffic signal simulator and vehicle counter. Create a four way 3-lane
signal interchange and randomly generate traffic on each lane of each road. The program should support two
modes; Auto and Manual. In the Auto mode, the signals should operate automatically and move traffic
accordingly. In the Manual mode, user should be able to control the traffic signal. In this mode, user should
also be able to generate accidents by turning more than one signal green. Your program should also count
the number of vehicles going north, south, east and west and store these values in a file. The program should
have the following interface;
Phase 3 Demo
You will be required to demonstrate your project in lab. Both group members must be present during demo.
Deadline : 1 week after assignment
Submission dates, submission instructions, policies and penalties for violation of policies will be discussed
in class.
Confucius