Answer to Question #198917 in C for Nabeel khan

Question #198917

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.


1
Expert's answer
2021-05-28T01:15:30-0400
#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]);
        }
    }
}


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS