If the polynomials are not very sparse, we can represent them using arrays. Where index is the exponent and value is the coefficient

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

typedef struct

{

int* arr;

int maxExponent;

}Polynomial;

Polynomial add(Polynomial a, Polynomial b)

{

Polynomial c;

Polynomial *min, *max;

if(a.maxExponent > b.maxExponent)

{

max = &a;

min = &b;

}

else

{

min = &a;

max = &b;

}

c.arr = calloc(max->maxExponent + 1, sizeof(int));

c.maxExponent = max->maxExponent;

memcpy(c.arr, max->arr, (max->maxExponent + 1) * sizeof(int));

int i;

for(i = 0; i <= min->maxExponent; ++i)

{

c.arr[i] += min->arr[i];

}

return c;

}

int main()

{

int a[] = {10, 30, 40, 50, 5};

int b[] = {20, 40, 10, 4};

Polynomial a1 = {a, 4};

Polynomial a2 = {b, 3};

Polynomial c = add(a1, a2);

int i;

for(i = 0; i <= c.maxExponent; i++)

printf("%d ", c.arr[i]);

printf("\n");

}

Advertisements
(function(g,$){if("undefined"!=typeof g.__ATA){
g.__ATA.initAd({collapseEmpty:'after', sectionId:26942, width:300, height:250});
g.__ATA.initAd({collapseEmpty:'after', sectionId:114160, width:300, height:250});
}})(window,jQuery);
var o = document.getElementById('crt-1012970944');
if ("undefined"!=typeof Criteo) {
var p = o.parentNode;
p.style.setProperty('display', 'inline-block', 'important');
o.style.setProperty('display', 'block', 'important');
Criteo.DisplayAcceptableAdIfAdblocked({zoneid:388248,containerid:"crt-1012970944",collapseContainerIfNotAdblocked:true,"callifnotadblocked": function () {var o = document.getElementById('crt-1012970944'); o.style.setProperty('display','none','important');o.style.setProperty('visbility','hidden','important'); } });
} else {
o.style.setProperty('display', 'none', 'important');
o.style.setProperty('visibility', 'hidden', 'important');
}
var o = document.getElementById('crt-1663068278');
if ("undefined"!=typeof Criteo) {
var p = o.parentNode;
p.style.setProperty('display', 'inline-block', 'important');
o.style.setProperty('display', 'block', 'important');
Criteo.DisplayAcceptableAdIfAdblocked({zoneid:837497,containerid:"crt-1663068278",collapseContainerIfNotAdblocked:true,"callifnotadblocked": function () {var o = document.getElementById('crt-1663068278'); o.style.setProperty('display','none','important');o.style.setProperty('visbility','hidden','important'); } });
} else {
o.style.setProperty('display', 'none', 'important');
o.style.setProperty('visibility', 'hidden', 'important');
}