**CS19002 Programming and Data Structures Laboratory** |
**Spring 2010, Section 5** |

# Assignment 7

Repeat Assignment 6 with dynamic memory allocation.
To be precise, make the following changes to Assignment 6.

### Define structure with dynamic memory

Define the multi-variate polynomial data type as follows.

#define NVAR 6
typedef struct {
int nterm;
int (*term)[1+NVAR];
} mvpoly;

The motivation behind this declaration is that now we can allocate the
exact amount of memory just needed to store all the non-zero terms of a
polynomial.

Another possibility is to use the following declaration.

typedef struct {
int nterm;
int **term;
} mvpoly;

Choose any of the above two type definitions.

You may also use a declaration with `int *term[1+NVAR];`
but, in that case, you need to store the transpose of the term matrix,
and all the individual functions should be appropriately modified. So
avoid this representation (unless you are too adventurous).

### Print function

The function `polyprint()` would work exactly as in Assignment 6.

### Initialize function

Rewrite the function `setpoly()` so that exactly *n* terms
are allocated for storing the sum of *n* variables.

### Product function

Rewrite the function `polyprod()` as follows. First, allocate
memory sufficient to store `f.nterm * g.nterm` terms. This
is obviously the maximum possible number of terms in the product. After
the product is computed, the actual number of terms may be less than this
maximum possible count. So the memory should be reallocated to store
only the non-zero terms in the product.

### Main function

Set *f* and *g* to the sum of *n* variables.
Subsequently, in a loop, compute the product of *f* and
*g*, and store the product in a new polynomial variable
*h*. Then, free the dynamic memory associated with *g*,
and copy *h* to *g*. Print *g*.

After the loop is over, free the dynamic memory associated
with *f* and g.

Now that you use dynamic memory, you can go for larger numbers of
variables. Report the output of your program for *n* = 6
variables.

**Note:** You may modify this code.
You may also use your own code for Assignment 6. However, for this
assignment, you are not allowed to store zero terms.