DSA Lab 03
DSA Lab 03
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.
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)
Functions:
1) virtual Type arrayIntersection(arr2, size2) = 0;
Should find common elements in the given 2 arrays.
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’.
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:
Output:
The subarray does not exist in the main array in the exact order
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:
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
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:
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
● 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];
}
● Code 6
● Code 7