summaryrefslogtreecommitdiff
path: root/transpose.c
blob: 32c769be094da55c9b69f8d57fd6cd02a40673f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*File: transpose.c The transpose of matrix A is obtained by interchanging the rows and columns. */
#include <stdio.h>

#define M 10
#define N 20

int main()
{

    double a[M][N], b[N][M];
    int row,col;

    for(row=0;row<M;row++)
    {
        for(col=0;col<N;col++)
        {
            a[row][col] = row*col;
        }
    }

    for(row=0;row<M;row++)
    {
        for(col=0;col<N;col++)
        {
            b[col][row]=a[row][col];
        }
    }
    
    printf("Matrix a is\n");
    for(row=0;row<M;row++)
    {
        for(col=0;col<N;col++)
        {
            printf("%3i ",(int)a[row][col]);
        }
        printf("\n");
    }
    
    printf("\nMatrix b is \n");
    for(row=0;row<N;row++)
    {
        for(col=0;col<M;col++)
        {
            printf("%3i ",(int)b[row][col]);
        }
        printf("\n");
    }
    
    printf("\nAn identity matrix of size %i\n",M);
    int ident[M][M];
    for(row=0;row<M;row++)
    {
        ident[row][row] = 1;
    }
    
    for(row=0;row<M;row++)
    {
        for(col=0;col<M;col++)
        {
            printf("%i ",ident[row][col]);
        }
        printf("\n");
    }
    return 0;
}