Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

dsa - Copy

The document contains multiple C++ code snippets demonstrating various programming concepts. It includes matrix operations, stack implementation, recursive algorithms for factorial and Fibonacci, infix to postfix conversion, and postfix expression evaluation. Each section showcases different functionalities and user interactions through console input and output.

Uploaded by

bhanutej2004
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

dsa - Copy

The document contains multiple C++ code snippets demonstrating various programming concepts. It includes matrix operations, stack implementation, recursive algorithms for factorial and Fibonacci, infix to postfix conversion, and postfix expression evaluation. Each section showcases different functionalities and user interactions through console input and output.

Uploaded by

bhanutej2004
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

1.

#include <iostream>
using namespace std;

void readMatrix(int matrix[10][10], int rows, int cols) {


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cin >> matrix[i][j];
}
}
}

void displayMatrix(int matrix[10][10], int rows, int cols) {


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
}

void addMatrices(int matrix1[10][10], int matrix2[10][10], int result[10][10], int


rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
}

void subtractMatrices(int matrix1[10][10], int matrix2[10][10], int result[10][10],


int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[i][j] = matrix1[i][j] - matrix2[i][j];
}
}
}

void multiplyMatrices(int matrix1[10][10], int matrix2[10][10], int result[10][10],


int rows1, int cols1, int cols2) {
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
result[i][j] = 0;
for (int k = 0; k < cols1; k++) {
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
}

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 binarySearch(int arr[], int left, int right, int x) {


if (right >= left) {
int mid = left + (right - left) / 2;
if (arr[mid] == x) return mid;
if (arr[mid] > x) return binarySearch(arr, left, mid - 1, x);
return binarySearch(arr, mid + 1, right, x);
}
return -1;
}

int fibonacci(int n) {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}

void towersOfHanoi(int n, char source, char auxiliary, char destination) {


if (n == 1) {
cout << "Move disk 1 from " << source << " to " << destination << endl;
return;
}
towersOfHanoi(n - 1, source, destination, auxiliary);
cout << "Move disk " << n << " from " << source << " to " << destination <<
endl;
towersOfHanoi(n - 1, auxiliary, source, destination);
}

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;

int precedence(char op) {


if (op == '+' || op == '-') return 1;
if (op == '*' || op == '/') return 2;
if (op == '^') return 3;
return 0;
}

bool isOperand(char c) {
return ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <=
'9'));
}

void infixToPostfix(const char infix[], char postfix[]) {


char stack[100];
int top = -1;
int j = 0;

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


char c = infix[i];

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;
}
}

while (top >= 0) {


postfix[j++] = stack[top--];
}
postfix[j] = '\0';
}

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;

int evaluatePostfix(char expression[], int length) {


int stack[100];
int top = -1;

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


char ch = expression[i];

if (ch >= '0' && ch <= '9') {


stack[++top] = ch - '0';
} else {
if (top < 1) {
cout << "Error: Not enough operands for operation!" << endl;
return 0;
}

int operand2 = stack[top--];


int operand1 = stack[top--];
int result = 0;

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;
}
---------------

You might also like