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
#include <stdio.h>
#include <stdlib.h>
 
#define MAX_TERMS 100
typedef struct {
    int row;
    int col;
    int value;
} element;
 
typedef struct SparseMatrix {
    element data[MAX_TERMS];
    int rows;    // ÇàÀÇ °³¼ö
    int cols;    // ¿­ÀÇ °³¼ö
    int terms;     // Ç×ÀÇ °³¼ö
} SparseMatrix;
 
SparseMatrix matrix_transpose2(SparseMatrix a)
{
    SparseMatrix b;
 
    int bindex;        // Çà·Ä b¿¡¼­ ÇöÀç ÀúÀå À§Ä¡
    b.rows = a.rows;
    b.cols = a.cols;
    b.terms = a.terms;
 
    if (a.terms > 0) {
        bindex = 0;
        for (int c = 0; c < a.cols; c++) {
            for (int i = 0; i < a.terms; i++) {
                if (a.data[i].col == c) {
                    b.data[bindex].row = a.data[i].col;
                    b.data[bindex].col = a.data[i].row;
                    b.data[bindex].value = a.data[i].value;
                    bindex++;
                }
            }
        }
    }
    return b;
}
 
void matrix_print(SparseMatrix a)
{
    printf("====================\n");
    for (int i = 0; i<a.terms; i++) {
        printf("(%d, %d, %d) \n", a.data[i].row, a.data[i].col, a.data[i].value);
    }
    printf("====================\n");
}
 
int main(void)
{
    SparseMatrix m = {
        { { 037 },{ 109 },{ 158 },{ 306 },{ 315 },{ 451 },{ 522 } },
        6,
        6,
        7
    };
    SparseMatrix result;
 
    result = matrix_transpose2(m);
    matrix_print(result);
    return 0;
}
cs