15,392,636 members
See more:
I am trying to add a monomial after creating a polynomial by asking user to enter number of nodes, coefficient and exponent. I have 2 polynomials. I want to ask user to choose one of the polynomials and then after that, add the entered monomial to this chosen polynomial.

But in my code, I don't know how to add those monimials to the chosen polynomials. I want to this with help of while function.

What I have tried:

```struct node
{
int exp;           //this line represents the exponent/power b for a*x^b
float coeff;       //this line represents the coefficient a for a*x^b
struct node *next; //this line represents the pointer which will point to the next node
};

struct node *create_new_nodes(struct node *m);
struct node *polynom_addition(struct node *p1, struct node *p2, struct node *polynom, struct node *ptrr);
void display(char const *tag, struct node *ptr1);
void del_list(struct node *list);

int main(void)
{
struct node *p1 = NULL, *p2 = NULL, *ptrr = NULL;

p1 = create_new_nodes(p1);
p2 = create_new_nodes(p2);

display("P1(x)", p1);
display("P2(x)", p2);
display("P(x)", ptrr);

del_list(p1);
del_list(p2);
del_list(ptrr);
return 0;
}

struct node *create_new_nodes(struct node *m)
{
int i, n;
struct node *head = NULL, *tail = NULL; /* temporary list pointers */

fputs("\nEnter the number of nodes: ", stdout);
if (scanf("%d", &n) != 1)
return NULL;

for (i = 0; i < n; i++)
{
struct node *ptr1 = malloc(sizeof *ptr1);

if (!ptr1)
{
{ /* this line checks if nodes in list */
free(victim); /* this line frees all nodes */
}
return NULL;
}
ptr1->next = NULL;

fputs("\nEnter the coefficient (a for a*x^b): ", stdout);
if (scanf("%f", &ptr1->coeff) != 1)
{
fputs("error: invalid integer input.\n", stderr);
return NULL;
}
fputs("Enter the exponent/power (b for a*x^b): ", stdout);
if (scanf("%d", &ptr1->exp) != 1)
{
fputs("error: invalid integer input.\n", stderr);
return NULL;
}
else
{
tail->next = ptr1; //this line sets the last node to the new node
tail = ptr1;       //this line upgrades tail to the new node
}
}
return head; //this line returns pointer to the beginning of the list
}

struct node *adding_monom(struct node *monomial, struct node *p1)
{
char choice;
int i, n;
struct node *ptr1 = malloc(sizeof *ptr1);

struct node *head = NULL, *tail = NULL; /* temporary list pointers */

fputs("\nDo you want to add a monomial to your polynomial? Y/N", stdout);
if (scanf("%c", &choice) == 'Y')
{
fputs("Which polynomial? First or second? 1/2", stdout);
if (scanf("%d", &i) == 1)
{
fputs("\nEnter the coefficient (a for a*x^b): ", stdout);
if (scanf("%f", &ptr1->coeff) != 1)
{
fputs("error: invalid integer input.\n", stderr);
return NULL;
}
fputs("Enter the exponent/power (b for a*x^b): ", stdout);
if (scanf("%d", &ptr1->exp) != 1)
{
fputs("error: invalid integer input.\n", stderr);
return NULL;
}
while (p1->exp == monomial->exp)
{

}

}
else
{
fputs("\nEnter the coefficient (a for a*x^b): ", stdout);
if (scanf("%f", &ptr1->coeff) != 1)
{
fputs("error: invalid integer input.\n", stderr);
return NULL;
}
fputs("Enter the exponent/power (b for a*x^b): ", stdout);
if (scanf("%d", &ptr1->exp) != 1)
{
fputs("error: invalid integer input.\n", stderr);
return NULL;
}
}
}
else
{
printf("Okay, then let's add those polynomials together!");
}
}
void display(char const *tag, struct node *ptr1)
{
if (!ptr1)
{
puts("(list-empty)");
return;
}

struct node *temp = ptr1;

printf("%s: ", tag);

while (temp)
{
printf("-->(%0.1f X %d)", temp->coeff, temp->exp);
temp = temp->next;
}

putchar('\n');
}

void del_list(struct node *list)
{
while (list)
{
struct node *victim = list;
list = list->next;
free(victim);
}
}```
Posted
Updated 9-May-21 6:06am
v3
Richard MacCutchan 9-May-21 9:12am

That is really a mathematical question, rather than a programming one. How would you do it if writing things on paper?
Margarit 9-May-21 9:17am

I actually kinda know the algorithm. I should be checking if the exponents are same, if yes then add the coefficients, if not just write them seperately. But I am don't know how to write it as a C code actually. That is my problem.
Richard MacCutchan 9-May-21 9:38am

Sorry, but it is not clear exactly what values you need to check, or where they are supposed to be stored.
Margarit 9-May-21 9:44am

Oh, you are right! I didn't want to upload all code because it is over 200 lines but I will try to get the important parts and improve my question. I would appreciate if you could check my code again and help me fix the problem.
Rick York 9-May-21 11:35am

I would write a function to display prompts and acquire the coefficient and exponent. Currently you have code for doing that in three places. You should make one function to do that and then you can call it where ever you need to.
Margarit 9-May-21 11:37am

Yeah I know, but I had other problems in my code and I just couldn't have time to do this I guess. I just copy-pasted same parts.
Rick York 9-May-21 13:39pm

I recently wrote code for a polynomial fitting algorithm and I used an array to hold the coefficients and the position in the array defined the exponent. This made the code very, very simple. I set a maximum of 11 for the order of the polynomial so my array had 12 slots. That could have been a dynamically allocated array but for my data I found eighth order polynomials were the most I have ever needed so a static array was adequate and twelve slots were more than enough.

## Solution 1

Adding to what Rick York said in comments, also make a separate function for each basic polynomial operation you want to support. One really useful one would be a `find_exponent(struct node *poly, int exponent, bool insert)` function that would search the (poly) linked list for a term with the given (exponent) and return a pointer to it if found. If there is no term, either insert a new term (with a zero coefficient) if the (insert) argument is true and return that pointer, or return NULL if (insert) is false. Another is a function `remove_term(struct node *poly, struct node *term)` that would unlink a given term from a polynomial and free it. Given those, you could then write a simple monomial add function with something like:
C
```struct node *add_monomial(struct node *poly, int exponent, float coeff)
{
struct node *term = find_exponent(poly, exponent, 1);
term->coeff += coeff;
if (term->coeff == 0.0) remove_term(poly, term);
return poly;
}
```

The idea here is to build a "toolbox" of basic functions you can use to get higher-level things done. Each function should do something useful, but be small enough that you can write and test it fairly quickly. You've already done just that with `del_list()`. These little utility functions make the "business" part of your code easier to write and easier to read.

Also, don't try to write them all at once. Write each one and test it separately, if you can. The first one I'd write is `print_poly(struct node *poly)`, tested with a couple of "hand-constructed" linked lists, and then start coding and testing the individual pieces needed to create a polynomial from console input. Once you can read and write, then start working on arithmetic.
Margarit 9-May-21 12:39pm

Thank you very much for your help!!