Invert row with column in an array?

0
  

Make a program that randomly generates 20 integers in the range of 0 through 999 and fills in an array of 5 x 4 size. Show the matrix, then show the transposed matrix (reverse row with column).

I've already been able to populate the array with random numbers, but I do not know how to transpire.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
  int i, j, matriz[5][4], matriz5[4][5];
  srand(time(NULL));

   for (i = 0; i < 5; i++)
       for(j = 0; j < 4; j++)
           matriz[i][j] = rand()%999;

   for (i = 0; i < 5; i++){
        for(j = 0; j < 4; j++){
           printf(" %d ",matriz[i][j]);
       }
       printf("\n");
   }

  return 0;
}
    
asked by anonymous 03.11.2018 / 01:26

1 answer

0

To find the transpose of the matrix, simply reverse the indices in the assignment of the values, the line goes to the column and vice versa.

Exemplifying in your code, creating the transposed array in matriz5 :

for (i = 0; i < 5; i++) {
    for(j = 0; j < 4; j++) {
        matriz5[j][i] = matriz[i][j];
        //      ^--^-----------^--^
    }
}

Then pay close attention to the fact that the boundaries of rows and columns will also be inverted:

for (i = 0; i < 4; i++) {
//              ^--- 
    for(j = 0; j < 5; j++) {
    //             ^---
        printf(" %d ", matriz5[i][j]);
    }
    printf("\n");
}

See it working on Ideone

    
03.11.2018 / 01:45