Write a program that swaps values between different locations in a two dimensional array. Make sure that the locations between which the values are being exchanged are symmetric.
#include <stdio.h>
int main ()
{
int size=4;
int matrix[4][4];
int i,j;
int tempValue;
for (i=0;i<size;i++)
{
for (j=0;j<size;j++)
{
printf("Enter matrix[%d][%d]: ",i,j);
scanf("%d",&matrix[i][j]);
}
}
//swapping
for(i=0;i<size;i++)
{
tempValue = matrix[i][0];
matrix[i][0] = matrix[i][3];
matrix[i][3] = tempValue;
tempValue = matrix[i][1];
matrix[i][1] = matrix[i][2];
matrix[i][2] = tempValue;
}
printf("\n");
for(i=0;i<size;i++)
{
printf("\n");
for (j=0;j<size;j++)
{
printf("%d\t",matrix[i][j]);
}
}
getchar();
getchar();
}
Comments
Leave a comment