Projects and Assignment
Projects and Assignment
Lab report
Submitted By
HADIA ABID (BSAI-298)
Ajiya Asif (BSAI-057)
Submitted to
Ayesha Jarral
Lab task 1:
#include <iostream>
using namespace std;
int main() {
const int SIZE = 5;
int arr[SIZE];
cout << "Enter 5 integers:" << endl;
for (int i = 0; i < SIZE; i++) {
cout << "Element " << (i + 1) << ": ";
cin >> arr[i];
}
printArray(arr, SIZE);
int sum = findSum(arr, SIZE);
cout << "Sum of array elements: " << sum << endl;
double average = findAverage(arr, SIZE);
cout << "Average of array elements: " << average << endl;
int max = findMax(arr, SIZE);
cout << "Maximum value in the array: " << max << endl;
return 0;
}
Output:
int main() {
int arr[] = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19};
int size = sizeof(arr) / sizeof(arr[0]);
while (true) {
cout << "Menu:\n1. Linear Search\n2. Binary Search\n3. Exit\nEnter your choice: ";
int choice;
cin >> choice;
switch (choice) {
case 1: {
int key;
cout << "Enter the element to search: ";
cin >> key;
int result = linearSearch(arr, size, key);
if (result != -1) {
cout << "Element " << key << " found at index " << result << "." << endl;
} else {
cout << "Element " << key << " not found in the array." << endl;
}
break;
}
case 2: {
int key;
cout << "Enter the element to search: ";
cin >> key;
int result = binarySearch(arr, size, key);
if (result != -1) {
cout << "Element " << key << " found at index " << result << "." << endl;
} else {
cout << "Element " << key << " not found in the array." << endl;
}
break;
}
case 3:
cout << "Exiting the program." << endl;
return 0;
default:
cout << "Invalid choice. Please choose 1, 2, or 3." << endl;
}
}
return 0;
}
Output:
Lab report 3:
#include <iostream>
using namespace std;
int main() {
int choice;
const int size = 10;
int arr[size] = {12, 45, 23, 8, 14, 31, 6, 57, 34, 19};
do {
cout << "Menu:\n1. Linear Search\n2. Binary Search\n3. Bubble Sort\n4. Selection Sort\n5.
Insertion Sort\n6. Exit\nEnter your choice: ";
cin >> choice;
switch (choice) {
case 1: {
int key;
cout << "Enter the element to search: ";
cin >> key;
int result = linearSearch(arr, size, key);
if (result != -1) {
cout << "Element found at index " << result << endl;
} else {
cout << "Element not found in the array." << endl;
}
break;
}
case 2: {
int key;
cout << "Enter the element to search: ";
cin >> key;
bubbleSort(arr, size);
int result = binarySearch(arr, size, key);
if (result != -1) {
cout << "Element found at index " << result << endl;
} else {
cout << "Element not found in the array." << endl;
}
break;
}
case 3:
bubbleSort(arr, size);
cout << "Array sorted using Bubble Sort." << endl;
break;
case 4:
selectionSort(arr, size);
cout << "Array sorted using Selection Sort." << endl;
break;
case 5:
insertionSort(arr, size);
cout << "Array sorted using Insertion Sort." << endl;
break;
case 6:
cout << "Exiting the program." << endl;
break;
default:
cout << "Invalid choice. Please choose a valid option." << endl;
}
return 0;
}
Output:
Lab Report 4:
Stack:
#include <iostream>
#include <stack>
using namespace std;
class StackProgram {
private:
stack<int> myStack;
public:
void pushElement(int value) {
myStack.push(value);
cout << "Pushed " << value << " onto the stack." << endl;
}
void popElement() {
if (!myStack.empty()) {
cout << "Popped " << myStack.top() << " from the stack." << endl;
myStack.pop();
} else {
cout << "Stack is empty. Cannot pop." << endl;
}
}
void displayStack() {
if (!myStack.empty()) {
stack<int> tempStack = myStack;
cout << "Stack contents from top to bottom: ";
while (!tempStack.empty()) {
cout << tempStack.top() << " ";
tempStack.pop();
}
cout << endl;
} else {
cout << "Stack is empty." << endl;
}
}
};
int main() {
StackProgram stackObj;
int choice, value;
while (true) {
cout << "\nStack Menu:\n";
cout << "1. Push an element\n";
cout << "2. Pop an element\n";
cout << "3. Display the stack\n";
cout << "4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter an element to push: ";
cin >> value;
stackObj.pushElement(value);
break;
case 2:
stackObj.popElement();
break;
case 3:
stackObj.displayStack();
break;
case 4:
cout << "Exiting the program." << endl;
return 0;
default:
cout << "Invalid choice. Please enter a valid option." << endl;
}
}
return 0;
}
Ouput:
Code Overview:
Your C++ code implements a basic stack data structure using the standard library's stack container.
Below is a concise breakdown of its components:
1. Class Definition:
The StackProgram class encapsulates stack operations.
It contains a private member myStack of type stack<int> for storing integer elements.
2. Public Member Functions:
pushElement(int value): Pushes the given value onto the stack and prints a confirmation
message.
popElement(): Pops the top element from the stack if it's not empty and prints its value. If the
stack is empty, it prints a message indicating so.
displayStack(): Displays the contents of the stack from top to bottom. If the stack is empty, it
prints a message indicating so.
3. Main Function:
The main function handles user interaction through a menu-driven interface.
It repeatedly prompts the user for input until the exit option is chosen.
4. Input Validation:
Input validation is limited; the code does not handle non-integer inputs gracefully.
5. Style and Conventions:
Class names conventionally start with an uppercase letter (StackProgram), which is followed.
CamelCase is used for function names, ensuring consistency.
6. Comments:
The code lacks comments for clarification, which could improve readability and maintainability.
Conclusion: Your code provides a functional implementation of a stack with essential functionalities.
However, improvements in input validation, code formatting, and commenting could enhance its
readability and robustness.
Lab report 5:
#include <iostream>
using namespace std;
class LinearQueue {
private:
int arr[MAX_SIZE];
int front, rear;
public:
LinearQueue() : front(-1), rear(-1) {}
bool isEmpty() {
return (front == -1 && rear == -1);
}
bool isFull() {
return (rear == MAX_SIZE - 1);
}
if (isEmpty()) {
front = 0;
}
arr[++rear] = data;
cout << "Enqueued " << data << " into the queue." << endl;
}
void dequeue() {
if (isEmpty()) {
cout << "Queue is empty. Unable to dequeue." << endl;
return;
}
if (front == rear) {
front = rear = -1;
} else {
front++;
}
}
void displayFront() {
if (isEmpty()) {
cout << "Queue is empty. No front element." << endl;
} else {
cout << "Front element: " << arr[front] << endl;
}
}
void displayQueue() {
if (isEmpty()) {
cout << "Queue is empty." << endl;
} else {
cout << "Queue elements: ";
for (int i = front; i <= rear; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
}
};
int main() {
LinearQueue queue;
int choice, data;
do {
cout << "\nQueue Menu:\n";
cout << "1. Enqueue\n";
cout << "2. Dequeue\n";
cout << "3. Display Front\n";
cout << "4. Display Queue\n";
cout << "5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter data to enqueue: ";
cin >> data;
queue.enqueue(data);
break;
case 2:
queue.dequeue();
break;
case 3:
queue.displayFront();
break;
case 4:
queue.displayQueue();
break;
case 5:
cout << "Exiting program. Goodbye!\n";
break;
default:
cout << "Invalid choice. Please try again.\n";
}
} while (choice != 5);
return 0;
}
Output:
Lab report 6:
#include <iostream>
using namespace std;
class CircularQueue {
private:
int arr[MAX_SIZE];
int front, rear;
public:
CircularQueue() : front(-1), rear(-1) {}
bool isEmpty() {
return (front == -1 && rear == -1);
}
bool isFull() {
return (front == (rear + 1) % MAX_SIZE);
}
if (isEmpty()) {
front = 0;
rear = 0;
} else {
rear = (rear + 1) % MAX_SIZE;
}
arr[rear] = data;
cout << "Enqueued " << data << " into the queue." << endl;
}
void dequeue() {
if (isEmpty()) {
cout << "Queue is empty. Unable to dequeue." << endl;
return;
}
void displayFront() {
if (isEmpty()) {
cout << "Queue is empty. No front element." << endl;
} else {
cout << "Front element: " << arr[front] << endl;
}
}
void displayQueue() {
if (isEmpty()) {
cout << "Queue is empty." << endl;
} else {
cout << "Queue elements: ";
int i = front;
while (true) {
cout << arr[i] << " ";
if (i == rear) break;
i = (i + 1) % MAX_SIZE;
}
cout << endl;
}
}
};
int main() {
CircularQueue queue;
int choice, data;
do {
cout << "\nCircular Queue Menu:\n";
cout << "1. Enqueue\n";
cout << "2. Dequeue\n";
cout << "3. Display Front\n";
cout << "4. Display Queue\n";
cout << "5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter data to enqueue: ";
cin >> data;
queue.enqueue(data);
break;
case 2:
queue.dequeue();
break;
case 3:
queue.displayFront();
break;
case 4:
queue.displayQueue();
break;
case 5:
cout << "Exiting program. Goodbye!\n";
break;
default:
cout << "Invalid choice. Please try again.\n";
}
} while (choice != 5);
return 0;
}
Output:
Objectives
To learn about linked list operations including insertion from beginning , middle, end and
display.
Linked List
• Linked List is a linear data structure which is made up of nodes connected by pointers.
#include <iostream>
Struct node
Int info;
Node *link;
};
Node*head= NULL;
Int count = 0;
newNode->info= data;
newNode->link = head;
head= newNode;
count++;
newNode->info = data;
newNode->link = NULL;
if (head == NULL)
Head = newNode;
Else
Node*temp=head;
Int c= 1;
{temp =temp->link;
C++;
newNode->link =temp->link;
temp->link =newNode;}
count++;
newNode->info= data;
newNode->link = NULL;
if (head == NULL)
{head = newNode;
Else
Node*temp=head;
While(temp->link!=NULL)
Temp=temp->link;
Temp->link=newNode;
Count++;}
Void display()
While (current!=NULL)
{
Cout<<current->info<<”\t”;
Current=current ->link;
Cout<<endl;
Int main()
insertBeg(2);
insertend (21);
insertmiddle(3);
display();
return 0;}
Out Put
Lab 8: Deletion from Linked List
Objectives
To learn about linked list operations including deletion from beginning, middle, end and
display.
Linked List
• Linked List is a linear data structure which is made up of nodes connected by pointers.
Lab Task
#include<iostream>
Struct node
Int info;
Node* link;};
Int count = 0;
newNode->info = data;
newNode->link = head;
head= newNode;
count++;
Void deleteBeg() {
If(head == NULL)
Cout<<”Empty List”;
Else{
Delete ptr;
Count--;}}
Void deleteMid(){
Int c = 0;
If(head == NULL)
Cout<<”Empty List”;
Else
Ptr = head;
Head = NULL;
Delete ptr;
Count--;
}
Else {
Ptr = head;
While (c != mid){
C++;
Prev = ptr;
Delete ptr;
Count--;}}
Void deleteEnd(){
If(head == NULL)
Cout<<”Empty List”;
Else
Ptr = head;
Head = NULL;
Delete ptr;
Count--;
}else{
Ptr = head;
Prev = ptr;
}
Prev -> link= NULL;
Delete ptr;
Count--;}
Void display()
While(current!=NULL)
Cout<<current->info<<”\t”;
Current=current->link;
Cout<<endl;
}
Int main()
insertBeg(92);
insertBeg(29);
insertBeg (12);
insertBeg(34);
insertBeg(21);
deleteBeg();
deleteMid();
deleteEnd();
display();
return 0;
}
Out Put
Lab 9: Linked List based Stack and Queue
Objectives
• Stack -> Pop -> Delete from beginning in Singly Linked List
• Queue -> Dequeue -> Delete from Beginning in Singly Linked List
• Check Empty list using the condition head == Null before deletion from stack and queue.
Lab Task
#include <iostream>
Struct node
Int info;
Node*link;
};
Int count = 0;
newNode->info = data;
newNode->link = head;
head = newNode;
count++;}
void pop() {
if(head == NULL)
cout<<”Empty List”;
else{
delete ptr;
count--;}
Void display()
While(current !=NULL)
{ cout<<current->info<<”\t”;
Current=current->link;
Cout<<endl;
Int main(){
Push(92);
Push(29);
Pop();
Push(34);
Display();
Return 0;}
Out Put
Lab Task
#include <iostream>
Struct node{
Int info;
Node* link;};
Int count = 0;
newNode->info = data;
newNode->link = NULL;
if (head == NULL)
{head= newNode;}
Else
Temp=temp->link;
Temp->link = newNode;}
Count++;
Void Dequeue() {
If(head == NULL)
Cout<<”Empty List”;
Else{
Delete ptr;
Count--;}
Void display()
While(current!=NULL)
Cout<<current->info<<”\t”;
Current=current->link;
Cout<<endl;
Int main(){
Enqueue (92);
Enqueue (29);
Dequeue();
Enqueue (34);
Display();
Return 0;}
Out Put
Objectives
• One pointer points to the previous node while the second pointer points to the next node.
• The previous pointer of the head is set to NULL as this is the first node.
• The next pointer of the tail node is set to NULL as this is the last node.
• As the doubly linked list contains two pointers i.e. previous and next, we can traverse it into
the directions forward and backward
Lab Task
#include <iostream>
Struct node
Int info;
Node* prev;
Node* next;
};
Node* head = NULL;
newNode->info= data;
newNode->next = head;
newNode->prev = NULL;
if (head != NULL)
head->prev= newNode;
head= newNode;
Void delete_node()
Head = head->next;
Delete temp;
Head->prev = NULL;
Void display()
While(current!=NULL)
Cout<<current->info<<”\t”;
Current=current->next;
Cout<<endl;
Int main(){
Insert(2);
Insert(6);
Insert(10);
Display();
Delete_node();
Display();
Return 0;
Out Put
• One pointer points to the previous node while the second pointer points to the next node.
Lab Task
#include <iostream>
Struct node
Int info;
Node* prev;
Node* next;
};
newNode->info = data;
newNode->next = head;
newNode->prev = NULL;
if (head != NULL)
head->prev = newNode;
head= newNode;
Void reverseDisplay()
Return;
While(temp->next!=NULL)
Temp=temp->next;
} while(temp!=NULL)
{ cout<<temp->info<<”\t”;
Temp=temp->prev;
Cout<<endl;
Int main()
Insert(2);
Insert(6);
Insert(10);
reverseDisplay();
return 0;
Out Put
LAB 12: Sorting using Divide and Conquer Approach
Objectives
To learn about divide and conquer algorithms implementation (Merge Sort, Quick Sort)
Merge Sort
The Merge Sort algorithm is a sorting algorithm that is considered as an example of the
divide and conquer strategy. So, in this algorithm, the array is initially divided into two equal
halves and then they are combined in a sorted manner. We can think of it as a recursive
algorithm that continuously splits the array in half until it cannot be further divided. This
means that if the array becomes empty or has only one element left, the dividing will stop, i.e.
it is the base case to stop the recursion. If the array has multiple elements, we split the array
into halves and recursively invoke the merge sort on each of the halves. Finally, when both
the halves are sorted, the merge operation is applied. Merge operation is the process of taking
two smaller sorted arrays and combining them to eventually make a larger one.
General Algorithm
Quick Sort
Quick Sort is a Divide and Conquer algorithm. It picks an element as pivot and partitions the
given array around the picked pivot. There are many different versions of quick Sort that pick
pivot in different ways.
General Algorithm
a. Partition the list into two sub lists, say lower Sub list and upper Sub list.
d. Combine the sorted lower Sub list and sorted upper Sub list.