#include <stdio.h>
#define MAX_SIZE 100
// 희소행열: 0값이 아닌 값만 저장하는 행열
// 전치행열: a 열 -> 전치 -> b의 행 바꾸는 것
typedef struct {
int row;
int col;
int value;
} term;
// befor a : 전치 전 희소행렬을 저장하기위한 구조체 배열로 선언
term a[100];
// after b : 전치 후 희소행렬을 저장하기위한 구조체 배열로 선언
term b[100];
void change(term x[], term y[]){
int n;
// 행,열,값의 정보를 b로 전치
b[0].row = a[0].col;
b[0].col = a[0].row;
n = b[0].value = a[0].value;
//값이 하나라도 있을때 col,row,value를 전치
if(n>0){
int current=1, i, j;
for(i=0; i<a[0].col; i++)
//a[0]에는 행령정보저장되어있음. a[1]부터 구조체배열원소값임
for(j=1; j<=n; j++){
if(a[j].col == i){
b[current].row = a[j].col;
b[current].col = a[j].row;
b[current].value = a[j].value;
current++;
}
}
}
}
int main() {
// ---------------------------------
// 1. 원래 희소행렬 정보 입력
// ---------------------------------
// 3행 4열, 0이 아닌 원소 4개
a[0].row = 3;
a[0].col = 4;
a[0].value = 4;
// 실제 데이터
a[1].row = 0;
a[1].col = 0;
a[1].value = 5;
a[2].row = 0;
a[2].col = 3;
a[2].value = 7;
a[3].row = 1;
a[3].col = 1;
a[3].value = 3;
a[4].row = 2;
a[4].col = 3;
a[4].value = 9;
// ---------------------------------
// 2. 전치 함수 호출
// ---------------------------------
change(a, b);
// ---------------------------------
// 3. 전치 결과 출력
// ---------------------------------
printf("=== 전치 결과 ===\n");
printf("행:%d 열:%d 값개수:%d\n",
b[0].row,
b[0].col,
b[0].value);
for (int i = 1; i <= b[0].value; i++) {
printf("b[%d] = row:%d col:%d value:%d\n",
i,
b[i].row,
b[i].col,
b[i].value);
}
printf("Hello world!\n");
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: