Polynom manipulations









up vote
-1
down vote

favorite
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.2fn", p.coeff_tab[1], p.coeff_tab[0]);
else
printf(" + %4.2fn", 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++;












share|improve this question























  • Can you create an MCVE? How to call these functions? Can you give example input? example output? expected desired output?
    – Kamil Cuk
    Nov 11 at 8:49










  • YEAH the two first functions work , but the last one it's completely false ...
    – Jihane.j
    Nov 11 at 8:53










  • example input Polynome p=lire_polynome(); affiche_polynome(p);
    – Jihane.j
    Nov 11 at 8: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?
    – Kamil Cuk
    Nov 11 at 8: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 at 9:10














up vote
-1
down vote

favorite
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.2fn", p.coeff_tab[1], p.coeff_tab[0]);
else
printf(" + %4.2fn", 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++;












share|improve this question























  • Can you create an MCVE? How to call these functions? Can you give example input? example output? expected desired output?
    – Kamil Cuk
    Nov 11 at 8:49










  • YEAH the two first functions work , but the last one it's completely false ...
    – Jihane.j
    Nov 11 at 8:53










  • example input Polynome p=lire_polynome(); affiche_polynome(p);
    – Jihane.j
    Nov 11 at 8: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?
    – Kamil Cuk
    Nov 11 at 8: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 at 9:10












up vote
-1
down vote

favorite
1









up vote
-1
down vote

favorite
1






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.2fn", p.coeff_tab[1], p.coeff_tab[0]);
else
printf(" + %4.2fn", 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++;












share|improve this question















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.2fn", p.coeff_tab[1], p.coeff_tab[0]);
else
printf(" + %4.2fn", 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++;









c codeblocks






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 9:19









Doches

2,13811324




2,13811324










asked Nov 11 at 8:27









Jihane.j

14




14











  • Can you create an MCVE? How to call these functions? Can you give example input? example output? expected desired output?
    – Kamil Cuk
    Nov 11 at 8:49










  • YEAH the two first functions work , but the last one it's completely false ...
    – Jihane.j
    Nov 11 at 8:53










  • example input Polynome p=lire_polynome(); affiche_polynome(p);
    – Jihane.j
    Nov 11 at 8: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?
    – Kamil Cuk
    Nov 11 at 8: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 at 9:10
















  • Can you create an MCVE? How to call these functions? Can you give example input? example output? expected desired output?
    – Kamil Cuk
    Nov 11 at 8:49










  • YEAH the two first functions work , but the last one it's completely false ...
    – Jihane.j
    Nov 11 at 8:53










  • example input Polynome p=lire_polynome(); affiche_polynome(p);
    – Jihane.j
    Nov 11 at 8: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?
    – Kamil Cuk
    Nov 11 at 8: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 at 9:10















Can you create an MCVE? How to call these functions? Can you give example input? example output? expected desired output?
– Kamil Cuk
Nov 11 at 8:49




Can you create an MCVE? How to call these functions? Can you give example input? example output? expected desired output?
– Kamil Cuk
Nov 11 at 8:49












YEAH the two first functions work , but the last one it's completely false ...
– Jihane.j
Nov 11 at 8:53




YEAH the two first functions work , but the last one it's completely false ...
– Jihane.j
Nov 11 at 8:53












example input Polynome p=lire_polynome(); affiche_polynome(p);
– Jihane.j
Nov 11 at 8:54




example input Polynome p=lire_polynome(); affiche_polynome(p);
– Jihane.j
Nov 11 at 8: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?
– Kamil Cuk
Nov 11 at 8:57




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?
– Kamil Cuk
Nov 11 at 8: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 at 9:10




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 at 9:10












2 Answers
2






active

oldest

votes

















up vote
0
down vote













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.





share|improve this answer



























    up vote
    0
    down vote













    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.2fn", 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





    share|improve this answer






















    • What's about the multiplication ?
      – Jihane.j
      Nov 11 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 at 18:39










    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247017%2fpolynom-manipulations%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    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.





    share|improve this answer
























      up vote
      0
      down vote













      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.





      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        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.





        share|improve this answer












        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.






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 9:10









        Kamil Cuk

        7,8271222




        7,8271222






















            up vote
            0
            down vote













            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.2fn", 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





            share|improve this answer






















            • What's about the multiplication ?
              – Jihane.j
              Nov 11 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 at 18:39














            up vote
            0
            down vote













            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.2fn", 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





            share|improve this answer






















            • What's about the multiplication ?
              – Jihane.j
              Nov 11 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 at 18:39












            up vote
            0
            down vote










            up vote
            0
            down vote









            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.2fn", 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





            share|improve this answer














            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.2fn", 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






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 11 at 10:03

























            answered Nov 11 at 9:29









            user8583031

            264




            264











            • What's about the multiplication ?
              – Jihane.j
              Nov 11 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 at 18:39
















            • What's about the multiplication ?
              – Jihane.j
              Nov 11 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 at 18:39















            What's about the multiplication ?
            – Jihane.j
            Nov 11 at 10:56




            What's about the multiplication ?
            – Jihane.j
            Nov 11 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 at 18:39




            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 at 18:39

















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247017%2fpolynom-manipulations%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            27

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            Category:Rhetoric