Write a C program to check unit matrix.
#include <stdio.h>
#define MAXROWS 10
#define MAXCOLS 10
int main() {
int i, j, order, flag = 0;
int mat[MAXROWS][MAXCOLS];
/* get the order of the matrix from the user */
printf("Enter the number of order:");
scanf("%d", &order);
/* Boundary Check */
if (order > MAXROWS || order < 0) {
printf("Boundary Level Exceeded!!\n");
return 0;
}
/* get the entries for the input matrix */
printf("\nEnter the matrix entries:\n");
for (i = 0; i < order; i++) {
for (j = 0; j < order; j++) {
scanf("%d", &mat[i][j]);
}
}
/* checking for diagonal matrix */
for (i = 0; i < order; i++) {
for (j = 0; j < order; j++) {
if ((i != j && mat[i][j] != 0) ||
(i == j && mat[i][j] != 1)) {
flag = 1;
goto end;
}
}
}
#define MAXROWS 10
#define MAXCOLS 10
int main() {
int i, j, order, flag = 0;
int mat[MAXROWS][MAXCOLS];
/* get the order of the matrix from the user */
printf("Enter the number of order:");
scanf("%d", &order);
/* Boundary Check */
if (order > MAXROWS || order < 0) {
printf("Boundary Level Exceeded!!\n");
return 0;
}
/* get the entries for the input matrix */
printf("\nEnter the matrix entries:\n");
for (i = 0; i < order; i++) {
for (j = 0; j < order; j++) {
scanf("%d", &mat[i][j]);
}
}
/* checking for diagonal matrix */
for (i = 0; i < order; i++) {
for (j = 0; j < order; j++) {
if ((i != j && mat[i][j] != 0) ||
(i == j && mat[i][j] != 1)) {
flag = 1;
goto end;
}
}
}
end:
/* printing the result */
if (flag) {
printf("Given Matrix is not an identity matrix!!\n");
} else {
printf("Given Matrix is an identity matrix!!\n");
}
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
Enter the number of order:4
Enter the matrix entries:
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
Given Matrix is an identity matrix!!
Enter the number of order:4
Enter the matrix entries:
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
Given Matrix is an identity matrix!!
No comments:
Post a Comment