Skip to content

Instantly share code, notes, and snippets.

@tarunasolanki
Created August 21, 2019 13:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tarunasolanki/ef145cd2cf35e1c0e2621739c21a49bb to your computer and use it in GitHub Desktop.
Save tarunasolanki/ef145cd2cf35e1c0e2621739c21a49bb to your computer and use it in GitHub Desktop.
Program To Find Trace Of Matrix In 2D Array In c++
#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