#include <iostream> using namespace std; class LowerTri { private : int n; int *A; public : LowerTri(int n) { this->n = n; A = new int[(n*(n+1))/2]; } void set (int i, int j , int x); int get (int i, int j); void display(); LowerTri() { delete []A; } }; void LowerTri :: set(int i, int j, int x) { if(i>=j) A[(i*(i-1)/2) + (j-1)] = x; } int LowerTri :: get(int i ,int j) { if(i>=j) return A[(i*(i-1)/2) + (j-1)]; else return 0; } void LowerTri :: display() { // cout << "\n\n"; 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 << "0 "; } cout << endl; } } int main() { int d ; cout << "Enter dimensions : " ; cin >> d; LowerTri lm(d); int x; cout << "Enter all elements : " << endl; for(int i=1 ; i<=d; i++) { for (int j=1; j<=d; j++) { cin >> x; lm.set(i,j,x); } } cout << "\n\n"; lm.display(); return 0; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter