Every square matrix has two diagonals: the main diagonal (top-left to bottom-right, elements a[i][i]) and the anti-diagonal (top-right to bottom-left, elements a[i][n-1-i]). This program reads a square matrix, swaps each main-diagonal element with the anti-diagonal element in the same row, and prints the result.
The original post used void main(), a broken scanf format string ("%dx%d" instead of "%d %d"), and broken \n in all output. This rewrite fixes those issues and adds a clear trace of the swap operation.
Which Elements Are Swapped?
For row i, the swap is: a[i][i] ↔ a[i][n-1-i]
| Row i | Main diagonal index | Anti-diagonal index |
|---|---|---|
| 0 | [0][0] | [0][n-1] |
| 1 | [1][1] | [1][n-2] |
| n/2 | [n/2][n/2] | [n/2][n/2] (center of odd matrix — self-swap, no change) |
| n-2 | [n-2][n-2] | [n-2][1] |
| n-1 | [n-1][n-1] | [n-1][0] |
C Program: Swap Main and Anti-Diagonal Elements
/* Swap main diagonal with anti-diagonal elements in a square matrix
* Compile: gcc -ansi -Wall -Wextra swap_diag.c -o swap_diag */
#include <stdio.h>
#define MAXN 10
static void print_matrix(int a[][MAXN], int n)
{
int i, j;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++)
printf("%4d", a[i][j]);
printf("\n");
}
}
int main(void)
{
int a[MAXN][MAXN];
int i, j, n, tmp;
printf("Enter order of square matrix (max %d): ", MAXN);
if (scanf("%d", &n) != 1 || n < 1 || n > MAXN) {
printf("Invalid order.\n");
return 1;
}
printf("Enter %d x %d matrix elements:\n", n, n);
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
if (scanf("%d", &a[i][j]) != 1) { printf("Invalid input.\n"); return 1; }
printf("\nOriginal matrix:\n");
print_matrix(a, n);
/* Swap main diagonal a[i][i] with anti-diagonal a[i][n-1-i] */
for (i = 0; i < n; i++) {
tmp = a[i][i];
a[i][i] = a[i][n - 1 - i];
a[i][n - 1 - i] = tmp;
}
printf("\nAfter swapping main diagonal with anti-diagonal:\n");
print_matrix(a, n);
return 0;
}
How to Compile and Run
gcc -ansi -Wall -Wextra swap_diag.c -o swap_diag
./swap_diag
Sample Run — 3×3 Matrix
Enter order of square matrix (max 10): 3 Enter 3 x 3 matrix elements: 1 2 3 4 5 6 7 8 9 Original matrix: 1 2 3 4 5 6 7 8 9 After swapping main diagonal with anti-diagonal: 3 2 1 4 5 6 9 8 7
Trace of the Swap (n=3)
| Row i | a[i][i] (main diag) | a[i][n-1-i] (anti-diag) | After swap |
|---|---|---|---|
| 0 | a[0][0] = 1 | a[0][2] = 3 | a[0][0]=3, a[0][2]=1 |
| 1 | a[1][1] = 5 | a[1][1] = 5 (same) | no change (center) |
| 2 | a[2][2] = 9 | a[2][0] = 7 | a[2][2]=7, a[2][0]=9 |
Main diagonal before: {1, 5, 9} → after: {3, 5, 7}
Anti-diagonal before: {3, 5, 7} → after: {1, 5, 9}
Sample Run — 4×4 Matrix
Enter order of square matrix (max 10): 4 Enter 4 x 4 matrix elements: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Original matrix: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 After swapping main diagonal with anti-diagonal: 4 2 3 1 5 7 6 8 9 11 10 12 16 14 15 13
Code Explanation
- Anti-diagonal index formula — for a row
iin ann×nmatrix, the anti-diagonal element is at columnn-1-i. Row 0 has its anti-diagonal at column n-1. Row n-1 has its anti-diagonal at column 0. This mirrors the column index around the center. - Odd-sized matrix center — when n is odd, row i=n/2 is the center row. Its main diagonal element and anti-diagonal element are the same position (the exact center of the matrix), so the swap is a no-op. The result is that the center element never changes.
- Three-variable swap — the swap uses
tmp = a[i][i]; a[i][i] = a[i][n-1-i]; a[i][n-1-i] = tmp;. This is the standard three-step swap. Without a temporary variable, one of the values would be overwritten before it can be copied. - Only square matrices — the program validates
n ≥ 1. The “main diagonal” concept only makes sense for square matrices (where row count equals column count). For rectangular matrices, you would need separate row and column dimensions, and the diagonal definitions change. - print_matrix as a helper — extracting the print loop into a function avoids repeating it. It takes a 2D array pointer where the second dimension is fixed at MAXN. In C89, you cannot declare a function with a variable-length second dimension — the size must be a compile-time constant.
What This Program Teaches
- Matrix diagonal indexing — the main diagonal is
a[i][i](same row and column). The anti-diagonal isa[i][n-1-i](column = n-1 minus row). Both are O(n) elements in an n×n matrix. - 2D array passing in C — when passing a 2D array to a function, you must specify the second dimension in the parameter:
int a[][MAXN]. Without it, the compiler cannot calculate the element offsets. This is a common beginner confusion. - Validate input before using it — checking
scanfreturn values and the range of n before using n to size loops prevents undefined behavior from bad input.
Related Programs
- Identity Matrix Check 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.