Solved

# Assert statement in a Polynomial Term program.

Posted on 2004-11-28

hi everyone, I've been writing a Polyterm (Polynomial Term) program that requires an assert statement in the addPolyterm function to insure each Polyterm's exponent to be exact the same before executing other codes. However, I'm having trouble with the addPolyterm function since whenever I run the program, although p1's exponent is the same as p2's, the program terminates... I have included my Polyterm.c, Polyterm.h, and TestPolyterm.c below.

In addition, can someone also check my displayPolyterm function of Polyterm.c since it's not working as it's supposed to according to the test program.

Thanks for any help.

************************************************

Polyterm.c

#include <stdio.h>

#include <stdlib.h>

#include <assert.h>

#include "Boolean.h"

#include "polyterm.h"

Polyterm newPolyterm( double c, int e )

{

Polyterm p = (Polyterm) malloc(sizeof(PolytermData));

p->coef = c;

p->expon = e;

return p;

}

Polyterm clonePolyterm( Polyterm p )

{

Polyterm copy = (Polyterm) malloc(sizeof(PolytermData));

copy->coef = p->coef;

copy->expon = p->expon;

return copy;

}

void freePolyterm( Polyterm p )

{

free(p);

}

double getCoef( Polyterm p )

{

return p->coef;

}

int getExpon( Polyterm p )

{

return p->expon;

}

int exponentCompare( Polyterm p1, Polyterm p2 )

{

if (p1->expon >= p2->expon)

return p1->expon;

else

return p2->expon;

}

/*

Use an assert in addPolyterm to be sure that your exponents agree.

If the assert crashes your program, you have a logic problem somewhere

in your Polynomial add algorithm.

*/

Polyterm addPolyterm( Polyterm p1, Polyterm p2 )

{

assert( (p1->expon) == (p2->expon) );

{

Polyterm sum = (Polyterm) malloc(sizeof(PolytermData));

sum->coef = p1->coef + p2->coef;

sum->expon = p1->expon;

return sum;

}

}

/*

Parameters are two different Polynomial terms. Multiply each term's

coefficient and add the exponents together.

*/

Polyterm mulPolyterm( Polyterm p1, Polyterm p2 )

{

Polyterm product = (Polyterm) malloc(sizeof(PolytermData));

product->coef = p1->coef * p2->coef;

product->expon = p1->expon + p2->expon;

return product;

}

/*

The readPolyterm function returns an int, which is either EOF or is

ignored. This is necessary since readPolyterm will be responsible for

detecting end of file when used to read in a Polynomial.

*/

int readPolyterm( FILE* f, Polyterm p )

{

if ( fscanf(f, "%ld, %d", &(p->coef), &(p->expon)) == EOF )

return EOF;

else

return 1;

}

/*

The display function now takes a FILE* parameter. Pass in stdout to

make it display on the screen.

*/

void displayPolyterm( FILE* f, Polyterm p )

{

fscanf(f, "%ld, %d", &(p->coef), &(p->expon));

printf("Coef = %ld, Expon = %d\n", (p->coef), (p->expon));

}

**********************************************************

Polyterm.h

#ifndef POLYTERM_H

#define POLYTERM_H

#include <stdio.h>

#include "Boolean.h"

/* =========================================

DEFINITIONS FOR A SINGLE POLYNOMIAL TERM

============================================ */

typedef struct PolytermData

{

double coef;

int expon;

} PolytermData;

typedef PolytermData* Polyterm;

Polyterm newPolyterm( double c, int e );

Polyterm clonePolyterm( Polyterm p );

void freePolyterm( Polyterm p );

double getCoef( Polyterm p );

int getExpon( Polyterm p );

int exponentCompare( Polyterm p1, Polyterm p2 );

Polyterm addPolyterm( Polyterm p1, Polyterm p2 );

Polyterm mulPolyterm( Polyterm p1, Polyterm p2 );

int readPolyterm( FILE* f, Polyterm p );

void displayPolyterm( FILE* f, Polyterm p );

#endif

*****************************************************

TestPolyterm.c

#include "polyterm.h"

#include <stdio.h>

#include <stdlib.h>

int main()

{

Polyterm p1,p2,p3,p4;

Boolean moreData;

int retValue; // to test for end of file detection */

p1 = newPolyterm( 2,3 );

printf("Enter coef, then expon = 3: ");

p2 = newPolyterm(0,0);

retValue = readPolyterm(stdin, p2);

p3 = addPolyterm( p1, p2 );

p4 = mulPolyterm( p1, p2 );

displayPolyterm(stdout, p1); printf("\n");

displayPolyterm(stdout, p2);printf("\n");

displayPolyterm(stdout,p3);printf("\n");

displayPolyterm(stdout,p4);printf("\n");

printf("\n");

/* check clone function */

printf("Cloning p2...\n");

freePolyterm( p1 );

p1 = clonePolyterm( p2 );

displayPolyterm(stdout,p1); printf("\n");

/* check end of file detection */

printf("Testing EOF detection\n");

printf("Enter coef then exponent on one line. Enter CTRL/Z to end.\n");

moreData = TRUE;

while (moreData)

{

p1 = newPolyterm(0,0);

retValue = readPolyterm(stdin, p1 );

if ( retValue != EOF )

{

printf("Got One " );

displayPolyterm(stdout, p1 );

printf("\n");

freePolyterm(p1);

}

else moreData = FALSE;

}

freePolyterm( p1 );

printf("\nThis should be a problem. P1 is freed...\n");

displayPolyterm(stdout,p1); printf("\n");

return EXIT_SUCCESS;

}