0% found this document useful (0 votes)
3 views9 pages

DSA Lab 03

Introduction of Stacks to Practice Stacks introductory QUESTIONS

Uploaded by

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

DSA Lab 03

Introduction of Stacks to Practice Stacks introductory QUESTIONS

Uploaded by

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

University of Central Punjab

Faculty of Information Technology


Data Structures and Algorithms
Spring 2025

Lab 03
Topic ● Understanding Classes
● Working on Three Different files
● Abstract Classes
● Templates
● Arrays
● Big-O time complexity
The basic purpose of this lab is to revise some preliminary concepts of C++ that
Objective
has been covered in the course of Introduction to Computing and
Programming Fundamentals and Object Oriented Programming.

Solve real-world problems skillfully with precision using programming


CLO constructs learned in theory with the course toolkit.
Statement

Instructions:
• Indent your code.
• Comment your code.
• Use meaningful variable names.
• Plan your code carefully on a piece of paper before you implement it.
• Name of the program should be the same as the task name. i.e. the first program
should be Task_1.cpp
• void main() is not allowed. Use int main()
• You have to work in multiple files. i.e separate .h and .cpp files
• You are not allowed to use any built-in functions
• You are required to follow the naming conventions as follow:
o Variables: firstName; (no underscores allowed)
o Function: getName(); (no underscores allowed)
o ClassName: BankAccount (no underscores allowed)

Students are required to complete the following tasks in lab timings.


Task 1
Create a C++ generic abstract class named as List, with the following:
Attributes:
1. Type * arr;
2. int maxSize;
3. int currentSize;

Functions:
1) virtual Type arrayIntersection(arr2, size2) = 0;
 Should find common elements in the given 2 arrays.

2) virtual void findSubArray(subArr, subSize) = 0;


 Should check whether the given sub array exist in the main array or not.

3) virtual void longestEqualBinaryArray() = 0;


 Should return or print the longest sub array having equal number of 0s and 1s.

Now use the above class to make another derived class named as MyList.
Now implement the pure virtual functions declared in base class, in the
derived class ‘MyBST’.

1) arrayIntersection(arr2, size2): Implement the to find the intersection of two sorted


arrays of sizes n and m. The function should return the common elements present in both
arrays.

Example:

 Input:

 Array 1: [1, 3, 4, 7] (n = 4)
 Array 2: [2, 4, 6, 7, 8] (m = 5)

 Output:

 Intersection: [4, 7]
2) findSubArray(subArr, subSize): Write function to check whether the elements of a
given subarray exist in the main array in the exact order. The function should return or
print a meesage, whether the given subarray is a proper subarray of the main array.

Example 1:
 Input:
 Main Array: [1, 2, 3, 4, 5, 6] (mainSize = 6)
 Subarray: [3, 4, 5] (subSize = 3)
 Output:
 The subarray exists in the main array in the exact order

Example 2:
 Input:

 Main Array: [7, 8, 9, 10, 11] (mainSize = 5)


 Subarray: [9, 10, 12] (subSize = 3)

 Output:

 The subarray does not exist in the main array in the exact order

3) longestEqualBinaryArray(): Write a function to find the length of the longest


contiguous sub-array in a binary array (consisting only of 0s and 1s) that contains an equal
number of 0s and 1s.

Example 1:
 Input: Array: [0, 1, 0, 1, 0, 1, 1] (size = 7)
 Output: Length: 6, Longest Subarray: [0, 1, 0, 1, 0, 1]

Example 2:
 Input: Array: [1, 0, 0, 1, 1, 1] (size = 6)
 Output: Length: 6, Longest Subarray: [0, 0, 1, 1]
Task 2
Using the class made in task 1, include the following additional functionalities as
well in MyList class:

1) primeCount(): Write a function to count the prime numbers numbers in a given


array of integers and print them.

Example:
 Input: Array: [1, 2, 3, 4, 5, 10, 15, 17] (size = 8)
 Output: The prime numbers are 2, 3, 5, 11, 17.
 Total count of Prime Numbers = 5

2) findDistinctPrimes(): The function should find or identify distinct prime numbers


in the array and then stores them in another array and return it. The function should
also manage the size of array accordingly if another distinct prime number is being
found.

Example 1:
 Input: Array: [1, 2, 3, 4, 5, 5, 8, 9, 13, 13, 15] (size = 11)
 Output: 2, 3, 5, 13

Example 2:
 Input: Array: [4, 5, 5, 8, 9, 12, 14, 17, 17, 19, 21, 23] (size = 12)
 Output: 5, 17, 19, 23
Task 3
Compute the complexity of the following codes, according to their line by line execution.
Finally compute their big(O) complexity.

Algorithm Example:
1. int Max = 0 //assume S[N] is filled with +ve numbers
2. For (int I =
0;I<N;I++) 3. {
4. If (S[I] > Max)
5. Max =
S[I] 6. }
7. cout<< Max

Solution:

Instruction No. of Times Executed Cost

1 1 1

2 N N

3 - -

4 N N

5 N N

6 - -

7 1 1

So f(n) = 1 + N + N + N + 1 = 2 + 3N = 3N+2
so f(n) = O (N) or O (n)

● Code 1
void printFirstElementOfArray(int arr[])
{
cout<<“First element of array :”<<arr[0];
}
● Code 2
void printAllElementOfArray(int arr[], int size)
{
for (int i = 0; i < size; i++)
{
cout<< arr[i];
}
}

● Code 3

void printAllPossibleOrderedPairs(int arr[], int size)


{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
cout<< “(”<<arr[i]<<”,”<< arr[j]<<”)”;
}
}
}

● Code 4
void selectCourse(int year, string interest, char grade)
{
if (year == 1)
{
if (interest == "science")
{
if (grade >= 'B')
{
cout << "Recommended Course: Advanced Biology" << endl;
}
else
{
cout << "Recommended Course: Intro to Biology" << endl;
}
}
else if (interest == "arts")
{
if (grade >= 'B')
{
cout << "Recommended Course: Advanced Art History" << endl;
}
else
{
cout << "Recommended Course: Intro to Art History" << endl;
}
}
else
{
cout << "Recommended Course: General Studies" << endl;
}
}
else
{
cout << "Choose advanced courses based on your major." << endl;
}
}
● Code 5
void printAllNumbersThenAllPairSums(int arr[], int size)
{
for (int i = 0; i < size; i++)
{
cout<<, arr[i];
}

for (int i = 0; i < size; i++)


{
for (int j = 0; j < size; j++)
{
cout<< arr[i] + arr[j];
}
}
}

● Code 6

void phytagorean(int value)


{
for(int i = 1; i <= value; i++)
{
for(int j = 1; j <= value; j++)
{
for(int k = 1; k <= value; k++)
{
int num1 = (i*i) +
(j*j); int num2 =
(k*k);
if (num1 == num2)
cout<<"Pair is: (" << i << ", " << j << ", " << k << ")" << endl;
}
}
}
}

● Code 7

void RangeCheck(int arr[],int sixe, int num1, int


num2){ int counter = 0;
for (int i=0; i<sixe; i++)
{
if (arr[i] >= num1 && arr[i] <= num2)
{
counter++;
cout << arr[i] << "is in the range " << endl;
}
}
cout << "Total Numbers in range are: " << counter << endl;
}

You might also like