Answer to Question #198341 in C for Ayesha

Question #198341

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-27T06:26:14-0400
#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();
}

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