F M03 - Transpose 2 Hacker Rank Answer

 Write a program that accepts an NxM matrix as input and prints its transpose.

Details about transpose : https://en.wikipedia.org/wiki/Transpose

Note that you also have to take as input N and M, the size of the matrix.

Sample Input 0

2 4
1 2 3 4
5 6 7 8

Sample Output 0

1 5
2 6
3 7
4 8
Source Code
#include <stdio.h>
int main() {
    int a[10][10], transpose[10][10], r, c, i, j;
    
    scanf("%d %d", &r, &c);

    // Assigning elements to the matrix
    
    for (i = 0; i < r; ++i)
        for (j = 0; j < c; ++j) {
            
            scanf("%d", &a[i][j]);
        }

    // Displaying the matrix a[][]

    for (i = 0; i < r; ++i)
        for (j = 0; j < c; ++j) 

    // Finding the transpose of matrix a
    for (i = 0; i < r; ++i)
        for (j = 0; j < c; ++j) {
            transpose[j][i] = a[i][j];
        }

    // Displaying the transpose of matrix a
    
    for (i = 0; i < c; ++i)
        for (j = 0; j < r; ++j) {
            printf("%d ", transpose[i][j]);
            if (j == r - 1)
                printf("\n");
        }
    return 0;
}

Comments