AD

Q&A 자료구조 희소행렬 관련 질문

티스트
2019-03-23 05:05:10 2097 2 8

#include

#include

#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;

void matrix_print(SparseMatrix a);

SparseMatrix matrix_copy(SparseMatrix a);


int main()

{

    SparseMatrix m = {

        { { 0, 3, 7 },{ 1, 0, 9 },{ 1, 5, 8 },{ 3, 0, 6 },{ 3, 1, 5 },{ 4, 5, 1 },{ 5, 2, 2 } },

        6,

        6,

        7

    };


    SparseMatrix result;

    result = matrix_copy(m);

    matrix_print(result);


    return 0;

}


void matrix_print(SparseMatrix a)

{

    int aindex = 0;


    printf("===================\n"); // 문제는 프린트

    for (int r = 0; r < a.rows; r++) {

        printf("|");

        for (int c = 0; c < a.cols; c++) {

            if (a.data[aindex].col == c && a.data[aindex].row == r) {

                printf(" %d |", a.data[aindex].value);

                aindex++;

            }

            else {

                printf(" 0 |");

            }

        }

        printf("\n");

    }

    printf("===================\n");


}

SparseMatrix matrix_copy(SparseMatrix a)

{

    SparseMatrix b;

    int bindex = 0;

    b.rows = a.rows;

    b.cols = a.cols;

    b.terms = a.terms;

    if (a.terms > 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].row;

                    b.data[bindex].col = a.data[i].col;

                    b.data[bindex].value = a.data[i].value;

                    bindex++;

                }

            }

        }

    }


    return b;

}


위에 이건 희소행렬의 (행,열,값)을 받아서 0이 포함된 원래 행렬을 출력하는 전체 코드고요

문제가 있다고 추청되는 부분은

for (int r = 0; r < a.rows; r++) {

        printf("|");

        for (int c = 0; c < a.cols; c++) {

            if (a.data[aindex].col == c && a.data[aindex].row == r) {

                printf(" %d |", a.data[aindex].value);

                aindex++;

            }

            else {

                printf(" 0 |");

            }

        }

        printf("\n");

    }

여기(프린트하는 부분)입니다

원래 

0 0 0 7 0 0

9 0 0 0 0 8

0 0 0 0 0 0

6 5 0 0 0 0

0 0 0 0 0 1

0 0 2 0 0 0


이렇게 출력하고 싶은데


0 0 0 0 0 0

9 0 0 0 0 0

0 0 0 0 0 0

6 5 0 0 0 0

0 0 0 0 0 0

0 0 2 0 0 0

이렇게 자꾸 4열부터 누락이 생기네요

이렇게 오래걸릴지 모르고 지금까지 붙잡고 있었네요 살려주세요 ㅠㅠ

후원댓글 8
댓글 8개  
이전 댓글 더 보기
TWIP 잔액: 확인중
▲윗글 쓰앵님... 알고리즘... RAnDi65535
▼아랫글 쨔잔 고9마9웠어요ㅠ
자유강의Q&A프로젝트 소개
3
04-11
0
04-06
2
Q&A
c언어 질문이요! [10]
딸랑짤랑
04-03
6
04-03
2
04-02
2
자유
프로그래밍을 하면 [4]
저탱이너무커서한타발림
04-01
2
자유
보안쪽으로 진로잡고 공부시작하려는데 [4]
앙기모띠주는나무띠
04-01
21
자유
백준의 새로운 서비스 [5]
겜잘스만구독함
04-01
9
03-31
1
03-28
1
자유
맥북 질문 [1]
최두식두부가게
03-27
3
03-26
2
자유
여러분들의 생각은? [6]
베일쿤
03-26
9
프로젝트 소개
GuiLocker- A wrapper of dislocker [1]
riemannulus
03-25
2
03-24
1
Q&A
쓰앵님... 알고리즘... [16]
RAnDi65535
03-23
2
Q&A
쓰앵님... 알고리즘... [10]
RAnDi65535
03-23
5
자유
쨔잔 [2]
고9마9웠어요ㅠ
03-22
23
03-22
4
자유
Visual Studio 퀄리티 실화? [1]
멘탈조각
03-22
1
자유
c언어 질문입니당 [8]
쿠킹호일사람되기
03-20
인기글 글 쓰기