Write a program which will ask the user to enter the size and elements for 6 different square matrices.
And then tell the user that is these symmetrical matrices or not? (A matrix (M) is said to be
symmetrical if MT = M).
Hints
• Code a function which will ask the user to enter the elements of a 2D array with R rows and C
columns. (Dynamic memory allocation will be useful here).
#include <iostream>
using namespace std;
const int MAX = 100;
bool isSymmetric(int mat[][MAX], int N)
{
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
if (mat[i][j] != mat[j][i])
return false;
return true;
}
int main()
{
int mat[][MAX] = { { 1, 3, 5 },
{ 3, 2, 4 },
{ 5, 4, 1 } };
if (isSymmetric(mat, 3))
cout << "Yes";
else
cout << "No";
return 0;
}
Comments
Leave a comment