The identity matrix (also called the unit matrix) is a square matrix in which every element on the main diagonal is 1, and all off-diagonal elements are 0. It is the multiplicative identity for matrix multiplication: for any matrix A, multiplying by the identity matrix I gives IA = AI = A. This program reads a matrix, checks whether it is an identity matrix, and prints the result.
The original post had void main(), broken \n throughout, an HTML comment (-->) left in the content, and a convoluted identity check condition. This rewrite uses a single clean condition: a[i][j] != (i == j ? 1 : 0).
C Program: Check if a Matrix is an Identity Matrix
/* Check if a matrix is an identity matrix
* Identity matrix: 1 on main diagonal, 0 everywhere else; must be square
* Compile: gcc -ansi -Wall -Wextra identity.c -o identity */
#include <stdio.h>
#define MAXN 10
int main(void)
{
int a[MAXN][MAXN];
int i, j, r, c, ok;
printf("Enter rows and columns: ");
if (scanf("%d %d", &r, &c) != 2 || r < 1 || c < 1 || r > MAXN || c > MAXN) {
printf("Invalid dimensions.\n");
return 1;
}
printf("Enter %d x %d matrix:\n", r, c);
for (i = 0; i < r; i++)
for (j = 0; j < c; j++)
if (scanf("%d", &a[i][j]) != 1) { printf("Invalid input.\n"); return 1; }
printf("\nMatrix:\n");
for (i = 0; i < r; i++) {
for (j = 0; j < c; j++)
printf("%3d", a[i][j]);
printf("\n");
}
/* Identity requires square matrix */
if (r != c) {
printf("\nNot an identity matrix (not square: %dx%d)\n", r, c);
return 0;
}
ok = 1;
for (i = 0; i < r && ok; i++)
for (j = 0; j < c && ok; j++)
if (a[i][j] != (i == j ? 1 : 0))
ok = 0;
printf("\n%s\n", ok ? "Identity matrix" : "Not an identity matrix");
return 0;
}
How to Compile and Run
gcc -ansi -Wall -Wextra identity.c -o identity
./identity
Sample Runs
3×3 identity matrix:
Enter rows and columns: 3 3 Enter 3 x 3 matrix: 1 0 0 0 1 0 0 0 1 Matrix: 1 0 0 0 1 0 0 0 1 Identity matrix
3×3 non-identity (a[0][2] = 2, not 0):
Enter rows and columns: 3 3 Enter 3 x 3 matrix: 1 0 2 0 1 0 0 0 1 Matrix: 1 0 2 0 1 0 0 0 1 Not an identity matrix
Non-square matrix (identity impossible):
Enter rows and columns: 2 3 Matrix: 1 0 0 0 1 0 Not an identity matrix (not square: 2x3)
Code Explanation
- Identity condition:
a[i][j] != (i == j ? 1 : 0)— wheni == j(diagonal), the expected value is 1. Wheni != j(off-diagonal), the expected value is 0. The ternary expression picks the correct expected value in a single check. If any element fails,okis set to 0 and the loops exit immediately (via the&& okcondition). - Early exit with
&& okin for-loop condition — writingfor (i = 0; i < r && ok; i++)causes the outer loop to stop as soon as a mismatch is found. Without this, the program would check every element unnecessarily after the first failure. This is more efficient than using agotoor aflagvariable with a separate break. - Non-square check before the loop — a non-square matrix can never be an identity matrix (the diagonal concept requires equal row and column counts). Checking this first avoids running the loop at all for rectangular inputs.
- Identity matrices at scale — in a 1×1 identity matrix: just {{1}}. In a 4×4: ones at [0][0], [1][1], [2][2], [3][3]; zeros everywhere else. The identity matrix has exactly n ones in an n×n matrix — all on the diagonal.
What This Program Teaches
- Diagonal vs off-diagonal indexing —
i == jselects diagonal elements.i != jselects off-diagonal elements.i < jselects the upper triangle.i > jselects the lower triangle. These index comparisons are fundamental to matrix programming in C. - Flag variable pattern — the
ok = 1flag starts as “assume true” and is cleared on the first counterexample. This pattern (assume success, disprove with evidence) is standard for validation loops. The result is read after the loop. - Ternary expression for conditional values —
(condition ? value_if_true : value_if_false)eliminates an if-else inside the comparison, making the check a single expression. This is clearer than:if (i==j) { if (a[i][j]!=1) ok=0; } else { if (a[i][j]!=0) ok=0; } - Properties of identity matrices — an identity matrix is its own inverse: I × I = I. It is also symmetric (I = IT). Multiplying any matrix A by I returns A unchanged (I × A = A × I = A). These properties make identity matrices the “1” of matrix algebra.
Related Programs
- Swap Diagonal Elements of Matrix in C
- Matrix Transpose in C
- Sum of Matrix Elements in C
- Sort Matrix Rows and Columns in C
- Pointers in C — Complete Guide
Recommended book:
The C Programming Language — Kernighan & Ritchie (India) |
(US)
|
C Programming: A Modern Approach — K.N. King (India) |
(US)
Practice what you learned: C Aptitude Questions — or try our C Programming Quiz App on Android.