구조체 사용하여 다항식의 덧셈 구현하는 문제

조회수 358회
#include <stdio.h>
#define MAX(a,b) ((a>b) ? a : b)
#define MAX_DEGREE 101

typedef struct {
    int degree;
    int coef[MAX_DEGREE];
} polynomial;

polynomial poly_add(polynomial A, polynomial B) {
    polynomial C;
    int degree_a = A.degree;
    int degree_b = B.degree;
    int degree_c = MAX(degree_a, degree_b);
    C.degree = degree_c;

    while (degree_c <= 0) {
        C.coef[degree_c] = A.coef[degree_c] + B.coef[degree_c];
        degree_c--;
    }

    return C;
}

int main() {
    polynomial a = { 5, {3, 6, 0, 0, 0, 10} };
    polynomial b = { 4, {7, 0, 5, 0, 1} };
    polynomial c = poly_add(a, b);

    return 0;
}

while문의 연산에서

  • C6385: 'A.coef'에서 잘못된 데이터를 읽는 중입니다.
  • C6385: 'B.coef'에서 잘못된 데이터를 읽는 중입니다.
  • C6385: 'C.coef'에 쓰는 동안 버퍼 오버런이 발생했습니다.

위의 에러들이 뜨는데, 왜 안 되는 건지 이해하기 어렵네요. 도와주세요!

1 답변

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)