#include <iostream> using namespace std; int main() { int *A,n,x, ch; int i,j; cout << "Enter dimension : " ; cin >> n; A = new int[n]; do { cout << "\n\n---Diagonal matrix---" << endl; cout << "1. Create " << endl; cout << "2. Get " << endl; cout << "3. Set " << endl; cout << "4. Display " << endl; cout << "5. Exit " << endl; cout << "Enter your choice : "; cin >> ch; switch(ch) { case 1 : cout << "Enter the diagonal elements : \n"; for (int i=1; i<=n; i++) cin >> A[i-1]; break; case 2 : cout << "Enter indices : "; cin >> i >> j; if (i==j) cout << "Element is : " << A[i-1] << endl; else cout << "Element is : 0 " << endl; break; case 3 : cout << "Enter the element : "; cin >> x; cout << "Enter the indices : "; cin >> i >> j; if (i==j) { A[i-1] = x; cout << "Element is inserted "<< endl; } else cout << "Element cannot be inserted at those indices!" << endl; break; case 4 : for (int i=1; i<=n; i++) { for(int j=1; j<=n; j++) { if (i==j) cout << A[i-1] << " "; else cout << "0 "; } cout << endl; } break; } } while(ch != 5); 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