• C

Assert statement in a Polynomial Term program.

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;

}
PaladinxyzAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PaladinxyzAuthor Commented:
Here's the additional boolean.h file in case it's needed.

#ifndef BOOLEAN_H
#define BOOLEAN_H
typedef enum{ FALSE, TRUE } Boolean;
#endif
0
Jaime OlivaresSoftware ArchitectCommented:
Problem is in your scan string in readPolyterm. It must be:

int readPolyterm( FILE* f, Polyterm p )
{
   if ( fscanf(f, "%lf %i", &(p->coef), &(p->expon)) == EOF )
      return EOF;
   else
       return 1;
}

Also, I think your displayPolyterm function must be:

void displayPolyterm( FILE* f, Polyterm p )
{
   fprintf(f, "Coef = %f, Expon = %i\n", p->coef, p->expon);
}

Good luck,
Jaime.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PaladinxyzAuthor Commented:
Thanks alot, and I have gotten the assert statement to work. Have a nice Sunday.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.