C++ program to show sum of main diagonal elements
C++ program to show sum of main diagonal elements by using nested for loop.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include<iostream> using namespace std; int main() { int size=3; int a[size][size]; int row,col,sum=0; cout<<"enter elements in matrix of size:"<<size<<"*"<<size<<endl; for(row=0;row<size;row++) { for(col
=0;col<size;col++) { cin>>a[row][col]; } } for(row=0;row<size;row++) { sum=sum+a[row][row]; } cout<<"sum of main diagonal elements:"<<sum; } |
Let’s see the declaration flow table of the given program.
Variables and arrays | Declaration | Initialization | Use line number |
a[size][size] | 6 | 13 | 18 |
row | 7 | 9A|9C|16A|16C | 9B|16B |
col | 7 | 11A|11C | 11B |
sum | 7 | 7|18 | 20 |
size | 5 | 5 | 8|9B|11B|16B |
Output