c - Single variable polynomial addition -


i have write program in c whereby have add two, single-variable polynomials. can partly , end wrong answer.

consider 2 polynomials:

5x^2 + 6x^3 + 9   6x^3 + 5x^2 + 3x + 2   

i know answer going be, manually. here logic:

if(term1->exp == term2->exp){ // same power of x     // add them, store them in final answer linked list     // increment pointers of both term1 , term2 linkedlist } if(term1->exp > term2->exp){ // term1 has higher power of x term2     // increment term1 linked list in search of lower power of x     // ** term1 pending ** }  if(term1->exp < term2->exp){ // term1 has lesser power of x term2     // increment term2 linked list in search of lower power of x     // ** term2 pending ** }   

the problem face pending terms. how take care of pending terms ?
how add them ?

code here: http://pastebin.com/70ujdniq

i have read code.

you have memory issues inside function addpolyomials.
problem caused bad wrong logic: leeor has given solution in answer yet: need add words else before 2nd , 3rd ifs.
if don't, when 1st if gives true, new pointer created , added list, , an advance done.

thus, when tne 2nd if reached, comparisson done these new advanced pointers.
logical error.

by doing change program, there issues yet.

since initialize polynomial3 garbage, shown in final result.
maybe have initialize, also, components exp , coef (to 0?).

        polynomial3->exp = 0;         polyoomial3->coef = 0; 

you have error in main() when "create" polynomial2.
there, using list polynomial1.
line has changed to:

            polynomial2 = addterm(polynomial2,exp,coef); 

with theses changes, can watch logic of program.
obtained nonsense solution.
think idea of defining "pending" flag not enough.

your logic seems consider polynomials if exponents in order.
user not enter polynomials in way.
examples @ top of question have exponents unordered (the first polynomial).

i think idea handle lists @ moment entered, putting terms in order. since terms added 1 @ time, can achieved searching in list top of it, , inserting terms properly.
example, if have 5x^2 + 6x^3 + 9, first step insert 5 2 @ top of list polynomial1. in next iteration pair 6 3 inserted again @ top, before pair (5, 2), , on. final list be:

  (6, 3, next), (5, 2, next), (9, 0, next), null 

this procedure ensures can iterate want. (there minor problem: happens if user enters repeated exponent? in case no new element added list: add new coefficient!)

finally, can analyze "pending" coefficientes issue in sum.
observe while() iterating if both lists not null.
when while() arrives end, 3 situations possible:

         (polynomial1 != null && polynomial2==null)  or (polynomial2 != null && polynomial1==null),   or both null.   

it enough check (polynomial1 != null) , (polynomial2 != null).
in 1st case, "paste" pending terms of polynomial1 polynomial3.
in 2nd case, same polynomial2.

finally, in display function handle in better way terms coefficient 0.
better terms coefficient 0 not shown.

your program needs more improvement, out of scope of question.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -