dsa - Copy
dsa - Copy
#include <iostream>
using namespace std;
int main() {
int m, n, p, choice;
int matrix1[10][10], matrix2[10][10], result[10][10];
cout << "Enter the number of rows and columns for the matrices: ";
cin >> m >> n;
while (true) {
cout << "\nMenu:\n";
cout << "1. Read matrices\n";
cout << "2. Display matrices\n";
cout << "3. Add matrices\n";
cout << "4. Subtract matrices\n";
cout << "5. Multiply matrices\n";
cout << "6. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Reading first matrix:\n";
readMatrix(matrix1, m, n);
cout << "Reading second matrix:\n";
readMatrix(matrix2, m, n);
break;
case 2:
cout << "First Matrix:\n";
displayMatrix(matrix1, m, n);
cout << "Second Matrix:\n";
displayMatrix(matrix2, m, n);
break;
case 3:
addMatrices(matrix1, matrix2, result, m, n);
cout << "Sum of matrices:\n";
displayMatrix(result, m, n);
break;
case 4:
subtractMatrices(matrix1, matrix2, result, m, n);
cout << "Difference of matrices:\n";
displayMatrix(result, m, n);
break;
case 5:
cout << "Enter the number of columns for the second matrix: ";
cin >> p;
readMatrix(matrix2, n, p);
multiplyMatrices(matrix1, matrix2, result, m, n, p);
cout << "Product of matrices:\n";
displayMatrix(result, m, p);
break;
case 6:
return 0;
default:
cout << "Invalid choice! Please try again.\n";
}
}
return 0;
}
---------------------------------------
2.
#include <iostream>
using namespace std;
class Stack {
int top;
int capacity;
int array[100];
public:
Stack(int size) {
capacity = size;
top = -1;
}
bool isFull() const {
return top == capacity - 1;
}
bool isEmpty() const {
return top == -1;
}
void push(int x) {
if (isFull()) {
cout << "Stack Overflow! Cannot push " << x << endl;
return;
}
array[++top] = x;
cout << x << " been pushed into stack." << endl;
}
int pop() {
if (isEmpty()) {
cout << "Stack Underflow!" << endl;
return -1;
}
int x = array[top--];
cout << x << " popped from the stack." << endl;
return x;
}
int peek() const {
if (isEmpty()) {
cout << "Stack empty!." << endl;
return -1;
}
return array[top];
}
int size() const {
return top + 1;
}
void display() const {
if (isEmpty()) {
cout << "Stack empty!" << endl;
return;
}
cout << "Stack elements: ";
for (int i = 0; i <= top; i++) {
cout << array[i] << " ";
}
cout << endl;
}
};
int main() {
Stack s(3);
s.push(10);
s.push(20);
s.push(30);
s.display();
cout << "Current top element: " << s.peek() << endl;
s.pop();
s.display();
s.push(40);
s.push(50);
cout << "Top element: " << s.peek() << endl;
cout << "Current stack size: " << s.size() << endl;
return 0;
}
------------------------------
3.
#include <iostream>
using namespace std;
int factorial(int n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}
int fibonacci(int n) {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
int factNumber = 5;
cout << "Factorial of " << factNumber << " is " << factorial(factNumber) <<
endl;
int arr[] = {1, 3, 5, 7, 9};
int size = sizeof(arr) / sizeof(arr[0]);
int target = 5;
int result = binarySearch(arr, 0, size - 1, target);
if (result != -1)
cout << "Element " << target << " found at index " << result << endl;
else
cout << "Element " << target << " not found in the array." << endl;
int fibPosition = 7;
cout << "Fibonacci number at position " << fibPosition << " is " <<
fibonacci(fibPosition) << endl;
int disks = 3;
cout << "Towers of Hanoi with " << disks << " disks:" << endl;
towersOfHanoi(disks, 'A', 'B', 'C');
return 0;
}
-------------------------------
4.
#include <iostream>
using namespace std;
bool isOperand(char c) {
return ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <=
'9'));
}
if (isOperand(c)) {
postfix[j++] = c;
} else if (c == '(') {
stack[++top] = c;
} else if (c == ')') {
while (top >= 0 && stack[top] != '(') {
postfix[j++] = stack[top--];
}
if (top >= 0 && stack[top] == '(') {
top--;
}
} else if (c == '+' || c == '-' || c == '*' || c == '/' || c == '^') {
while (top >= 0 && precedence(stack[top]) >= precedence(c)) {
postfix[j++] = stack[top--];
}
stack[++top] = c;
}
}
int main() {
char infix[100], postfix[100];
cout << "Enter an infix expression: ";
cin >> infix;
infixToPostfix(infix, postfix);
cout << "Postfix expression: " << postfix << endl;
return 0;
}
------------------------------
5.
#include <iostream>
using namespace std;
switch (ch) {
case '+': result = operand1 + operand2; break;
case '-': result = operand1 - operand2; break;
case '*': result = operand1 * operand2; break;
case '/':
if (operand2 == 0) {
cout << "Error: Division by zero!" << endl;
return 0;
}
result = operand1 / operand2;
break;
default:
cout << "Error: Invalid operator '" << ch << "'!" << endl;
return 0;
}
stack[++top] = result;
}
}
if (top != 0) {
cout << "Error: Too many operands!" << endl;
return 0;
}
return stack[top];
}
int main() {
char postfixExpression[] = {'5', '3', '+', '8', '2', '-', '*', '\0'};
int length = sizeof(postfixExpression) / sizeof(postfixExpression[0]) - 1;
int result = evaluatePostfix(postfixExpression, length);
cout << "Result of the postfix expression: " << result << endl;
return 0;
}
---------------