1

I am working on a project in a basic C class that involves reading polynomial equations in from a file, taking the derivatives, and outputting the derivatives back to a different file.

I am doing the parsing of the array that holds the input from the file in my first function (after main), and there is a large while loop that, in theory, should move through the array and basically figure out what everything in that array is, in terms of a polynomial. The goal is to put the coefficients and exponents into one of two arrays, one for positive exponents and one for negative exponents, where the element number would represent the exponent and the value of the coefficient inside of that array element.

The problem that we have run into is that for some reason, the exponent variable never gets assigned a value. We think that this is due to a possible logic error somewhere in the if statements, but we can't seem to determine what it is. Any help would be greatly appreciated.

#include <stdio.h>
#include <string.h>

void function1 (char *, double *, double *);
void function2 (char *, double *, double *);

int main(void)
{
     char input [40];
     double neg_part[11], pos_part[11];
     double cos[11], sin[11], tan[11];
     int i=0;
     for  (i;i<11;i++)
     {
         neg_part[i]=0;
         pos_part[i]=0;
     }

     FILE *ifp = fopen("functions.txt","r+"), *ofp = fopen("derive.txt","w");
     do
     {
         if (ifp)
             while (!feof(ifp))
             {
                  fgets(input, 40, ifp);
                  function1(&input, &neg_part, &pos_part);
                  function2 (&input, &neg_part, &pos_part);
             }

     }while(!feof(ifp));

}


void function1(char *inputptr, double neg_part[], double pos_part[])
{
    int exponent, i=0;
    double xcoef;
    while (*inputptr!='\n')
    {
        if (isdigit(*(inputptr)))
        {
            if (*(inputptr+1)==' '|| *(inputptr+1)=='-' || *(inputptr+1)=='+')
            {
                 xcoef= strtol(inputptr, &inputptr, 10);
                 exponent=0;
                 pos_part[exponent]=xcoef;
            }

            else if (*(inputptr+1)=='x')
            {
                xcoef= strtol(inputptr, &inputptr, 10);
                if (*(inputptr+1)== '^')
                {
                    inputptr+2;
                    exponent=strtol(inputptr, &inputptr, 10);
                }
                else if(*(inputptr+1)==' ' || *(inputptr+1)=='-' || *(inputptr+1)=='+')
                {
                    exponent=1;
                }
            }
        }

        if (!isdigit(*inputptr))
        {
            if (*inputptr=='x')
            {
                xcoef=1;
                if (*(inputptr+1)=='^')
                {
                    exponent= strtol(inputptr, &inputptr, 10);
                }
                else if (*(inputptr+1)=='+'  ||  *(inputptr+1)=='-'  ||  *(inputptr+1)==' ')
                    exponent= 0;
            }
        }
        if (exponent>=0)
        {
            pos_part[exponent]=xcoef;
        }
        else if(exponent<0)
        {
            exponent=exponent*(-1);
            neg_part[exponent]=xcoef;
        }

        i++;
    }

}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • [`while (!feof(file))` is always wrong](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong). And you should always check the actual input function — `fgets()` in this example. And a `do ... while` loop around an `if` that contains a `while (!feof(ifp))` loop is contorted and dangerous. If `ifp` is null, the output loop will likely crash, or give you an infinite loop. – Jonathan Leffler May 05 '15 at 02:16
  • so what would you suggest we use instead to control the loop other than what we already have? – Russell Davis May 05 '15 at 02:43
  • The logic should be approximately: `if (ifp) { while (fgets(input, sizeof(input), ifp) != 0) { …loop body… } }`. There are probably other problems too; the lack of incrementing `inputptr` is probably the main problem. But the double-loop is … shall we say 'unusual'? – Jonathan Leffler May 05 '15 at 02:58
  • i just now see what you're saying, and i totally agree that the double loop was ridiculous and i have no idea why that was there. As for the lack of incrementing of the pointer, when i add i (which is incremented) to it, it does not seem to readily fix the problem, so i truly have no idea at this point, and have moved on. – Russell Davis May 05 '15 at 03:54
  • Since we don't have an MCVE ([How to create a Minimal, Complete, and Verifiable Example?](http://stackoverflow.com/help/mcve)) or SSCCE ([Short, Self-Contained, Correct Example](http://sscce.org/)) to work with (no `function2()`, for example), there's not a lot we can do to help. I suppose we could remove the call to `function2()` and see whether we get the loop too. But we also need some sample data — the sample data that is causing your code to loop. – Jonathan Leffler May 05 '15 at 03:58
  • the file i am using currently looks like this: 2x^2+592x+4 4x^4+24x^2 13x+63X^4+942x^2 – Russell Davis May 05 '15 at 04:04

3 Answers3

1
while (*inputptr!='\n')

inputptr doesn't move. There are inputptr+2;.Did you mean inputptr+=2 to increment inputptr by 2 ?

  • `inputptr` does move as it is altered by `strtol()`. The logical error was in `inputptr+2` where the intent is to point past the `^` character. – alvits May 05 '15 at 02:13
0

You have a statement: inputptr+2;, which does nothing, logically. Perhaps you meant inputptr+=2, which increments the inputptr by 2?

Good luck. I want to see this code, looks great!

acenturyandabit
  • 1,188
  • 10
  • 24
  • i fixed this, but the loop still doesn't ever seem to reach the terminating case... so i'm guessing that's not the problem (at least not the only one) – Russell Davis May 05 '15 at 02:24
0

I would go line by line printing out "here" or some other statement to see where your code is actually reaching. If it never prints out "here" in any of the internal if statements, then probably your while condition isn't being met.

jacoballenwood
  • 2,787
  • 2
  • 24
  • 39