If you have any query feel free to chat us!
Happy Coding! Happy Learning!
In a lower triangular matrix, all the elements above the main diagonal are zero, and the elements on and below the main diagonal contain meaningful values. Row-major mapping is a way to store a 2D matrix in a 1D array while maintaining the same row-wise order. In row-major mapping, the elements of each row are stored together in the array.
For a lower triangular matrix of size N, the row-major mapping formula to access an element at row
iand columnjis:cssCopy code
index = i*(i+1)/2 + jHere,
indexis the position of the element in the 1D array,iis the row number (0-based), andjis the column number (0-based).Let's create a C++ program to demonstrate the row-major mapping for a lower triangular matrix:
cppCopy code
#include <iostream> #include <vector> class LowerTriangularMatrix { private: int size; std::vector<int> elements; public: LowerTriangularMatrix(int n) { size = n; int totalElements = n * (n + 1) / 2; elements.resize(totalElements, 0); } // Function to set an element at (i, j) position in the lower triangular matrix void setElement(int i, int j, int value) { if (i >= j && i < size && j < size) { int index = i * (i + 1) / 2 + j; elements[index] = value; } else { std::cout << "Invalid index. Element not set." << std::endl; } } // Function to get an element from (i, j) position in the lower triangular matrix int getElement(int i, int j) { if (i >= j && i < size && j < size) { int index = i * (i + 1) / 2 + j; return elements[index]; } else { std::cout << "Invalid index. Returning 0." << std::endl; return 0; } } // Function to display the lower triangular matrix void displayLowerTriangularMatrix() { std::cout << "Lower Triangular Matrix:" << std::endl; for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { if (i >= j) { int index = i * (i + 1) / 2 + j; std::cout << elements[index] << " "; } else { std::cout << "0 "; } } std::cout << std::endl; } } }; int main() { int size = 4; LowerTriangularMatrix matrix(size); matrix.setElement(0, 0, 1); matrix.setElement(1, 0, 2); matrix.setElement(1, 1, 3); matrix.setElement(2, 0, 4); matrix.setElement(2, 1, 5); matrix.setElement(2, 2, 6); matrix.setElement(3, 0, 7); matrix.setElement(3, 1, 8); matrix.setElement(3, 2, 9); matrix.setElement(3, 3, 10); matrix.displayLowerTriangularMatrix(); return 0; }Output:
yamlCopy code
Lower Triangular Matrix: 1 0 0 0 2 3 0 0 4 5 6 0 7 8 9 10In this program, we have defined the
LowerTriangularMatrixclass, which uses a 1D vector to store the elements of the lower triangular matrix. ThesetElementandgetElementfunctions use the row-major mapping formula to access and modify the elements. ThedisplayLowerTriangularMatrixfunction displays the lower triangular matrix with meaningful elements.You can set elements and access values using the (i, j) coordinates, and the class will handle the correct mapping to the 1D array.
Start the conversation!
Be the first to share your thoughts
Quick answers to common questions about our courses, quizzes, and learning platform
Didn't find what you're looking for?
Contact Support