Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Assert statement in a Polynomial Term program.

Posted on 2004-11-28
3
Medium Priority
?
318 Views
Last Modified: 2010-04-15
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;

}
0
Comment
Question by:Paladinxyz
  • 2
3 Comments
 

Author Comment

by:Paladinxyz
ID: 12689745
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
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 1000 total points
ID: 12690052
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
 

Author Comment

by:Paladinxyz
ID: 12692316
Thanks alot, and I have gotten the assert statement to work. Have a nice Sunday.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question