Problem of implementing the addition of polynomials using structures

Asked 1 years ago, Updated 1 years ago, 335 views

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

In the operation of a while statement,

The above errors appear, but it's hard to understand why it doesn't work. Help!

c struct

2023-04-15 06:40

1 Answers

I don't think these errors occurred in the current code state.

The wrong part of the current code is while (degree_c <=0) This part has an inverted inequality. It should be replaced with while (degree_c >=0).


2023-04-15 09:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.