Dragan Zrilic
Dragan Zrilic

Reputation: 23

Addition and multiplication of two polynomials in C

So I'm having a problem with my code. The only thing that is left is the degree printout of my polynomials. Can someone tell me what should I modify to fix it? (Note* the code must use these functions,and I can't use pointers to made it work)

    #include <stdio.h>
// Dva polinoma,sabrati,izmnoziti i ispisati
typedef struct polinom
{
    int koef[100];
    int n;
}POLINOM;

POLINOM citaj();
POLINOM saberi(POLINOM, POLINOM);
POLINOM pomnozi(POLINOM, POLINOM);
void ispisi(POLINOM);

int main()
{
    POLINOM pol1, pol2, zbir, przvd;
    printf("1. Polinom:\n");
    pol1 = citaj();
    printf("\n2. Polinom:\n");
    pol2 = citaj();
    zbir = saberi(pol1, pol2);
    printf("\n Zbir:\n");
    ispisi(zbir);
    przvd = pomnozi(pol1, pol2);
    printf("\n Proizvod:\n");
    ispisi(przvd);
    getch();
}

POLINOM citaj()
{
    POLINOM pol;
    int i;
    do
    {
        printf("Najveci stepen polinoma: "), scanf("%d", &pol.n);
    } while (pol.n < 1 || pol.n>100);
    for (i = 0; i < 100; i++)
    {
        if (i <= pol.n)
        {
            printf("(X%d)=", i), scanf("%d", &pol.koef[i]);
        }
        else
            pol.koef[i] = 0;
    }
    return pol;
}

POLINOM saberi(POLINOM pol1, POLINOM pol2)

{
    POLINOM zbir;
    int i;
    if (pol1.n > pol2.n)
        zbir.n = pol1.n;
    else
        zbir.n = pol2.n;
    for (i = 0; i < 100; i++)
        zbir.koef[i] = pol1.koef[i] + pol2.koef[i];

    return zbir;
}

POLINOM pomnozi(POLINOM pol1, POLINOM pol2)

{
    POLINOM przvd;
    int i, j;
    if (pol1.n > pol2.n)
    {
        przvd.n = pol1.n;
        for (i = 0; i <= przvd.n; i++)
        for (j = 0; j <= przvd.n; j++)
            przvd.koef[i] = pol1.koef[i] * pol2.koef[j];
    }
    else
    {
        przvd.n = pol2.n;
        for (i = 0; i <= przvd.n; i++)
        for (j = 0; j <= przvd.n; j++)
                przvd.koef[i + j] += pol1.koef[i] * pol2.koef[j];
    }
    return przvd;
}

void ispisi(POLINOM a)
{
    int i;
    for (i = 0; i <= a.n; i++)
        printf("(X%d)= %d ", i, a.koef[i]);
}

Here is an example of my code being run:

$ ./a.out
1. Polinom:
Najveci stepen polinoma: ^C
sundev19:/home/jgalloway12 $ ./a.out
1. Polinom:
Najveci stepen polinoma: 5
(X0)=4
(X1)=3
(X2)=2
(X3)=1
(X4)=5
(X5)=6

2. Polinom:
Najveci stepen polinoma: 5
(X0)=1
(X1)=2
(X2)=3
(X3)=4
(X4)=5
(X5)=6

 Zbir:
(X0)= 5 (X1)= 5 (X2)= 5 (X3)= 5 (X4)= 10 (X5)= 12
 Proizvod:
(X0)= -4199088 (X1)= 16 (X2)= 25 (X3)= 35 (X4)= 50 (X5)= 76 $

Fixed it this is the working code if anyone needs it:

#include <stdio.h>
// Dva polinoma,sabrati,izmnoziti i ispisati
typedef struct polinom
{
    int koef[100];
    int n;
}POLINOM;

POLINOM citaj();
POLINOM saberi(POLINOM, POLINOM);
POLINOM pomnozi(POLINOM, POLINOM);
void ispisi(POLINOM);

int main()
{
    POLINOM pol1, pol2, zbir, przvd;
    printf("1. Polinom:\n");
    pol1 = citaj();
    printf("\n2. Polinom:\n");
    pol2 = citaj();
    zbir = saberi(pol1, pol2);
    printf("\n Zbir:\n");
    ispisi(zbir);
    przvd = pomnozi(pol1, pol2);
    printf("\n Proizvod:\n");
    ispisi(przvd);
    getch();
}

POLINOM citaj()
{
    POLINOM pol;
    int i;
    do
    {
        printf("Najveci stepen polinoma: "), scanf("%d", &pol.n);
    } while (pol.n < 1 || pol.n>100);
    for (i = 0; i < 100; i++)
    {
        if (i <= pol.n)
        {
            printf("(X%d)=", i), scanf("%d", &pol.koef[i]);
        }
        else
            pol.koef[i] = 0;
    }
    return pol;
}

POLINOM saberi(POLINOM pol1, POLINOM pol2)

{
    POLINOM zbir;
    int i;
    if (pol1.n > pol2.n)
        zbir.n = pol1.n;
    else
        zbir.n = pol2.n;
    for (i = 0; i < 100; i++)
        zbir.koef[i] = pol1.koef[i] + pol2.koef[i];

    return zbir;
}

POLINOM pomnozi(POLINOM pol1, POLINOM pol2)

{
    POLINOM przvd;
    int i, j,k;
    przvd.n = pol1.n + pol2.n ;
    for (k = 0; k <= przvd.n; k++)
        przvd.koef[k] = 0;
        for (i = 0; i <= pol1.n; i++)
            for (j = 0; j <= pol2.n; j++)
            przvd.koef[i + j] += pol1.koef[i] * pol2.koef[j];
    return przvd;
}

void ispisi(POLINOM a)
{
    int i;
    for (i = 0; i <= a.n; i++)
        if(a.koef[i]!=0)
            printf("(X%d)= %d ", i, a.koef[i]);
}

Upvotes: 0

Views: 715

Answers (1)

user58697
user58697

Reputation: 7923

There are few issues with this code.

  • przvd coefficients are not initialized, so += adds initial garbage.

  • Both polynomials are looped up to the degree of the higher polynomial, meaning that non-existent coefficients of the lower polynomial are accessed.

It looks like you expect the coefficients being magically initialized to 0. It is not the case. In C, automatic variables are not initialized; you have to do it manually.

Also, both branches of pomnozi must use the same (convolution) formula. As coded, the first branch is mathematically wrong.

Upvotes: 1

Related Questions