IB001 – Seminární skupiny 16, 17, 18 a 19

Transponovanie matice – zdrojový kód

/*
   Program na transponovanie matice
*/

#include <stdio.h>

#define SIZE 4

int main(void)
{
    int matica[SIZE][SIZE] = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};

    // vypis matice
    printf("Matica:\n");
    for (int i = 0; i < SIZE; i++)
    {
        for (int j = 0; j < SIZE; j++)
        {
            printf("%3d", matica[i][j]);
        }
        puts("");
    }

    // transponovanie matice
    for (int i = 0; i < SIZE; i++)
    {
        for (int j = i + 1; j < SIZE; j++)
        {
            int pom = matica[j][i];
            matica[j][i] = matica[i][j];
            matica[i][j] = pom;
        }
    }

    // vypis matice po transponovani
    printf("\nMatica po transponovani:\n");
    for (int i = 0; i < SIZE; i++)
    {
        for (int j = 0; j < SIZE; j++)
        {
            printf("%3d", matica[i][j]);
        }
        puts("");
    }

    return 0;
}