diff --git a/Matrix Prod.cpp b/Matrix Prod.cpp new file mode 100644 index 00000000..8b67068f --- /dev/null +++ b/Matrix Prod.cpp @@ -0,0 +1,54 @@ +#include +using namespace std; + +int main() { + int row1, col1, row2, col2; + + cout << "Enter the number of rows and columns of the first matrix: "; + cin >> row1 >> col1; + + cout << "Enter the number of rows and columns of the second matrix: "; + cin >> row2 >> col2; + + + if (col1 != row2) { + cout << "Matrix multiplication is not possible. Columns of the first matrix must be equal to rows of the second matrix." << endl; + return 1; + } + + cout << "Enter elements of the first matrix:" << endl; + int matrix1[row1][col1]; + for (int i = 0; i < row1; ++i) { + for (int j = 0; j < col1; ++j) { + cin >> matrix1[i][j]; + } + } + + cout << "Enter elements of the second matrix:" << endl; + int matrix2[row2][col2]; + for (int i = 0; i < row2; ++i) { + for (int j = 0; j < col2; ++j) { + cin >> matrix2[i][j]; + } + } + + int result[row1][col2] = {{0}}; + + for (int i = 0; i < row1; ++i) { + for (int j = 0; j < col2; ++j) { + for (int k = 0; k < col1; ++k) { + result[i][j] += matrix1[i][k] * matrix2[k][j]; + } + } + } + + cout << "Result of matrix multiplication:" << endl; + for (int i = 0; i < row1; ++i) { + for (int j = 0; j < col2; ++j) { + cout << result[i][j] << " "; + } + cout << endl; + } + + return 0; +} diff --git a/Maximum Rod Cutting.cpp b/Maximum Rod Cutting.cpp new file mode 100644 index 00000000..574c6250 --- /dev/null +++ b/Maximum Rod Cutting.cpp @@ -0,0 +1,26 @@ + +#include +using namespace std; + + +int max(int a, int b) { return (a > b)? a : b;} +int max(int a, int b, int c) { return max(a, max(b, c));} + + +int maxProd(int n) +{ + + if (n == 0 || n == 1) return 0; + + int max_val = 0; + for (int i = 1; i < n; i++) + max_val = max(max_val, i*(n-i), maxProd(n-i)*i); + + return max_val; +} + +int main() +{ + cout << "Maximum Product is " << maxProd(10); + return 0; +} diff --git a/Reverse string.cpp b/Reverse string.cpp new file mode 100644 index 00000000..47d0ed33 --- /dev/null +++ b/Reverse string.cpp @@ -0,0 +1,24 @@ +#include +#include +using namespace std; + +int main() { + string str; + + // Input a string + cout << "Enter a string: "; + cin >> str; + + // Initialize an empty string to store the reversed string + string reversedStr = ""; + + // Reverse the string + for (int i = str.length() - 1; i >= 0; i--) { + reversedStr += str[i]; + } + + // Display the reversed string + cout << "Reversed string: " << reversedStr << endl; + + return 0; +}