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 n;
printf("Enter size of 2D matrix : ");
scanf("%d", &n);
float arr[n][n];
int i,j;
printf("Enter matrix Elements \n");
for (i=0;i<n;i++)
{
for (j=0;j<n;j++)
{
scanf("%f",&arr[i][j]);
}
}
printf("\n Original Array \n");
for(i=0;i<n;i++)
{
printf("\n");
for (j=0;j<n;j++)
{
printf("%g\t",arr[i][j]);
}
}
for(i=0;i<n;i++)
{
for (j=0;j<n;j++)
{
if(i!=j)
{
float temp = arr[i][j];
arr[i][j] = arr[j][i];
arr[j][i] = temp;
}
}
}
printf("\n\n");
printf("\n Final Array after swapping \n");
for(i=0;i<n;i++)
{
printf("\n");
for (j=0;j<n;j++)
{
printf("%g\t",arr[i][j]);
}
}
}
Comments
Leave a comment