Write a program to find the sum of diagonal elements of a given matrix | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

Write a program to find the sum of diagonal elements of a given matrix

please answer fast

20th Nov 2016, 2:16 PM
Soutik
Soutik - avatar
5 Answers
+ 2
#include <iostream> using namespace std; /*program to sum elements lying on any of the diagonal of matrix*/ //sum will be including both diagonals int main() { int i,j,sum=0,ar[3][3]; for(i=0;i<3;i++) { for(j=0;j<3;j++) { cin>>ar[i] ; } } for(i=0;i<3;i++) { sum+=(ar[i][i]+ar[i][2-i]); } sum-=ar[1][1]; cout<<sum; return 0; }
29th Nov 2016, 11:12 AM
Rishabh Agrawal
Rishabh Agrawal - avatar
+ 1
it will ask for order of the square matrix then ask for the elements then display the entered matrix then show sum of diagonal elements I included the standard diagonal that's is a11. a22 a33 etc only #include <iostream.h> void main(){ int i, j n int a[10][10]; int sum=0; cout<<"Enter matrixs order"; cin>>n; cout<<endl<<"Enter elements of " .<<. for(i=0; i<n;++i) for(j=0;j<c;++j) { cout<<"Enter element of<<i+1<<j+1; cin>>a[i][j]; >} cout<<enda<<"Entered matrix was"; for(i=0;i<n;++i) for(j=0;j<n ++j) { cout<<" "<<a[i)[j) if (j==n-i) cout<<endl; } for(i=0; i<n; ++i) { sum+=a[i<)[i]; } cout<<"The Sum of its diagonal element is"<<sum; }
20th Nov 2016, 4:14 PM
Sandeep Chatterjee
+ 1
this is another program that is small version it only displays the sum of the matrix it is like mattews program but good int main(){ int mat[N][N]; int sum=0 int i=0; for.(i=0;i<N;i++) { sum+=mat[i][i"] } cout<<sum
21st Nov 2016, 1:52 PM
Sandeep Chatterjee
0
int main(){ int mat[N][N]; int sum=0; for(int i=0; i<N; i++){ for(int j=0; j<N; j++){ if(i==j){ sum+=mat[i][j]; } } } }
20th Nov 2016, 2:37 PM
Matteo Tardelli
Matteo Tardelli - avatar
0
design a class that will respesent a diagonal matrix of integers. care must be taken that no memory is reserved for storing any information that can is already known to the user. constructor with no parameter will represent a null matrix; cunstructor with 1 interger parameter say n will either represent a nXn diagonal martix with all diagonal elements set to 0 or represent a null matrix if n is invalid. set_element(r,c, value); function will store the value for the r-th row and c-th column of the matrix. provided r and c are valid for the matrix. get_element(r,c); function will return the value of the r-th row and c-th column of the matrix. provided r and c are valid for the matrix. design the destructor properly write a program to enter the values to the diagonal matrix of user given order. and also to display the matrix in human readable form. function for entering the matrix and displaying the matrix MAY of MAY NOT be the part of the class. design of the class : 2 (entire will be 0 if public data me
13th Dec 2021, 4:18 AM
Biswajit Das
Biswajit Das - avatar