In: Computer Science
Write a program that read an array consists of 4 rows and 4 columns, then computes and print the sum of the elements above and below the main diagonal. (WITH C++)
Here is the program
#include<iostream>
using namespace std;
int main() {
   int arr[4][4];
   int ad=0,bd=0,i,j,n;
   n=4;
   cout<<"Enter the matrix:\n";
  
   for(i=0;i<n;++i) {
   cout<<"Enter " << n << " elements of
Row No: "<< (i+1) << endl;
       for(j=0;j<n;++j) {
          
cin>>arr[i][j];
       }
   }
   cout << "The entered matrix is
"<<endl;
   for(i=0;i<n;++i) {
   cout << endl;
       for(j=0;j<n;++j) {
          
cout<<arr[i][j]<< "\t";
       }
   }
  
   // sum of the elements above and below the main
diagonal.
   for(i=0;i<n;++i)
       for(j=0;j<n;++j)
           if(j>i) //
above diagonal
          
    ad+=arr[i][j];
           else
          
    if(i>j) // below diagonal
          
        bd+=arr[i][j];
  
   cout<<"\nSum of elements above the main
diagonal:"<<ad;
   cout<<"\nSum of elements below the main
diagonal:"<<bd;
   return 0;
}
Sample output:
