C로 배우는 쉬운 자료구조 - 희소 행렬을 전치 행렬로 변환
희소행렬을 전치행렬로 변환
소 스 |
|
#include<stdio.h> #define ROW 8 #define COL 7
void sparse_array(int (*a)[COL], int count); void trans_array(int (*a)[COL], int count);
int main(void) { int a[ROW][COL]={{ 0, 0, 2, 0, 0, 0,12}, {0, 0, 0, 0, 7, 0, 0}, {23, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 31, 0, 0, 0}, {0, 14, 0, 0, 0, 25, 0}, {0, 0, 0, 0, 0, 0, 6}, {52, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 11, 0, 0}, }; int i, j, count=0; printf(" ======== 희소 행렬 ========\n"); for(i=0;i<ROW;i++){ for(j=0;j<COL;j++){ printf("%4d",a[i][j]); if(a[i][j] !=0) count++; } printf("\n"); } sparse_array(a,count);
} void sparse_array(int (*a)[COL], int count) { int i,j; printf("\n==0이 아닌 행렬 출력==\n"); printf("%d\t%d\t%d\n\n", ROW, COL, count);
for(i=0;i<ROW;i++) for(j=0;j<COL;j++) if(a[i][j] !=0) printf("%d\t%d\t%d\n",i,j,a[i][j]); trans_array(a,count); }
void trans_array(int (*a)[COL], int count) { int i,j; printf("\n==== 전치 행렬 ==== \n"); printf("%d\t%d\t%d\n\n", COL, ROW, count); for(i=0;i<COL;i++) { for(j=0;j<ROW;j++) if(a[j][i] !=0) printf("%d\t%d\t%d\n", i,j,a[j][i]); } } |
'Book' 카테고리의 다른 글
[한빛미디어] C로 배우는 쉬운 자료구조 - 5장 연습문제 (0) | 2009.05.08 |
---|---|
C로 배우는 쉬운 자료구조 - 링크드 리스트 구현하기 (1) | 2009.05.08 |
[한빛미디어] 인터넷 프로그래밍 입문 12장 연습문제 (0) | 2009.05.08 |
[한빛미디어] 인터넷 프로그래밍 입문 11장 연습문제 (0) | 2009.05.08 |
[한빛미디어] 인터넷 프로그래밍 입문 10장 연습문제 (0) | 2009.04.28 |