0% found this document useful (0 votes)
11 views12 pages

SP23-BSE-160

Uploaded by

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

SP23-BSE-160

Uploaded by

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

LAB ASSIGNMENT 1

SUBMITTED BY UMAMA ABID


REG# SP23-BSE-160
TASK 1

CODE

#include <iostream>

void intersection(int arr1[], int size1, int arr2[], int size2) {

for (int i = 0; i < size1; ++i) {

for (int j = 0; j < size2; ++j) {

if (arr1[i] == arr2[j]) {

std::cout << arr1[i] << " ";

arr2[j] = -1; // Mark the found element to avoid duplicates

break; // Exit inner loop after finding match

int main() {

int arr1[] = {4, 9, 5};

int arr2[] = {9, 4, 9, 8, 4};

std::cout << "Intersection: ";

intersection(arr1, 3, arr2, 5);

return 0;

OUTPUT
TASK 2

CODE

#include <iostream>

void reverse(int arr[], int start, int end) {

while (start < end) {

int temp = arr[start];

arr[start] = arr[end];

arr[end] = temp;

start++;

end--;

void rotate(int arr[], int n, int k) {

k %= n; // Handle cases where k >= n

reverse(arr, 0, n - 1); // Reverse the whole array

reverse(arr, 0, k - 1); // Reverse the first k elements

reverse(arr, k, n - 1); // Reverse the rest of the array

int main() {

int arr[] = {1, 2, 3, 4, 5, 6, 7};

int k = 3;

int n = sizeof(arr) / sizeof(arr[0]);

rotate(arr, n, k);

std::cout << "Rotated array: ";

for (int i = 0; i < n; i++) {

std::cout << arr[i] << " "


}

return 0;

OUTPUT

TASK 3

CODE

#include <iostream>

class DynamicArray {

private:

int* array;

int capacity;

int size;

public:

DynamicArray() {

capacity = 2; // Initial capacity

size = 0;

array = new int[capacity];

~DynamicArray() {

delete[] array; // Free allocated memory


}

void append(int value) {

if (size == capacity) {

capacity *= 2; // Double capacity if needed

int* newArray = new int[capacity];

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

newArray[i] = array[i];

delete[] array;

array = newArray;

array[size++] = value;

void deleteValue(int value) {

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

if (array[i] == value) {

for (int j = i; j < size - 1; j++) {

array[j] = array[j + 1]; // Shift elements left

size--; // Reduce size

break; // Exit after deleting first occurrence

}
void display() {

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

std::cout << array[i] << " ";

std::cout << std::endl;

};

int main() {

DynamicArray dynArray;

dynArray.append(1);

dynArray.append(2);

dynArray.append(3);

std::cout << "Dynamic Array: ";

dynArray.display(); // Output: 1 2 3

dynArray.deleteValue(2);

std::cout << "After Deletion: ";

dynArray.display(); // Output: 1 3

return 0;

OUTPUT
TASK 4

CODE

#include <iostream>

class Stack {

private:

int* stack;

int capacity;

int top;

public:

Stack() {

capacity = 10; // Initial capacity

stack = new int[capacity];

top = -1; // Indicates empty stack

~Stack() {

delete[] stack; // Free allocated memory

void push(int value) {

if (top == capacity - 1) {

// Expand capacity if needed

capacity *= 2;

int* newStack = new int[capacity];

for (int i = 0; i <= top; i++) {

newStack[i] = stack[i];

delete[] stack;

stack = newStack;
}

stack[++top] = value;

int pop() {

if (top >= 0) {

return stack[top--]; // Return the top element

return -1; // Indicate an empty stack

int secondToTop() {

if (top < 1) {

return -1; // Indicate there is no second-to-top

return stack[top - 1]; // Return the second-to-top element

};

int main() {

Stack stack;

stack.push(1);

stack.push(2);

stack.push(3);

std::cout << "Second-to-top element: " << stack.secondToTop() << std::endl; // Output: 2

stack.pop();

std::cout << "Second-to-top after pop: " << stack.secondToTop() << std::endl; // Output: 1

return 0;

}
OUTPUT

TASK 5

OUTPUT

#include <iostream>

// Function to determine the precedence of an operator

int precedence(char op) {

if (op == '+' || op == '-') return 1;

if (op == '*' || op == '/') return 2;

return 0;

int stringLength(char* exp) {

int length = 0;

while (exp[length] != '\0') {

length++;

return length;

// Custom function to reverse a string

void reverse(char* exp) {


int n = stringLength(exp); // Find length using custom function

for (int i = 0; i < n / 2; ++i) {

char temp = exp[i]; // Manual swap

exp[i] = exp[n - i - 1];

exp[n - i - 1] = temp;

// Custom function to check if a character is alphanumeric (a-z, A-Z, 0-9)

bool isAlnum(char ch) {

if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9')) {

return true;

return false;

// Main conversion function to convert infix to prefix

void infixToPrefix(char* infix, char* prefix) {

reverse(infix); // Step 1: Reverse the infix expression manually

char stack[100]; // Stack for operators

int top = -1;

int j = 0; // Index for the prefix array

// Step 2: Traverse the reversed infix expression

for (int i = 0; infix[i] != '\0'; ++i) {

if (isAlnum(infix[i])) {

prefix[j++] = infix[i]; // Append operand to prefix


} else if (infix[i] == ')') {

stack[++top] = infix[i]; // Push ')' onto the stack

} else if (infix[i] == '(') {

while (top != -1 && stack[top] != ')') {

prefix[j++] = stack[top--];

top--; // Remove ')' from the stack

} else { // Handling operators

while (top != -1 && precedence(stack[top]) > precedence(infix[i])) {

prefix[j++] = stack[top--];

stack[++top] = infix[i]; // Push the current operator

// Step 3: Pop remaining elements from the stack

while (top != -1) {

prefix[j++] = stack[top--];

prefix[j] = '\0'; // Null-terminate the prefix string

reverse(prefix); // Step 4: Reverse the result to get the final prefix

int main() {

char infix[] = "(A-B/C)*(A/K-L)";

char prefix[100];
infixToPrefix(infix, prefix);

std::cout << "Prefix Notation: " << prefix << std::endl;

return 0;

OUTPUT

You might also like