diff --git a/DSA/Matrix/DiagonalMatrix.cpp b/DSA/Matrix/DiagonalMatrix.cpp new file mode 100644 index 00000000..20f43961 --- /dev/null +++ b/DSA/Matrix/DiagonalMatrix.cpp @@ -0,0 +1,83 @@ +// C++ pgm to print diagonal matrix. +#include +using namespace std; + +class DiagonalMatrix +{ +private: + int *A, n; + +public: + DiagonalMatrix(int n) + { + this->n = n; + A = new int(n); + }; + void set(int i, int j, int x); + int get(int i, int j); + void display(); + + ~DiagonalMatrix(); +}; + +// Here, i & j denotes the position where element should get inserted! +void DiagonalMatrix::set(int i, int j, int x) +{ + // condition to fill elements in diagonal matrix. + if (i == j) + { + // inserting at proper index. + A[j - 1] = x; + } +} + +// function used to fetch elements at specific index. +int DiagonalMatrix::get(int i, int j) +{ + if (i == j) + { + return A[j - 1]; + } + return 0; +} + +// displays diagonal matrix. +void DiagonalMatrix::display() +{ + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + if (i == j) + { + cout << A[i] << " "; + } + else + { + cout << "0 "; + } + } + cout << endl; + } +} + +// destructor +DiagonalMatrix::~DiagonalMatrix() +{ + delete[] A; +} + +int main() +{ + // pass dimansions + DiagonalMatrix DM(4); + // pass the values according to dimensions. + DM.set(1, 1, 5); + DM.set(2, 2, 6); + DM.set(3, 3, 7); + DM.set(4, 4, 8); + DM.display(); + //cout< +using namespace std; + +class LTMatrix +{ +private: + int n; + int *A; + +public: + LTMatrix(int n) + { + this->n = n; + A = new int[n * (n + 1) / 2]; + } + ~LTMatrix() { delete[] A; } + void Display(bool row = true); + void setRowMajor(int i, int j, int x); + void setColMajor(int i, int j, int x); + int getRowMajor(int i, int j); + int getColMajor(int i, int j); + int getN() { return n; } +}; + +// set the elements in proper posn for matrix for row major approach! +void LTMatrix::setRowMajor(int i, int j, int x) +{ + if (i >= j) + { + int index = ((i * (i - 1)) / 2) + j - 1; + A[index] = x; + } +} + +// set the elements in proper posn for matrix for column major approach! +void LTMatrix::setColMajor(int i, int j, int x) +{ + if (i >= j) + { + int index = (n * (j - 1) - (((j - 2) * (j - 1)) / 2)) + (i - j); + A[index] = x; + } +} + +// get the elements from matrix for row major approach! +int LTMatrix::getRowMajor(int i, int j) +{ + if (i >= j) + { + int index = ((i * (i - 1)) / 2) + j - 1; + return A[index]; + } + else + { + return 0; + } +} + +// get the elements from matrix for column major approach! +int LTMatrix::getColMajor(int i, int j) +{ + if (i >= j) + { + int index = (n * (j - 1) - (((j - 2) * (j - 1)) / 2)) + (i - j); + return A[index]; + } + else + { + return 0; + } +} + +// displays the lower tri. matrix. +void LTMatrix::Display(bool row) +{ + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= n; j++) + { + if (i >= j) + { + if (row) + { + cout << getRowMajor(i, j) << " "; + } + else + { + cout << getColMajor(i, j) << " "; + } + } + else + { + cout << 0 << " "; + } + } + cout << endl; + } +} + +int main() +{ + + // can edit these values acc. to dimensions! + cout << "Matrix by row major approach: " << endl; + LTMatrix rm(4); + rm.setRowMajor(1, 1, 1); + rm.setRowMajor(2, 1, 2); + rm.setRowMajor(2, 2, 3); + rm.setRowMajor(3, 1, 4); + rm.setRowMajor(3, 2, 5); + rm.setRowMajor(3, 3, 6); + rm.setRowMajor(4, 1, 7); + rm.setRowMajor(4, 2, 8); + rm.setRowMajor(4, 3, 9); + rm.setRowMajor(4, 4, 10); + + rm.Display(); + cout << endl; + + cout << "Matrix by column major approach: " << endl; + LTMatrix cm(4); + cm.setColMajor(1, 1, 1); + cm.setColMajor(2, 1, 2); + cm.setColMajor(2, 2, 3); + cm.setColMajor(3, 1, 4); + cm.setColMajor(3, 2, 5); + cm.setColMajor(3, 3, 6); + cm.setColMajor(4, 1, 7); + cm.setColMajor(4, 2, 8); + cm.setColMajor(4, 3, 9); + cm.setColMajor(4, 4, 10); + + cm.Display(false); + + return 0; +} \ No newline at end of file diff --git a/DSA/Matrix/SymmetricMatrix.cpp b/DSA/Matrix/SymmetricMatrix.cpp new file mode 100644 index 00000000..59708b12 --- /dev/null +++ b/DSA/Matrix/SymmetricMatrix.cpp @@ -0,0 +1,109 @@ +// C++ pgm displays symmetric matrix given any elements in an array. +#include +using namespace std; + +class Symmetric +{ +private: + int *A, n, x; + +public: + Symmetric(int n); + void create(int n); + void set(int i, int j, int x); + int get(int i, int j); + void display(); + ~Symmetric(); +}; + +// creates the mat. acc. to dimensions. +Symmetric::Symmetric(int n) +{ + this->n = n; + A = new int[n * (n + 1) / 2]; +} + +// get the elements by user. +void Symmetric::create(int n) +{ + this->n = n; + cout << "Enter the elements for " << n << "x" << n << " matrix: " << endl; + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= n; j++) + { + cin >> x; + set(i, j, x); + } + } +} + +// set the elements in matrix at proper index. +void Symmetric::set(int i, int j, int x) +{ + if (i >= j) + { + // row major mapping + A[i * (i - 1) / 2 + j - 1] = x; + } + else + // column major mapping + A[j * (j - 1) / 2 + i - 1] = x; +} + +int Symmetric::get(int i, int j) +{ + if (i >= j) + { + // row major mapping + return A[i * (i - 1) / 2 + j - 1]; + } + else + { + // column major mapping + return A[j * (j - 1) / 2 + i - 1]; + } +} + +// display the matrix elements in proper manner. +void Symmetric::display() +{ + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= n; j++) + { + if (i >= j) + { + cout << A[(i * (i - 1)) / 2 + (j - 1)] << " "; + } + else + { + cout << A[j * (j - 1) / 2 + i - 1] << " "; + } + } + cout << endl; + } +} + +// destructor +Symmetric::~Symmetric() +{ + delete[] A; +} + +int main() +{ + int n; + cout << "Enter the dimension for matrix: "; + cin >> n; + + Symmetric S1(n); + S1.create(n); + S1.display(); + // get the elements at specific index function; + // for 3x3 matrix below, for others u can edit! + cout << "The element at row 3 & column 2 is: " << S1.get(3, 2) << endl; + cout << "The element at row 2 & column 1 is: " << S1.get(2, 1) << endl; + + return 0; +} \ No newline at end of file