Write a program that checks whether a matric is a symmetric matrix.
A symmetric matrix is a square matrix which is equal to its transpose.
Difficulty level
This exercise is mostly suitable for students
#include <stdio.h>
#include <conio.h>
#define SIZE 50
void main()
{
int A[SIZE][SIZE];
int dim; /* dimensions */
int I, J, aux;
do {
printf("Number of rows/columns (max.%d) : ", SIZE);
scanf("%d", &dim);
} while (dim <= 0 || dim > SIZE);
printf("*** Matrix ***\n");
for (I = 0; I<dim; I++)
for (J = 0; J<dim; J++)
{
printf("A[%d][%d] : ", I, J);
scanf("%d", &A[I][J]);
}
printf(" Matrix \n");
for (I = 0; I<dim; I++)
{
for (J = 0; J<dim; J++)
printf("%6d", A[I][J]);
printf("\n");
}
// we loop over the rows
for (I = 0; I<dim; I++)
{
for (J = 0; J<I; J++)
if(A[I][J] != A[J][I])
break;
if(J<I)
break;
}
if(I==dim)
printf(" Symmetric matrix \n");
else
printf(" NOT Symmetric matrix \n");
getch();
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Almost uniform hashing function