-1

I want to add two polynom and set the results on the polynom result I have a problem with the third function please any indications I really need help PS: dynamic declaration! It seems that i don't understand pointers very well so it would be helpful if you suggest to me anything may help me to understand this chapter, please!

     typedef struct {
            int degree;
            float* coeff_tab;
        } Polynome;


        Polynome lire_polynome()
        {
            Polynome p;
            int i;

            printf("Degree du polynome? ");
            scanf("%d", &p.degree);
            p.coeff_tab = (float*)malloc((unsigned long)(p.degree+1)*sizeof (float));

            for(i=0; i<=p.degree; i++)
            {
                printf("coeff de degree %i: ", i);
                scanf("%f", &p.coeff_tab[i]);
            }
            //free(p.coeff_tab);

            return p;
        }


        void affiche_polynome(Polynome p)
        {
            int i;
            if(p.degree > 1){
                printf("%4.2fX^%i ", p.coeff_tab[p.degree], p.degree);
                for(i=p.degree-1; i>1; i--){
                    printf(" + %4.2fX^%i ", p.coeff_tab[i], i);
                }
                printf(" + %4.2fX + %4.2f\n", p.coeff_tab[1], p.coeff_tab[0]);
            } else {
                printf(" + %4.2f\n", p.coeff_tab[0]);
            }
        }
        void sommePoly(Polynome p1 , Polynome p2 , Polynome psom)
 {
     int degsom,deg1,deg2,i=0,j=0,k=0;
     float coeffsum,coeff1,coeff2;
     Polynome *P,*S ,*Psom;
     P=&p1; S=&p2;
     degsom=max(p1.degree,p2.degree);
     psom.coeff_tab=(float*)malloc((unsigned long)(degsom+1)*sizeof (float));
     Psom=&psom;
     if( p1.degree == p2.degree)

     {
        psom.coeff_tab[k]=p1.coeff_tab[i]+p2.coeff_tab[j];
        psom.degree=p1.degree;
        Psom ++;
     i++;j++;k++;
     }
     else if(p1.degree > p2.degree) {
        psom.degree=p1.degree;
        psom.coeff_tab[k]=p1.coeff_tab[i];
        i++;k++;
    }else {
        psom.coeff_tab[k]=p2.coeff_tab[j];
        psom.degree=p2.degree;
        j++;k++;

    }
          }
Doches
  • 3,276
  • 2
  • 19
  • 26
Jihane.j
  • 27
  • 9
  • Can you create an [MCVE](https://stackoverflow.com/help/mcve)? How to call these functions? Can you give example input? example output? expected desired output? – KamilCuk Nov 11 '18 at 08:49
  • YEAH the two first functions work , but the last one it's completely false ... – Jihane.j Nov 11 '18 at 08:53
  • example input Polynome p=lire_polynome(); affiche_polynome(p); – Jihane.j Nov 11 '18 at 08:54
  • `scanf("%f", &p.coeff_tab[i]);` Can you post example input you give to your program? What output does your program give for it? What would you want your program to print? – KamilCuk Nov 11 '18 at 08:57
  • In the main function theres this : Polynome p=lire_polynome() ; /* reading the Polynome number one */ affiche_polynome(p); /* then reading poly 2 */ Polynome s=lire_polynome(); affiche_polynome(s); – Jihane.j Nov 11 '18 at 09:10
  • I will just give the degree of polynoms and the coefficients then it shows to me degree of polynom ? 2. coeff of deg 0 : 1 coeff of deg 1 : 2 coeff of def 2 : 3 3.00X^2 + 2.00X + 1.00 – Jihane.j Nov 11 '18 at 09:14
  • I have literally asked what do you type from your keyboard. From [man scanf](https://linux.die.net/man/3/scanf) there is `The scanf() function reads input from the standard input stream`. My question is: What do you put on stdin of your program? That is the input you give to your program. You can edit your post, however I think I found the problem in my answer. – KamilCuk Nov 11 '18 at 09:14
  • The same thing with polynom number 2 ; and I want my program to print 6.00X^2+4.00X+2 it's just an example ( it's the addition of two polynoms simply ) – Jihane.j Nov 11 '18 at 09:16
  • I told you I give to my program the deg and the coefficients of polynom that's all – Jihane.j Nov 11 '18 at 09:17
  • Scanf ("%f",&p.coeff_tab[I] );. Reads the coefficients of polynom it's clear I think – Jihane.j Nov 11 '18 at 09:20
  • Start with indenting your code. – n. m. could be an AI Nov 11 '18 at 10:05

2 Answers2

1

void sommePoly(Polynome p1 , Polynome p2 , Polynome psom)

In C arguments are passed by making a copy of the value of the variable. So p1 is a copy of the variable you pass is it. You need to pass pointers in order to modify the values behind pointers. Or return the value, as you do in case of Polynome lire_polynome().

Usually I would expect functions:

int lire_polynome(Polynome *p);
int sommePoly(Polynome *p1 , Polynome *p2 , Polynome *psom);

Using some return value ex. int you can return a negative or non-zero number in case of error (eg. malloc error).

So you function may look like this:

int sommePoly(Polynome *p1 , Polynome *p2 , Polynome *psom)
{
     int degsom, deg1, deg2, i = 0, j = 0, k = 0;
     float coeffsum, coeff1, coeff2;
     degsom = max(p1->degree, p2->degree);
     psom->coeff_tab = malloc((degsom + 1) * sizeof(float));
     if (psom->coeff_tab == NULL) { return -1; }
     if (p1->degree == p2->degree){
        psom->coeff_tab[k] = p1->coeff_tab[i] + p2->coeff_tab[j];
        psom->degree = p1->degree;
     ... // and so on replace psom. to psom-> ie. replace dot with ->
    }
    return 0;
}

Then you call it passing pointer to structures:

Polynome p1;
if (lire_polynome(&p1) != 0) { /* error handling */ }
Polynome p2;
if (lire_polynome(&p2) != 0) { /* error ahdling */ }
Polynome p3;
if (sommePoly(&p1, &p2, &p3) != 0) { /* error handling */ }
  1. Remember to check for scanf errors if (scanf("%f", ...) != 1) { fprintf(stderr, "error in scanf"); exit(1); }
  2. Remember to check for malloc errors.
  3. Don't cast result of malloc
  4. The casting to unsigned long in (unsigned long)(p.degree+1)*sizeof (float) is strange and rather error prone. sizeof(...) is of type size_t, which is the correct type for size representation, casting it to unsigned long is unnecesery and may result in errors in some big number cases. Just malloc((p.degree + 1) * sizeof(float)) or calloc(p->degree + 1, sizeof(float));
  5. Grab a good read about linux kernel coding style.
  6. A more simple example of passing pointers and modifing value can be found in this thread.
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
1

I think this version of sommePoly is what you are looking for:

void sommePoly(Polynome *p1 , Polynome *p2 , Polynome *psom)
{
        int     i;
        Polynome        *big, *sml;

        if (p1->degree > p2->degree) {
                big = p1;
                sml = p2;
        }
        else {
                big = p2;
                sml = p1;
        }

        psom->degree = big->degree;
        psom->coeff_tab = calloc(psom->degree + 1, sizeof(float));

        for (i = 0; i < sml->degree + 1; i++)
        {
                psom->coeff_tab[i] = big->coeff_tab[i] + sml->coeff_tab[i];
        }
        for (; i < big->degree + 1; i++)
        {
                psom->coeff_tab[i] = big->coeff_tab[i];
        }
}

TIP 1: Try using calloc() instead of malloc(). It is much safer when trying to allocate any array of N items of Z size. TIP 2: And of course always check return value of calloc()/malloc() before dereferencing.

To illustrate intended usage of sommePoly:

void printPoly(Polynome *p)
{
        int     i;
        for (i = p->degree; i > 0; i--) {
                if (0.0 != p->coeff_tab[i])
                        printf("%4.2fX^%i + ", p->coeff_tab[i], i);
        }
        if (0.0 != p->coeff_tab[i])
                printf("%4.2f\n", p->coeff_tab[0]);
}

int main(int argc, char *argv[])
{
        Polynome        a, b, sum;

        a.degree = 5;
        a.coeff_tab = calloc(a.degree + 1, sizeof(float));
        a.coeff_tab[0] =  1.0;
        a.coeff_tab[1] =  8.0;
        a.coeff_tab[2] = -2.0;
        a.coeff_tab[3] =  0.0;
        a.coeff_tab[4] =  3.0;
        a.coeff_tab[5] =  1.0;

        b.degree = 3;
        b.coeff_tab = calloc(b.degree + 1, sizeof(float));
        b.coeff_tab[0] =  1.0;
        b.coeff_tab[1] = -3.0;
        b.coeff_tab[2] =  5.0;
        b.coeff_tab[3] =  7.0;

        sommePoly(&a, &b, &sum);

        printPoly(&a);
        printPoly(&b);
        printPoly(&sum);

        free(a.coeff_tab);
        free(b.coeff_tab);
        free(sum.coeff_tab);

        return 0;
}

and the output:

$ cc -g -O0 -Wall  poly.c -o poly
$ ./poly
1.00X^5 + 3.00X^4 + -2.00X^2 + 8.00X^1 + 1.00
7.00X^3 + 5.00X^2 + -3.00X^1 + 1.00
1.00X^5 + 3.00X^4 + 7.00X^3 + 3.00X^2 + 5.00X^1 + 2.00
  • What's about the multiplication ? – Jihane.j Nov 11 '18 at 10:56
  • Well, multiplication would be slightly more complicated than the sum function. You would need to iterate through each term of the two polynomials and carry out the multiplication: a for()-loop within a for()-loop. – user8583031 Nov 11 '18 at 18:39