Write a program to 4 X 4 matrix. Input the elements in the matrix and rotate the elements by one position of each row.
#include <stdio.h>
int main() {
int arr[4][4];
int i, j, a, b, f;
printf("\nInput numbers to 4*4 matrix");
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
printf("\nKey in the [%d][%d]) value", i + 1, j + 1);
scanf("%d", &arr[i][j]);
}
}
for (i = 0; i < 4; i++) {
for (j = 0, f = 0; j < 4; j++) {
if (i != j && f == 0)
continue;
a = arr[i][j];
b = arr[j][i];
arr[i][j] = b;
arr[j][i] = a;
f = 1;
}
}
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++)
printf("%d ", arr[i][j]);
printf("\n");
}
}
Comments
Leave a comment