Discrete Structure Lab Manual Final-1
Discrete Structure Lab Manual Final-1
CS09202|11
Laboratory Manual
Student Copy
Fall 2024
Submitted to:
Miss Hafiza Mariyum Mustafa
Lecturer
Submitted by:
Shahzaib Khan , Moiz Iqbal
Name:
70176698, 70173907
Registration Number:
● Make sure that your observation for previous week experiment is evaluated by the
faculty member and your have transferred all the contents to your record before entering
to lab.
● At the beginning of the class, if the faculty or the instructor finds that a student is not
adequately prepared, they will be marked as absent and not be allowed to perform the
experiment.
● Please utilize teaching assistants fully. To encourage you to be prepared and to read the
lab manual before coming to the laboratory, unannounced questions may be asked at
any time during the lab.
● Maintain silence, order and discipline inside the lab. Don’t use cell phones inside the
laboratory.
● Report to the instructor if you find equipment that is out of order or you break
something.
The University of Lahore Department of Technology
Safety Precautions
This is a partial list of basic safety precautions to use when working on a computer:
Safety Undertaking
I have read all of the above, and I agree to conform to its contents.
Student Signature:
Date:
Lab Instructor:
The University of Lahore Department of Technology
4 of the learned upon accurate technical content to minimum technical answers to the the questions asked by
skill technical content the questions asked content to the questions asked by examiner.
Some attempt at
Little attempt at
Well-designed Good data layering data. Some Very poor attempt and
layering data.
Object classes with good abstraction and some generalization of missing code. Code is
Missing, duplicate
Oriented data abstraction data layering. Good interface evident but maintenance
6 or not useful class
Design and layering. Code set of operations missed opportunity. headache. No OOP
interface. Some
10% is highly provided. Little Code can be language feature is
OOP language
maintainable duplications maintained with used
feature is used
significant effort
1. Responds well,
quick and very
1. Generally
accurate all the 1. Not much
Responsive and 1. Generally 1. Non-Responsive
time. Responsive and
accurate most of the Responsive and and inaccurate all the
Responsiveness 2. Effectively uses accurate most of
times. accurate few times. times.
to Questions/ eye contact, the times.
7 2. Maintains eye 2. Some eye contact, 2. No eye contact and
Accuracy speaks clearly, 2. Uses eye contact
contact, speaks speaks clearly and unable to speak
10% effectively and ineffectively and
clearly with suitable unclearly in different 3. Dresses
confidently using fails to speak
volume and pace. portions. inappropriately
suitable volume clearly and audibly
and pace.
Designation: Lecturer
E-mail: mariyaum.mustafa@tech.uol.edu.pk
Course CLOs/ PLO 1 PLO2 PLO3 PLO4 PLO5 PLO6 PLO7 PLO8 PLO9 PLO 10 PLO 11 PLO 12
Code
x
CS09202 CLO 3
x
CS09202
List of Experiments
Experiment
Title
No.
Introduce the basic concepts and syntax of C++ programming, including
1 I/O operations and variables.
Understanding the structure of C++ programs, the use of operators, and
2 the implementation of assignment statements.
EVALUATION LOG
Student Name: Shahzaib Khan, Moiz Iqbal Registration No.: 70176698, 70173907
Obtained Marks
LAB No. Checked date Checked by
CLO3 CLO4
10
11
12
13
14
Lab No. 1
Introduce the basic concepts and syntax of C++ programming,
including I/O operations and variables
Objective :
In this lab software introduction has been discussed along with basic writing of the
program inside the compiler and its steps.
Tools:
Dev C++
Background Knowledge:
Dev C++ is an Integrated Development Environment (IDE) for C and C++
programming language. With this environment you can edit, build, run, and debug
your program freely. You can write Windows or console-based C/C++ programs
easily.
C++ is a statically typed, free-form, (usually) compiled, multi-paradigm,
intermediate-level general-purpose middle-level programming language. Many of
today’s operating systems, system drivers, browsers and games use C++ as their core
language. This makes C++ and its software like C Fee and DEV C++ an excellent
choice. Since it is an enhanced/extended version of C programming language, C and
C++ are often denoted together as C/C++.
Sample Tasks
Task 1
Write a program to print your name on the output screen
#include <iostream>
/* this program is print your name on the output
screen */
using namespace std;
int main( )
{
cout<<"Imran khan"<<endl;
}
Task 2
Write a program to calculate the sum of any two numbers on the output screen
#include
<iostream>
using namespace std;
int main( )
{
int a,b,sum;
a=2;
b=3;
sum=a+b;
cout<<"Sum ="<<sum<<endl;
}
Task 3
Write a program to multiply any two numbers and print the result on the output
screen
#include
<iostream>
using namespace std;
int main( )
{
int a,b,mul;
a=2;
b=3;
mul=a*b;
cout<<"Multiply
="<<mul<<endl; return 0;
}
Exercise :
Exercise 1:
Write down the syntax for declaration of variable
Solution:
data_type variable_name;
Instructor Signatures:
Remarks:
Exercise 2
Write a program which displays your Bio data on the output screen.
Solution:
#include <iostream>
using namespace std;
int main() {
cout << "Name: Ahmad Nadeem" << endl;
cout << "Age: 25" << endl;
cout << "Address: Karachi, Pakistan" << endl;
cout << "Qualification: Bachelor of Science in Computer Science" << endl;
cout << "Hobbies: Reading, Coding, Traveling" << endl;
return 0;
}
Instructor Signatures:
Remarks:
Exercise 3
Write a program to add and multiply any three variables a, b, c and print
the result on the output screen
Solution:
sum = a + b + c;
product = a * b * c;
return 0;
Instructor Signatures:
Remarks:
Registration Number:
Date:
Instructor Signatures:
Remarks:
The University of Lahore Department of Technology
4 of the learned upon accurate technical content to minimum technical answers to the the questions asked by
skill technical content the questions asked content to the questions asked by examiner.
Some attempt at
Little attempt at
Well-designed Good data layering data. Some Very poor attempt and
layering data.
Object classes with good abstraction and some generalization of missing code. Code is
Missing, duplicate
Oriented data abstraction data layering. Good interface evident but maintenance
6 or not useful class
Design and layering. Code set of operations missed opportunity. headache. No OOP
interface. Some
10% is highly provided. Little Code can be language feature is
OOP language
maintainable duplications maintained with used
feature is used
significant effort
1. Responds well,
quick and very
1. Generally
accurate all the 1. Not much
Responsive and 1. Generally 1. Non-Responsive
time. Responsive and
accurate most of the Responsive and and inaccurate all the
Responsiveness 2. Effectively uses accurate most of
times. accurate few times. times.
to Questions/ eye contact, the times.
7 2. Maintains eye 2. Some eye contact, 2. No eye contact and
Accuracy speaks clearly, 2. Uses eye contact
contact, speaks speaks clearly and unable to speak
10% effectively and ineffectively and
clearly with suitable unclearly in different 3. Dresses
confidently using fails to speak
volume and pace. portions. inappropriately
suitable volume clearly and audibly
and pace.
#include<iostream>
using namespace std;
int main ( )
{
int length, width;
cin >> length;
cin >> width;
}
Task 2
Write a program to input length and width in any two variable and print the
result on the output screen
#include<iostream>
using namespace std
int main ( )
{
cout << "Enter the length: "<<endl;
cin >> length;
cout << "Enter the width: "<<endl;
cin >> width;
Task 3
Write a program to assign two variables by assignment statement. Assign the
values and print the result on the screen
#include<iostream>
using namespace std;
Int main ( )
{
int a,b;
a=200;
b=100;
cout<<< “value of a=”<<a<<endl;
cout<< “value of b =”<<b<<endl;
Task4
Write a program to print a message on screen by using “endl” manipulator.
Int main()
{
cout<<"i am a "<<endl<<" pakistani";
}
Task5
Write a program in C++ to get two numbers from keyboard during program
execution. Calculate the sum and product of the numbers and then print the
result on the computer screen.
#include<iostream>
Int main()
{
int n1,n2,s,p;
cout<<"Enter first number ?";
cin>>n1;
cout<<"Enter secound number ?";
cin>>n2;
s=n1+n2;
p=n1*n2;
cout<<"sums of numbers="<<s<<endl;
cout<<"products of numbers="<<p<<endl;
}
Exercise :
Exercise 1:
Write a program to print your name and registration number on the output screen
Solution:
#include <iostream>
using namespace std;
int main() {
cout << "Name: Ahmad Nadeem" << endl;
cout << "Registration Number: 12345-CS-2025" << endl;
return 0;
}
Instructor Signatures:
Remarks:
Exercise 2
Write a program to print the menu as below:
This is my first program
My name is Ali
I am electrical engineer want to serve mankind
Solution:
#include <iostream>
using namespace std;
int main() {
cout << "This is my first program" << endl;
cout << "My name is Ali" << endl;
cout << "I am electrical engineer want to serve mankind" << endl;
return 0;
}
Instructor Signatures:
Remarks:
Exercise 3
Write a program in C++ to input three values from user at run time and print the
summation and multiplication of these three values on the output screen
Solution:
cout << "Sum of the three numbers = " << sum << endl;
cout << "Product of the three numbers = " << product << endl;
return 0;
Instructor Signatures:
Remarks:
Exercise 4:
Write a program in c to input the marks of any three subjects from user at run time
and calculate the total and average marks. Also show the result on the output screen.
Solution:
#include <stdio.h>
int main() {
float subject1, subject2, subject3;
float total, average;
return 0;
}
Instructor Signatures:
Remarks:
Exercise 5:
Write a program to get name and age (in years) of a person. Calculate the age in
months and print the name of person and its age in months.
Solution:
#include <iostream>
using namespace std;
int main() {
string name;
int ageInYears;
int ageInMonths;
cout << "Enter your name: ";
getline(cin, name);
return 0;
}
Instructor Signatures:
Remarks:
Registration Number:
Date:
Instructor Signatures:
Remarks:
Lab No. 1
Introduction to C++ programming
Objective :
In this lab software introduction has been discussed along with basic writing of the
program inside the compiler and its steps.
Tools:
Dev C++
Background Knowledge:
Dev C++ is an Integrated Development Environment (IDE) for C and C++
programming language. With this environment you can edit, build, run, and debug
your program freely. You can write Windows or console-based C/C++ programs
easily.
C++ is a statically typed, free-form, (usually) compiled, multi-paradigm,
intermediate-level general-purpose middle-level programming language. Many of
today’s operating systems, system drivers, browsers and games use C++ as their core
language. This makes C++ and its software like C Fee and DEV C++ an excellent
choice. Since it is an enhanced/extended version of C programming language, C and
C++ are often denoted together as C/C++.
Sample Tasks
Task 1
Write a program to print your name on the output screen
#include <iostream>
/* this program is print your name on the output
screen */
using namespace std;
int main( )
{
cout<<"Imran khan"<<endl;
}
Task 2
Write a program to calculate the sum of any two numbers on the output screen
#include
<iostream>
using namespace std;
int main( )
{
int a,b,sum;
a=2;
b=3;
sum=a+b;
cout<<"Sum ="<<sum<<endl;
}
Task 3
Write a program to multiply any two numbers and print the result on the output
screen
#include
<iostream>
using namespace std;
int main( )
{
int a,b,mul;
a=2;
b=3;
mul=a*b;
cout<<"Multiply
="<<mul<<endl; return 0;
}
Exercise :
Exercise 1:
Write down the syntax for declaration of variable
Solution:
data_type variable_name;
Instructor Signatures:
Remarks:
Exercise 2
Write a proggram which displays your Bio data on the output screen.
Solution:
#include <iostream>
using namespace std;
int main() {
cout << "Name: Ahmad Nadeem" << endl;
cout << "Age: 25" << endl;
cout << "Address: Karachi, Pakistan" << endl;
cout << "Qualification: Bachelor of Science in Computer Science" << endl;
cout << "Hobbies: Reading, Coding, Traveling" << endl;
return 0;
}
Instructor Signatures:
Remarks:
Exercise 3
Write a program to add and multiply any three variables a, b, c and print
the result on the output screen
Solution:
sum = a + b + c;
product = a * b * c;
return 0;
Instructor Signatures:
Remarks:
Registration Number:
Date:
Instructor Signatures:
Remarks:
The University of Lahore Department of Technology
4 of the learned upon accurate technical content to minimum technical answers to the the questions asked by
skill technical content the questions asked content to the questions asked by examiner.
Some attempt at
Little attempt at
Well-designed Good data layering data. Some Very poor attempt and
layering data.
Object classes with good abstraction and some generalization of missing code. Code is
Missing, duplicate
Oriented data abstraction data layering. Good interface evident but maintenance
6 or not useful class
Design and layering. Code set of operations missed opportunity. headache. No OOP
interface. Some
10% is highly provided. Little Code can be language feature is
OOP language
maintainable duplications maintained with used
feature is used
significant effort
1. Responds well,
quick and very
1. Generally
accurate all the 1. Not much
Responsive and 1. Generally 1. Non-Responsive
time. Responsive and
accurate most of the Responsive and and inaccurate all the
Responsiveness 2. Effectively uses accurate most of
times. accurate few times. times.
to Questions/ eye contact, the times.
7 2. Maintains eye 2. Some eye contact, 2. No eye contact and
Accuracy speaks clearly, 2. Uses eye contact
contact, speaks speaks clearly and unable to speak
10% effectively and ineffectively and
clearly with suitable unclearly in different 3. Dresses
confidently using fails to speak
volume and pace. portions. inappropriately
suitable volume clearly and audibly
and pace.
if Statement
If statement consists of a Boolean expression followed by one or more statements.
Syntax
The syntax of an ‘if’ statement in C programming language is:
if(boolean_expression)
{
/* statement(s) will execute if the boolean expression is true */
}
If-else Statement
The flow diagram as shown in figure 3.2
Syntax
if(boolean_expression)
{
/* statement(s) will execute if the boolean expression is true */
}
else
{
/* statement(s) will execute if the boolean expression is false */
}
Sample Tasks
Task 1
Write a program to assign value in any variable and check whether it is less than
20 or not
#include <iostream>
Using namespace std;
int main ()
{
int a = 10;
if( a < 20 )
{ cout<<"a is less than 20"<<endl; }
cout<<"value of a is :”<<a;}
Task 2
Write a program to assign value in to variable ‘a’ and check whether it is less
than 20 or not using if else statement
#include<iostream>
Using namespace std;
int main ()
{
int a = 100;
if( a < 20 )
{
cout<< “a is less than 20"<<endl;
}
else
{ cout<<"a is not less than 20"<<endl; }
cout<<"value of a is :"<<a<<endl;
}
Task 3
Write a program to read three integer numbers then find and print the largest
one among these numbers.
#include <iostream>
using namespace std;
int main( )
{
int x,y,z, max;
cin>>x;
cin>>y;
cin>>z;
max=x;
if(max<y)
max=y;
if(max<z)
max=z;
cout<<” The Max Value is:"<<max<<endl; }
Task 4
Write a program to executes a single statement if the given condition is true
#include <iostream>
using namespace std;
main ()
{ int a, b;
a=100;
b=50;
if(a>b)
cout<<“Lahore”<<endl;
cout<<“ok”<<endl; }
Task 5
Write a C++ program to check whether an alphabet is vowel or consonant using
switch case.
#include <iostream>
using namespace std;
int main() {
char ch;
// Ask the user to enter an alphabet
cout << "Enter an alphabet: ";
cin >> ch;
// Convert to lowercase to handle both uppercase and lowercase inputs
ch = tolower(ch);
// Check if the input is a letter
if (ch < 'a' || ch > 'z') {
cout << "Invalid input. Please enter a valid alphabet." << endl;
return 1; // Exit with an error code
}
// Use switch case to check if the character is a vowel or consonant
switch (ch) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
cout << ch << " is a vowel." << endl;
break;
default:
cout << ch << " is a consonant." << endl;
}
}
Exercise :
Exercise 1:
Write a program that determines a student’s grade. The program will read three types
of scores (quiz, mid-term, and final scores) and determine the grade based on the
following rules:
if the average score =90% =>grade=A
if the average score >= 70% and <90% => grade=B
if the average score>=50% and <70% =>grade=C
if the average score<50% =>grade=F
Solution:
Marks Obtained Total Marks
Instructor Signatures:
Remarks:
Exercise 2
Write a program that prompts the user for the current year, the user's current age, and
another year. It then calculates the age that the user was or will be in the second year
entered
Solution:
Marks Obtained Total Marks
Instructor Signatures:
Remarks:
Exercise 3
Write a program to calculate the electricity bill. The rates of electricity per unit are as
follow If the units consumed are equal or less than 300, then the cost is Rs 3/ per unit
If units consumed are more than 300, then the cost is Rs. 3.5/- per unit and a
surcharge of 5% of bill is added.
Solution:
Marks Obtained Total Marks
Instructor Signatures:
Remarks:
Exercise 4
Write a C program to create Simple Calculator using switch case.
Solution:
Marks Obtained Total Marks
Instructor Signatures:
Remarks:
Student Name:
Registration Number:
11-03-2025
Date:
Instructor Signatures:
Remarks:
The University of Lahore Department of Technology
4 of the learned upon accurate technical content to minimum technical answers to the the questions asked by
skill technical content the questions asked content to the questions asked by examiner.
Some attempt at
Little attempt at
Well-designed Good data layering data. Some Very poor attempt and
layering data.
Object classes with good abstraction and some generalization of missing code. Code is
Missing, duplicate
Oriented data abstraction data layering. Good interface evident but maintenance
6 or not useful class
Design and layering. Code set of operations missed opportunity. headache. No OOP
interface. Some
10% is highly provided. Little Code can be language feature is
OOP language
maintainable duplications maintained with used
feature is used
significant effort
1. Responds well,
quick and very
1. Generally
accurate all the 1. Not much
Responsive and 1. Generally 1. Non-Responsive
time. Responsive and
accurate most of the Responsive and and inaccurate all the
Responsiveness 2. Effectively uses accurate most of
times. accurate few times. times.
to Questions/ eye contact, the times.
7 2. Maintains eye 2. Some eye contact, 2. No eye contact and
Accuracy speaks clearly, 2. Uses eye contact
contact, speaks speaks clearly and unable to speak
10% effectively and ineffectively and
clearly with suitable unclearly in different 3. Dresses
confidently using fails to speak
volume and pace. portions. inappropriately
suitable volume clearly and audibly
and pace.
Relational Operators
The following table 4.2 shows all the relational operators supported by C++. Assume
variable A holds 10 and variable B holds 20, then:
Logical Operators
Following table 4.3 shows all the logical operators supported by C+ language.
Assume variable A holds 1 and variable B holds 0, then:
Bitwise Operators
Bitwise operators work on bits and perform bit-by-bit operation. The truth table for &,
|, and ^ is as follows as shown in Table 4.4
Sample Tasks
Task 1
Write a program to convert millimeter in to inches and display the result on the screen
#include<iostream>
using namespace std;
int main ()
{
float mm,inch;
mm=100.26 ;
inch=mm/25.4;
cout<<mm<<<inch<<”inches”;
}
Task 2
Write a program to add two floating point values and display the sum on the screen
#include<iostream>
using namespace std;
int main ()
{
float a,b,s;
a=10.2;
b=5.5;
s=a+b;
cout<<”Sum of “<< a<<”and”<<b<<”is=”<<s;
}
Task 3
Write a program that convert 37 centigrade temperature in to Fahrenheit using the
formula F=9/5 (C +32) and display the temperature in Fahrenheit.
#include<iostream>
using namespace std;
int main ()
{
float f,c;
c=37;
f=9/5 *c + 32;
cout<<c<<”centigrade=”<<f<<”Fahrenheit”; }
Exercise :
Exercise 1:
Write a program to input any two numbers from user and to perform all arithmetic
operators available in C++
Solution:
cout << "Addition (num1 + num2) = " << (num1 + num2) << endl;
cout << "Subtraction (num1 - num2) = " << (num1 - num2) << endl;
cout << "Multiplication (num1 * num2) = " << (num1 * num2) << endl;
if (num2 != 0) {
cout << "Division (num1 / num2) = " << (num1 / num2) << endl;
cout << "Modulus (num1 % num2) = " << (num1 % num2) << endl;
} else {
cout << "Division and modulus by zero is not allowed." << endl;
}
return 0;
Instructor Signatures:
Remarks:
Exercise 2
Write a program to input any two numbers from user and to perform all relational
operators
Solution:
#include <iostream>
using namespace std;
int main() {
int num1, num2;
cout << num1 << " == " << num2 << " : " << (num1 == num2) << endl;
cout << num1 << " != " << num2 << " : " << (num1 != num2) << endl;
cout << num1 << " > " << num2 << " : " << (num1 > num2) << endl;
cout << num1 << " < " << num2 << " : " << (num1 < num2) << endl;
cout << num1 << " >= " << num2 << " : " << (num1 >= num2) << endl;
cout << num1 << " <= " << num2 << " : " << (num1 <= num2) << endl;
return 0;
}
Instructor Signatures:
Remarks:
Exercise 3
Write a program to input any two numbers from user and to perform all logical
operators
Solution:
#include <iostream>
using namespace std;
int main() {
bool a, b;
return 0;
}
Instructor Signatures:
Remarks:
Exercise 4:
Write a program to swap two variables using arithmetic operation without using the
3rd variable
Solution:
#include <iostream>
using namespace std;
int main() {
int a, b;
return 0;
}
Instructor Signatures:
Remarks:
Registration Number:
Date:
Instructor Signatures:
Remarks:
The University of Lahore Department of Technology
4 of the learned upon accurate technical content to minimum technical answers to the the questions asked by
skill technical content the questions asked content to the questions asked by examiner.
Some attempt at
Little attempt at
Well-designed Good data layering data. Some Very poor attempt and
layering data.
Object classes with good abstraction and some generalization of missing code. Code is
Missing, duplicate
Oriented data abstraction data layering. Good interface evident but maintenance
6 or not useful class
Design and layering. Code set of operations missed opportunity. headache. No OOP
interface. Some
10% is highly provided. Little Code can be language feature is
OOP language
maintainable duplications maintained with used
feature is used
significant effort
1. Responds well,
quick and very
1. Generally
accurate all the 1. Not much
Responsive and 1. Generally 1. Non-Responsive
time. Responsive and
accurate most of the Responsive and and inaccurate all the
Responsiveness 2. Effectively uses accurate most of
times. accurate few times. times.
to Questions/ eye contact, the times.
7 2. Maintains eye 2. Some eye contact, 2. No eye contact and
Accuracy speaks clearly, 2. Uses eye contact
contact, speaks speaks clearly and unable to speak
10% effectively and ineffectively and
clearly with suitable unclearly in different 3. Dresses
confidently using fails to speak
volume and pace. portions. inappropriately
suitable volume clearly and audibly
and pace.
Objective :
This lab aims to help students understand and apply logical operators (&&, ||, !) in
C++ by creating truth tables, testing logical equivalences (like De Morgan’s Laws),
and practicing short-circuit evaluation. Students will implement logical expressions in
code, validate equivalences, and apply these skills to practical programming
scenarios.
Tools:
Dev C++
Background Knowledge:
Compound propositions that have the same truth values in all possible cases are called
logically equivalent. We can also define this notion as follows, “The compound
propositions p and q are called logically equivalent if p ↔ q is a tautology. The
notation p ≡ q denotes that p and q are logically equivalent.”
The following are some important logical equivalences
In today’s lab we will extend our previous lab work and use it to prove different
logical equivalences. The use of AND, OR and Not have been told earlier. This lab
will use few more logical operators which will be used to prove the logical
equivalences.
Example:
TRUE: Has a value 1.
False: Has a value 0.
NOT: used for negation.
Sample Tasks
Task 1
Write a program to proof identity law and display the result on the screen
#include<iostream>
using namespace std;
int main ()
{
int p;
cout<<” Enter a Boolean value for p”;
cin>>p;
cout<<p&True;
}
Task 2
Write a program to proof double negation law and display the rsult on the screen
#include <iostream>
using namespace std;
int main() {
bool A = true; // You can change this to false to test both cases
// Applying double negation
bool result = !( !A );
cout <<"Value of A: "<< A << endl;
cout <<"Double negation of A ( !( !A ) ): "<< result << endl;
if (result == A) {
cout <<"Double negation law holds: !( !A ) is equivalent to A"<< endl;
} else {
cout <<"Double negation law does not hold"<< endl;}}
Task 3
Write a program that veriy the commutative law in C++.
#include <iostream>
using namespace std;
int main() {
bool A = true; // You can change these values to test different cases
bool B = false;
// Applying commutative law for AND (A && B is equivalent to B && A)
bool andResult1 = A && B;
bool andResult2 = B && A;
// Displaying the results
cout <<"A && B: "<< andResult1 << endl;
cout <<"B && A: "<< andResult2 << endl;
cout <<"Commutative law for AND holds: "<< (andResult1 == andResult2) <<
endl;
}
Exercise :
Exercise 1:
Write a program to verify the distributive law by using logical operators available in
C++
Solution:
MarksObtained Total Marks
InstructorSignatures:
Remarks:
Exercise 2
Write a program to verify the Demorgan’s law by using logical operators available in
C++
Solution:
InstructorSignatures:
Remarks:
FINALEVALUTIONLAB 04
StudentName:
RegistrationNumber:
Date:
Remarks:
The University of Lahore Department of Technology
4 of the learned upon accurate technical content to minimum technical answers to the the questions asked by
skill technical content the questions asked content to the questions asked by examiner.
Some attempt at
Little attempt at
Well-designed Good data layering data. Some Very poor attempt and
layering data.
Object classes with good abstraction and some generalization of missing code. Code is
Missing, duplicate
Oriented data abstraction data layering. Good interface evident but maintenance
6 or not useful class
Design and layering. Code set of operations missed opportunity. headache. No OOP
interface. Some
10% is highly provided. Little Code can be language feature is
OOP language
maintainable duplications maintained with used
feature is used
significant effort
1. Responds well,
quick and very
1. Generally
accurate all the 1. Not much
Responsive and 1. Generally 1. Non-Responsive
time. Responsive and
accurate most of the Responsive and and inaccurate all the
Responsiveness 2. Effectively uses accurate most of
times. accurate few times. times.
to Questions/ eye contact, the times.
7 2. Maintains eye 2. Some eye contact, 2. No eye contact and
Accuracy speaks clearly, 2. Uses eye contact
contact, speaks speaks clearly and unable to speak
10% effectively and ineffectively and
clearly with suitable unclearly in different 3. Dresses
confidently using fails to speak
volume and pace. portions. inappropriately
suitable volume clearly and audibly
and pace.
Sample Tasks
Task 1
Write a program that initialize an array of 5 elements and display each element on the
screen.
#include <iostream>
using namespace std;
int main() {
int arr[5]; // Array of 5 elements
Arr[5]={2,4,6,8,10}
cout << "You entered: ";
for (int i = 0; i < 5; i++) {
cout << arr[i] << " ";
}
cout << endl;}
Task 2
Write a program that declared an array of 5 elements and take input from user and
display each element on the screen.
#include <iostream>
using namespace std;
int main() {
int arr[5]; // Array of 5 elements
cout << "Enter 5 elements: ";
for (int i = 0; i < 5; i++) {
cin >> arr[i];
}
cout << "You entered: ";
for (int i = 0; i < 5; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
Task 3
Write a program that take one sets of first five odd integers and add these numbers
and display the result on screen.
#include <iostream>
using namespace std;
int main() {
odd[5] = {1,3,5,7,9};
sum = 0;
count = 0;
Exercise :
Exercise 1:
Write a program to reverse an array in C++
Solution:
#include <iostream>
using namespace std;
int main() {
const int SIZE = 10;
int arr[SIZE];
cout << "Enter " << SIZE << " integers:" << endl;
for (int i = 0; i < SIZE; i++) {
cout << "Element " << i + 1 << ": ";
cin >> arr[i];
}
for (int i = 0; i < SIZE / 2; i++) {
int temp = arr[i];
arr[i] = arr[SIZE - 1 - i];
arr[SIZE - 1 - i] = temp;
}
cout << "Reversed array:" << endl;
for (int i = 0; i < SIZE; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
Instructor Signatures:
Remarks:
Exercise 2
Write a program to find out number of even and odd out of array of size 10.
Solution:
#include <iostream>
using namespace std;
int main() {
int numbers[10];
int evenCount = 0, oddCount = 0;
cout << "Enter 10 integers:" << endl;
for (int i = 0; i < 10; i++) {
cout << "Enter number " << i + 1 << ": ";
cin >> numbers[i];
if (numbers[i] % 2 == 0) {
evenCount++;
} else {
oddCount++;
}
}
cout << "Number of even numbers: " << evenCount << endl;
cout << "Number of odd numbers: " << oddCount << endl;
return 0;
}
Instructor Signatures:
Remarks:
Date:
Instructor Signatures:
Remarks:
The University of Lahore Department of Technology
4 of the learned upon accurate technical content to minimum technical answers to the the questions asked by
skill technical content the questions asked content to the questions asked by examiner.
Some attempt at
Little attempt at
Well-designed Good data layering data. Some Very poor attempt and
layering data.
Object classes with good abstraction and some generalization of missing code. Code is
Missing, duplicate
Oriented data abstraction data layering. Good interface evident but maintenance
6 or not useful class
Design and layering. Code set of operations missed opportunity. headache. No OOP
interface. Some
10% is highly provided. Little Code can be language feature is
OOP language
maintainable duplications maintained with used
feature is used
significant effort
1. Responds well,
quick and very
1. Generally
accurate all the 1. Not much
Responsive and 1. Generally 1. Non-Responsive
time. Responsive and
accurate most of the Responsive and and inaccurate all the
Responsiveness 2. Effectively uses accurate most of
times. accurate few times. times.
to Questions/ eye contact, the times.
7 2. Maintains eye 2. Some eye contact, 2. No eye contact and
Accuracy speaks clearly, 2. Uses eye contact
contact, speaks speaks clearly and unable to speak
10% effectively and ineffectively and
clearly with suitable unclearly in different 3. Dresses
confidently using fails to speak
volume and pace. portions. inappropriately
suitable volume clearly and audibly
and pace.
Objective :
In this lab we understand the working of bubble sort and selection sort and
implementation of these algorithms in C++.
Tools:
Dev C++
Background Knowledge:
Sorting algorithms are techniques used to arrange elements in a list or array in a
particular order (e.g., ascending or descending). They are fundamental in computer
science and come in various types, each with different performance characteristics
and use cases.
Bubble Sort is a simple sorting algorithm that repeatedly steps through the list,
compares adjacent elements, and swaps them if they are in the wrong order. The pass
through the list is repeated until the list is sorted.
Selection Sort is a sorting algorithm that divides the input list into two parts: the
sorted part and the unsorted part. It repeatedly selects the smallest (or largest) element
from the unsorted part and moves it to the sorted part.
Sample Tasks
Task 1
Write an algorithm of bubble sorting.
Solution:
1. Start
2. Input the array and its size n
3. Repeat for i from 0 to n - 2
a. Repeat for j from 0 to n - i - 2
b. If array[j] > array[j + 1]
Swap array[j] and array[j + 1]
4. End Repeat (inner loop)
5. End Repeat (outer loop)
6. Display the sorted array
7. Stop
Task 2
Write an algorithm for selection sorting.
Solution:
1. Start
2. Input the array and its size n
3. Repeat for i from 0 to n - 2
a. Set minIndex = i
b. Repeat for j from i + 1 to n - 1
If array[j] < array[minIndex]
Set minIndex = j
c. If minIndex != i
Swap array[i] and array[minIndex]
4. End Repeat
5. Display the sorted array
6. Stop
Exercise :
Exercise 1:
Write down the code for bubble sorting in C++.
Solution:
#include <iostream>
using namespace std;
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = size
Marks Obtained Total Marks
Instructor Signatures:
Remarks:
Exercise 2
Write a program which display the array after sorting by using selection
algorithm in c++.
Solution:
#include <iostream>
using namespace std;
if (minIndex != i) {
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
}
int main() {
int arr[] = {29, 10, 14, 37, 13};
int n = sizeof(arr) / sizeof(arr[0]);
selectionSort(arr, n);
Instructor Signatures:
Remarks:
Registration Number:
Date:
Instructor Signatures:
Remarks:
The University of Lahore Department of Technology
4 of the learned upon accurate technical content to minimum technical answers to the the questions asked by
skill technical content the questions asked content to the questions asked by examiner.
Some attempt at
Little attempt at
Well-designed Good data layering data. Some Very poor attempt and
layering data.
Object classes with good abstraction and some generalization of missing code. Code is
Missing, duplicate
Oriented data abstraction data layering. Good interface evident but maintenance
6 or not useful class
Design and layering. Code set of operations missed opportunity. headache. No OOP
interface. Some
10% is highly provided. Little Code can be language feature is
OOP language
maintainable duplications maintained with used
feature is used
significant effort
1. Responds well,
quick and very
1. Generally
accurate all the 1. Not much
Responsive and 1. Generally 1. Non-Responsive
time. Responsive and
accurate most of the Responsive and and inaccurate all the
Responsiveness 2. Effectively uses accurate most of
times. accurate few times. times.
to Questions/ eye contact, the times.
7 2. Maintains eye 2. Some eye contact, 2. No eye contact and
Accuracy speaks clearly, 2. Uses eye contact
contact, speaks speaks clearly and unable to speak
10% effectively and ineffectively and
clearly with suitable unclearly in different 3. Dresses
confidently using fails to speak
volume and pace. portions. inappropriately
suitable volume clearly and audibly
and pace.
Task 2
Write a program that take input frm the user and print the 2D array on the
output screen.
#include <iostream>
using namespace std;
int main() {
// Define the dimensions of the 2D array
int rows, cols;
cout << "Enter the number of rows: ";
cin >> rows;
cout << "Enter the number of columns: ";
cin >> cols;
int array[rows][cols];
// Input elements of the 2D array
cout << "Enter the elements of the 2D array:" << endl;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << "Element at [" << i << "][" << j << "]: ";
cin >> array[i][j];}}
Task 3
Write a program for addition of two 2-D arrays in C++.
#include <iostream>
using namespace std;
void addMatrices(int rows, int cols, int A[][10], int B[][10], int C[][10]) {
for(int i = 0; i < rows; ++i)
for(int j = 0; j < cols; ++j)
C[i][j] = A[i][j] + B[i][j];
}
int main() {
int A[10][10] = { {1, 2}, {3, 4} };
zz int B[10][10] = { {5, 6}, {7, 8} };
int C[10][10];
int rows = 2, cols = 2;
Exercise :
Exercise 1:
Write a program to compute and display the sum of each row and each column in a
2D array.
Solution:
#include <iostream>
using namespace std;
int main() {
const int rows = 3;
const int cols = 4;
int arr[rows][cols] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
return 0;
}
Instructor Signatures:
Remarks:
Exercise 2
Write a program to find the maximum element in a 2D array.
Solution:
#include <iostream>
using namespace std;
int main() {
const int rows = 3;
const int cols = 4;
int arr[rows][cols] = {
{7, 2, 9, 4},
{5, 16, 1, 8},
{3, 10, 6, 12}
};
cout << "The maximum element in the 2D array is: " << maxElement << endl;
return 0;
}
Instructor Signatures:
Remarks:
Exercise 3
Write a program to add two matrices of the same size.
Solution:
#include <iostream>
using namespace std;
int main() {
const int rows = 3;
const int cols = 3;
int matrix1[rows][cols] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int matrix2[rows][cols] = {
{9, 8, 7},
{6, 5, 4},
{3, 2, 1}
};
int sum[rows][cols];
return 0;
}
Marks Obtained Total Marks
Instructor Signatures:
Remarks:
Registration Number:
Date:
Instructor Signatures:
Remarks:
The University of Lahore Department of Technology
4 of the learned upon accurate technical content to minimum technical answers to the the questions asked by
skill technical content the questions asked content to the questions asked by examiner.
Some attempt at
Little attempt at
Well-designed Good data layering data. Some Very poor attempt and
layering data.
Object classes with good abstraction and some generalization of missing code. Code is
Missing, duplicate
Oriented data abstraction data layering. Good interface evident but maintenance
6 or not useful class
Design and layering. Code set of operations missed opportunity. headache. No OOP
interface. Some
10% is highly provided. Little Code can be language feature is
OOP language
maintainable duplications maintained with used
feature is used
significant effort
1. Responds well,
quick and very
1. Generally
accurate all the 1. Not much
Responsive and 1. Generally 1. Non-Responsive
time. Responsive and
accurate most of the Responsive and and inaccurate all the
Responsiveness 2. Effectively uses accurate most of
times. accurate few times. times.
to Questions/ eye contact, the times.
7 2. Maintains eye 2. Some eye contact, 2. No eye contact and
Accuracy speaks clearly, 2. Uses eye contact
contact, speaks speaks clearly and unable to speak
10% effectively and ineffectively and
clearly with suitable unclearly in different 3. Dresses
confidently using fails to speak
volume and pace. portions. inappropriately
suitable volume clearly and audibly
and pace.
Objective :
In a C++ Understand the working of various sorting algorithms . Implement and
compare the performance of sorting techniques.Explore the applications of sorting in
data organization.These objectives focus on both understanding the theoretical
concepts of sorting algorithms and gaining practical programming skills to manipulate
these algorithms.
Tools:
Dev C++
Background Knowledge:
Sorting is the process of arranging data in a specific order, either ascending or
descending. Sorting is crucial in computer science for optimizing search algorithms
and organizing data.
Types of Sorting Algorithms:
Bubble Sort - Repeatedly compares adjacent elements and swaps them if they are in
the wrong order.
Selection Sort - Finds the smallest element in the array and places it at the beginning.
Insertion Sort - Builds the sorted array one element at a time by inserting it in the
correct position.
Merge Sort - Divides the array into halves, recursively sorts them, and merges the
sorted halves.
Quick Sort - Divides the array using a pivot, then recursively sorts the partitions.
Heap Sort - Uses a binary heap structure to sort the array.
Sample Tasks
Task 1
Write a program to Implement the Bubble Sort algorithm.
#include <iostream>
using namespace std;
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap arr[j] and arr[j+1]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);
Task 2
Write a program that implement the Selection Sort algorithm.
#include <iostream>
using namespace std;
void selectionSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex])
minIndex = j;
}
// Swap the found minimum element with the first element
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
int main() {
int arr[] = {29, 10, 14, 37, 13};
int n = sizeof(arr) / sizeof(arr[0]);
selectionSort(arr, n);
cout << "Sorted array: ";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}
Task 3
Write a program to implement Insertion Sort in C++.
#include <iostream>
using namespace std;
void insertionSort(int arr[], int n) {
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
// Move elements of arr[0..i-1], that are greater than key, one position ahead
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
int main() {
int arr[] = {12, 11, 13, 5, 6};
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
cout << "Sorted array: ";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
return 0;
}
Exercise :
Exercise 1:
Write a program to implement Merge Sort in C++.
Solution:
#include <iostream>
using namespace std;
int i = 0, j = 0, k = l;
delete[] L;
delete[] R;
}
int main() {
int n;
cout << "Enter number of elements: ";
cin >> n;
mergeSort(arr, 0, n - 1);
delete[] arr;
return 0;
}
Instructor Signatures:
Remarks:
Exercise 2
Write a program to implement quick Sort.
Solution:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter number of elements: ";
cin >> n;
delete[] arr;
return 0;
}
Instructor Signatures:
Remarks:
Registration Number:
Date:
Instructor Signatures:
Remarks:
The University of Lahore Department of Technology
4 of the learned upon accurate technical content to minimum technical answers to the the questions asked by
skill technical content the questions asked content to the questions asked by examiner.
Some attempt at
Little attempt at
Well-designed Good data layering data. Some Very poor attempt and
layering data.
Object classes with good abstraction and some generalization of missing code. Code is
Missing, duplicate
Oriented data abstraction data layering. Good interface evident but maintenance
6 or not useful class
Design and layering. Code set of operations missed opportunity. headache. No OOP
interface. Some
10% is highly provided. Little Code can be language feature is
OOP language
maintainable duplications maintained with used
feature is used
significant effort
1. Responds well,
quick and very
1. Generally
accurate all the 1. Not much
Responsive and 1. Generally 1. Non-Responsive
time. Responsive and
accurate most of the Responsive and and inaccurate all the
Responsiveness 2. Effectively uses accurate most of
times. accurate few times. times.
to Questions/ eye contact, the times.
7 2. Maintains eye 2. Some eye contact, 2. No eye contact and
Accuracy speaks clearly, 2. Uses eye contact
contact, speaks speaks clearly and unable to speak
10% effectively and ineffectively and
clearly with suitable unclearly in different 3. Dresses
confidently using fails to speak
volume and pace. portions. inappropriately
suitable volume clearly and audibly
and pace.
int main() {
int arr[] = {10, 23, 45, 70, 11, 15};
int n = sizeof(arr) / sizeof(arr[0]);
int key = 70;
int result = linearSearch(arr, n, key);
if (result != -1)
cout << "Element found at index " << result << endl;
else
cout << "Element not found" << endl;
return 0;
}
Task 2
Write a program to implement Binary Search in C++.
#include <iostream>
using namespace std;
if (arr[mid] == key)
return mid; // Element found
else if (arr[mid] < key)
low = mid + 1; // Search in the right half
else
high = mid - 1; // Search in the left half
}
return -1; // Element not found
}
int main() {
int arr[] = {10, 20, 30, 40, 50, 60, 70};
int n = sizeof(arr) / sizeof(arr[0]);
int key = 50;
int result = binarySearch(arr, 0, n - 1, key);
if (result != -1)
cout << "Element found at index " << result << endl;
else
cout << "Element not found" << endl;
return 0;
}
Exercise :
Exercise 1:
Write a program that modify the Binary Search program to find the last occurrence of
an element in a sorted array.
Solution:
#include <iostream>
using namespace std;
if (arr[mid] == x) {
result = mid; // Record this occurrence
low = mid + 1; // Search to the right to find last occurrence
} else if (arr[mid] < x) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return result;
}
int main() {
int n;
cout << "Enter number of elements: ";
cin >> n;
int x;
cout << "Enter element to find last occurrence: ";
cin >> x;
if (index != -1)
cout << "Last occurrence of " << x << " is at index " << index << endl;
else
cout << x << " not found in the array." << endl;
delete[] arr;
return 0;
}
Remarks:
Exercise 2
Write a program that implement a recursive version of Binary Search.
Solution:
#include <iostream>
using namespace std;
if (arr[mid] == x)
return mid;
else if (arr[mid] > x)
return recursiveBinarySearch(arr, low, mid - 1, x);
else
return recursiveBinarySearch(arr, mid + 1, high, x);
}
int main() {
int n;
cout << "Enter number of elements: ";
cin >> n;
if (index != -1)
cout << "Element " << x << " found at index " << index << endl;
else
cout << "Element not found in the array." << endl;
delete[] arr;
return 0;
}
Instructor Signatures:
Remarks:
Student Name:
Registration Number:
Date:
Instructor Signatures:
Remarks:
The University of Lahore Department of Technology
4 of the learned upon accurate technical content to minimum technical answers to the the questions asked by
skill technical content the questions asked content to the questions asked by examiner.
Some attempt at
Little attempt at
Well-designed Good data layering data. Some Very poor attempt and
layering data.
Object classes with good abstraction and some generalization of missing code. Code is
Missing, duplicate
Oriented data abstraction data layering. Good interface evident but maintenance
6 or not useful class
Design and layering. Code set of operations missed opportunity. headache. No OOP
interface. Some
10% is highly provided. Little Code can be language feature is
OOP language
maintainable duplications maintained with used
feature is used
significant effort
1. Responds well,
quick and very
1. Generally
accurate all the 1. Not much
Responsive and 1. Generally 1. Non-Responsive
time. Responsive and
accurate most of the Responsive and and inaccurate all the
Responsiveness 2. Effectively uses accurate most of
times. accurate few times. times.
to Questions/ eye contact, the times.
7 2. Maintains eye 2. Some eye contact, 2. No eye contact and
Accuracy speaks clearly, 2. Uses eye contact
contact, speaks speaks clearly and unable to speak
10% effectively and ineffectively and
clearly with suitable unclearly in different 3. Dresses
confidently using fails to speak
volume and pace. portions. inappropriately
suitable volume clearly and audibly
and pace.
Example Explained
Declared functions are not executed immediately. They are "saved for later use",
and will be executed later, when they are called.To call a function, write the
function's name followed by two parentheses () and a semicolon ;
Sample Tasks
Task 1
Write a program to define and call a simple function in C++.
#include <iostream>
using namespace std;
// Function declaration
void greet() {
cout << "Hello, welcome to C++ functions!" << endl;
}
int main() {
greet(); // Function call
return 0;
}
Task 2
Write a program to calculate the sum of any two numbers by using functions in
C++.
#include <iostream>
using namespace std;
int main() {
add(5, 10); // Function call
return 0;
}
Task 3
Write a program to multiply any two numbers and print the result on the output
screen by creating a function that returns a value.
#include <iostream>
using namespace std;
int main() {
int result = multiply(4, 5);
cout << "The product is: " << result << endl;
return 0;}
Task 4
Write a program to demonstrate pass by reference concept and implement
swapping of two values by using functions in C++.
#include <iostream>
using namespace std;
int main() {
int x = 10, y = 20;
cout << "Before swapping: x = " << x << ", y = " << y << endl;
swap(x, y);
cout << "After swapping: x = " << x << ", y = " << y << endl;
return 0;
}
Task 5
Write a program to find the sum of elements in an array passed to a function.
#include <iostream>
using namespace std;
int main() {
int numbers[] = {1, 2, 3, 4, 5};
int size = sizeof(numbers) / sizeof(numbers[0]);
cout << "Sum of the array is: " << sumArray(numbers, size) << endl;
return 0;
}
Task 6
Write a program to double the values of elements in an array using a function.
#include <iostream>
using namespace std;
int main() {
int numbers[] = {1, 2, 3, 4, 5};
int size = sizeof(numbers) / sizeof(numbers[0]);
doubleArray(numbers, size);
Exercise :
Exercise 1:
Write a program to calculate the total price of items with a default discount by
using functions in C++.
Solution:
Marks Obtained Total Marks
Instructor Signatures:
Remarks:
Exercise 2
Write a program to calculate the average of an array with a fixed size passed to a
function.
Solution:
Marks Obtained Total Marks
Instructor Signatures:
Remarks:
Exercise 3
Write a program to convert temperature from farenhite to celcius using functions
in C++.
Solution:
Marks Obtained Total Marks
Instructor Signatures:
Remarks:
Registration Number:
Date:
Instructor Signatures:
Remarks:
The University of Lahore Department of Technology
4 of the learned upon accurate technical content to minimum technical answers to the the questions asked by
skill technical content the questions asked content to the questions asked by examiner.
Some attempt at
Little attempt at
Well-designed Good data layering data. Some Very poor attempt and
layering data.
Object classes with good abstraction and some generalization of missing code. Code is
Missing, duplicate
Oriented data abstraction data layering. Good interface evident but maintenance
6 or not useful class
Design and layering. Code set of operations missed opportunity. headache. No OOP
interface. Some
10% is highly provided. Little Code can be language feature is
OOP language
maintainable duplications maintained with used
feature is used
significant effort
1. Responds well,
quick and very
1. Generally
accurate all the 1. Not much
Responsive and 1. Generally 1. Non-Responsive
time. Responsive and
accurate most of the Responsive and and inaccurate all the
Responsiveness 2. Effectively uses accurate most of
times. accurate few times. times.
to Questions/ eye contact, the times.
7 2. Maintains eye 2. Some eye contact, 2. No eye contact and
Accuracy speaks clearly, 2. Uses eye contact
contact, speaks speaks clearly and unable to speak
10% effectively and ineffectively and
clearly with suitable unclearly in different 3. Dresses
confidently using fails to speak
volume and pace. portions. inappropriately
suitable volume clearly and audibly
and pace.
Objective :
he goal of this lab manual is to help students understand and implement recursive
functions in C++. By solving various tasks, students will learn how recursion simplifies
complex problems and when to use it effectively. The manual also aims to emphasize
the importance of base cases and stack memory in recursive calls.
Tools:
Dev C++
Background Knowledge:
A function is a block of code which only runs when it is called.You can pass data,
known as parameters, into a function.Functions are used to perform certain actions, and
they are important for reusing code: Define the code once, and use it many times.
To create (often referred to as declare) a function, specify the name of the function,
followed by parentheses ():
void myFunction() {
// code to be executed
}
Example Explained
Declared functions are not executed immediately. They are "saved for later use",
and will be executed later, when they are called.To call a function, write the
function's name followed by two parentheses () and a semicolon ;
Sample Tasks
Task 1
Write a program to calculate the factorial of a number using recursion. in C++.
#include <iostream>
using namespace std;
int factorial(int n) {
if (n == 0) // Base case
return 1;
return n * factorial(n - 1); // Recursive call
}
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
cout << "Factorial of " << num << " is: " << factorial(num) << endl;
return 0;
}
Task 2
Write a program to Generate the Fibonacci sequence using recursion in C++.
#include <iostream>
using namespace std;
int fibonacci(int n) {
if (n == 0) return 0; // Base case
if (n == 1) return 1; // Base case
return fibonacci(n - 1) + fibonacci(n - 2); // Recursive call
}
int main() {
int n;
cout << "Enter the position of Fibonacci number: ";
cin >> n;
cout << "Fibonacci number at position " << n << " is: " << fibonacci(n) << endl;
return 0;
}
Exercise :
Exercise 1:
Write a program to find the greatest common divisor (GCD) of two numbers using
recursion. in C++.
Solution:
Instructor Signatures:
Remarks:
FINAL EVALUTION LAB 12
Student Name:
Registration Number:
Date:
Instructor Signatures:
Remarks:
The University of Lahore Department of Technology
4 of the learned upon accurate technical content to minimum technical answers to the the questions asked by
skill technical content the questions asked content to the questions asked by examiner.
Some attempt at
Little attempt at
Well-designed Good data layering data. Some Very poor attempt and
layering data.
Object classes with good abstraction and some generalization of missing code. Code is
Missing, duplicate
Oriented data abstraction data layering. Good interface evident but maintenance
6 or not useful class
Design and layering. Code set of operations missed opportunity. headache. No OOP
interface. Some
10% is highly provided. Little Code can be language feature is
OOP language
maintainable duplications maintained with used
feature is used
significant effort
1. Responds well,
quick and very
1. Generally
accurate all the 1. Not much
Responsive and 1. Generally 1. Non-Responsive
time. Responsive and
accurate most of the Responsive and and inaccurate all the
Responsiveness 2. Effectively uses accurate most of
times. accurate few times. times.
to Questions/ eye contact, the times.
7 2. Maintains eye 2. Some eye contact, 2. No eye contact and
Accuracy speaks clearly, 2. Uses eye contact
contact, speaks speaks clearly and unable to speak
10% effectively and ineffectively and
clearly with suitable unclearly in different 3. Dresses
confidently using fails to speak
volume and pace. portions. inappropriately
suitable volume clearly and audibly
and pace.
Objective :
he goal of this lab manual is to understand and implement different concepts of
discrete structures i in C++. By solving various tasks, students will learn how
simplifies complex problems and when to use it effectively.
Tools:
Dev C++
Exercise 1:
Truth Table Generator
Objective: Implement a program to generate a truth table for a given logical
expression.
Problem Statement:
Create a program that takes a logical expression (e.g., A∧B∨¬AA \land B \lor \neg
AA∧B∨¬A) and generates a truth table. Allow users to input the number of variables
and the logical expression.
Hints:
Use arrays to store variable values for each row of the truth table.
Implement operators like AND (&&), OR (||), and NOT (!).
Solution:
#include <iostream>
using namespace std;
int main() {
bool A, B;
return 0;
}
Instructor Signatures:
Remarks:
Exercise 2:
Solving Recurrence Relations
Objective: Implement solutions to recurrence relations programmatically.
Scenario:
Write a program to solve recurrence relations like:
1. Fibonacci sequence (T(n)=T(n−1)+T(n−2)T(n) = T(n-1) + T(n-
2)T(n)=T(n−1)+T(n−2))
2. Towers of Hanoi (T(n)=2T(n−1)+1T(n) = 2T(n-1) + 1T(n)=2T(n−1)+1)
3. Any custom recurrence relation provided as input.
Solution:
#include <iostream>
#include <cmath>
using namespace std;
int fibonacci(int n) {
if (n == 0) return 0;
if (n == 1) return 1;
return fibonacci(n - 1) + fibonacci(n - 2);
}
int fibonacciClosed(int n) {
double phi = (1 + sqrt(5)) / 2;
return round(pow(phi, n) / sqrt(5));
}
int towerOfHanoi(int n) {
if (n == 1) return 1;
return 2 * towerOfHanoi(n - 1) + 1;
}
int towerOfHanoiClosed(int n) {
return (1 << n) - 1;
}
int main() {
int choice, n;
cout << "Choose recurrence:\n";
cout << "1. Fibonacci\n";
cout << "2. Tower of Hanoi\n";
cout << "Enter choice: ";
cin >> choice;
if (choice == 1) {
cout << "Fibonacci Recursive: " << fibonacci(n) << endl;
cout << "Fibonacci Closed-form: " << fibonacciClosed(n) << endl;
} else if (choice == 2) {
cout << "Hanoi Recursive: " << towerOfHanoi(n) << endl;
cout << "Hanoi Closed-form: " << towerOfHanoiClosed(n) << endl;
} else {
cout << "Invalid choice." << endl;
}
return 0;
}
Instructor Signatures:
Remarks:
FINAL EVALUTION LAB 13
Student Name:
Registration Number:
Date:
Instructor Signatures:
Remarks:
The University of Lahore Department of Technology
4 of the learned upon accurate technical content to minimum technical answers to the the questions asked by
skill technical content the questions asked content to the questions asked by examiner.
Some attempt at
Little attempt at
Well-designed Good data layering data. Some Very poor attempt and
layering data.
Object classes with good abstraction and some generalization of missing code. Code is
Missing, duplicate
Oriented data abstraction data layering. Good interface evident but maintenance
6 or not useful class
Design and layering. Code set of operations missed opportunity. headache. No OOP
interface. Some
10% is highly provided. Little Code can be language feature is
OOP language
maintainable duplications maintained with used
feature is used
significant effort
1. Responds well,
quick and very
1. Generally
accurate all the 1. Not much
Responsive and 1. Generally 1. Non-Responsive
time. Responsive and
accurate most of the Responsive and and inaccurate all the
Responsiveness 2. Effectively uses accurate most of
times. accurate few times. times.
to Questions/ eye contact, the times.
7 2. Maintains eye 2. Some eye contact, 2. No eye contact and
Accuracy speaks clearly, 2. Uses eye contact
contact, speaks speaks clearly and unable to speak
10% effectively and ineffectively and
clearly with suitable unclearly in different 3. Dresses
confidently using fails to speak
volume and pace. portions. inappropriately
suitable volume clearly and audibly
and pace.
Objective :
he goal of this lab manual is to understand and implement different concepts of
discrete structures i in C++. By solving various tasks, students will learn how
simplifies complex problems and when to use it effectively.
Tools:
Dev C++
Exercise 1:
Lab Tasks:
Sorting:
Solution:
Marks Obtained Total Marks
Instructor Signatures:
Remarks:
FINAL EVALUTION LAB 14
Student Name:
Registration Number:
Date:
Instructor Signatures:
Remarks:
The University of Lahore Department of Technology
4 of the learned upon accurate technical content to minimum technical answers to the the questions asked by
skill technical content the questions asked content to the questions asked by examiner.
Some attempt at
Little attempt at
Well-designed Good data layering data. Some Very poor attempt and
layering data.
Object classes with good abstraction and some generalization of missing code. Code is
Missing, duplicate
Oriented data abstraction data layering. Good interface evident but maintenance
6 or not useful class
Design and layering. Code set of operations missed opportunity. headache. No OOP
interface. Some
10% is highly provided. Little Code can be language feature is
OOP language
maintainable duplications maintained with used
feature is used
significant effort
1. Responds well,
quick and very
1. Generally
accurate all the 1. Not much
Responsive and 1. Generally 1. Non-Responsive
time. Responsive and
accurate most of the Responsive and and inaccurate all the
Responsiveness 2. Effectively uses accurate most of
times. accurate few times. times.
to Questions/ eye contact, the times.
7 2. Maintains eye 2. Some eye contact, 2. No eye contact and
Accuracy speaks clearly, 2. Uses eye contact
contact, speaks speaks clearly and unable to speak
10% effectively and ineffectively and
clearly with suitable unclearly in different 3. Dresses
confidently using fails to speak
volume and pace. portions. inappropriately
suitable volume clearly and audibly
and pace.