#include <iostream> using namespace std; class Diagonal { private : int n; int *A; public : Diagonal(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(); ~Diagonal() { delete []A; } }; void Diagonal :: set(int i, int j, int x) { if(i==j) A[i-1] = x; } int Diagonal :: get(int i ,int j) { if (i==j) return A[i-1]; else return 0; } void Diagonal :: 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; } } int main() { Diagonal m(4) ; m.set(1,1,1); m.set(2,2,2); m.set(3,3,3); m.set(4,4,4); m.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