Created
August 21, 2019 13:54
-
-
Save tarunasolanki/ef145cd2cf35e1c0e2621739c21a49bb to your computer and use it in GitHub Desktop.
Program To Find Trace Of Matrix In 2D Array In c++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
int main() | |
{ | |
int i , j , sum = 0 , a[3][3] ; | |
cout << "Trace matrix in 2D array " << endl ; | |
cout << "Enter Array Elements : " << endl ; | |
// loop for taking inputs in array | |
for (i = 0 ; i < 3 ; i++) | |
{ | |
for (j = 0 ; j < 3 ; j++) | |
{ | |
cin >> a[i][j] ; | |
} | |
} | |
// loop for printing array elements | |
for (i = 0 ; i < 3 ; i++) | |
{ | |
for (j = 0 ; j < 3 ; j++) | |
{ | |
cout << a[i][j] << " "; | |
} | |
cout << endl ; | |
} | |
//loop for finding trace of matrix | |
for(i = 0 ; i < 3 ; i++) | |
{ | |
for (j = 0 ; j < 3 ; j++) | |
{ | |
if (i==j) | |
{ | |
sum+=a[i][j] ; | |
} | |
} | |
} | |
cout << "Trace of matrix : " << sum << endl ; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment