Answer to Question #111251 in C for sara

Question #111251
Write a program to obtain transpose of a 4 x 4 matrix. The transpose of a matrix is
obtained by exchanging the elements of each row with the elements of the corresponding
column.
1
Expert's answer
2020-04-22T09:03:37-0400
#include<stdio.h>
#include<stdlib.h>
#include<time.h>


int main() {
	srand(time(NULL));


	const int N = 4;


	int arr[N][N];
	
	int i = 0;
	int j = 0;
	for (i = 0; i < N; i++)
		for (j = 0; j < N; j++) 
			arr[i][j] = rand() % 100;


	printf("Original matrix:\n");
	for (i = 0; i < N; i++) {
		for (j = 0; j < N; j++)
			printf("%d ", arr[i][j]);
		printf("\n");
	}


	for(i = 0;i<N;i++)
		for (j = i + 1; j < N; j++) {
			int temp = arr[i][j];
			arr[i][j] = arr[j][i];
			arr[j][i] = temp;
		}


	printf("\nTranspose matrix:\n");
	for (i = 0; i < N; i++) {
		for (j = 0; j < N; j++)
			printf("%d ", arr[i][j]);
		printf("\n");
	}


	return 0;
}

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