C: Making a polynomial function via command line arguments

164 Views Asked by At

For my Calculus class it has been asked frequently to find the average speed between two points to find the instantaneous speed, requiring me to do f(b) - f(a) / b - a. I want to do pass parameters to program (eventually loaded onto my Ti-84) to solve this for me quickly, ideally working like this: ./a.out 2 4 -1 , 2.9 2.9999 resulting in the polynomial 2x^2 + 4x - 1, which would be ran on both 2.9 and 2.9999. So far I have written most of the logic in this code, however I am having difficulty putting it into proper C.

#include<stdio.h>
int main(int argc, char *argv[])
{
        int comma; // Ex: ` ./a.out 1 2 3 , 4 5 ` comma is at index 3
        // Find out where comman is located
        for ( int i=1;i<argc; i++)
        {
                if (argv[i] == ','){
                        int comma = i;
                        break;
                }
        }

        printf("%d", comma);

        // PSUEDO-CODE START
        //
        // Problem: f(b) - f(a) / b - a
        //
        // Idea
        // ------------------------------------------------------------
        // ./a.out 7 2 3 , 2 5
        // // Right now argc is 6
        // // Right now comma is 4
        // polynomialOrder = argc - comma
        //
        // // Get bigger number for the / b - a part
        // firstNum  = argv[comma + 1]
        // secondNum = argv[comma + 2]
        // biggerNum = ( firstNum > secondNum ) ? firstNum : secondNum;
        // smallerNum = ( firstNum < secondNum ) ? firstNum : secondNum;
        //
        // ------------------------------------------------------------
        //
        // Psuedo-code:
        //
        // for  (int i=polynomialOrder; i >= 0; i--)
        // {
        //      if (i == 0){ function += argv[i]} // I don't want 3x^2 + 2x^1 + 4^0 , the
4^0 should be 4
        //      else{
        //              // Generate a function?
        //              polynomial += argv[i]^i;  // The += appends to the function
        //      }
        // }
        // // result is -7x^2 + 2x^1 + 1
        //
        // Function generated:
        // -------------------------------------------------------
        // float polynomial(a, b) {
        //      float result;
        //      float a_result = (-7*(a**2)) + (2*(a**1)) + 1;
        //      float b_result = same as ^, substitute in b
        //      float result = (b_result - a_result) / ( b - a);
        //      return return;
        // }
        // -------------------------------------------------------
        // }
        // -------------------------------------------------------
        // polynomial( smallerNum, biggerNum );

        return 0;
}
1

There are 1 best solutions below

0
On

At least these problems:

Wrong compare

',' is not a string and is an invalid argument to strcmp(). strcmp() returns 0 when strings are equal.

// if (strcmp(argv[i], ',')) // Check if arg is ','
if (strcmp(argv[i], ",") == 0) // Check if arg is ","

Local object

comma is only local to the block. This comma cease to exist after the break. Later polynomialOrder = argc - comma; has no comma to access.

{
   printf("Parameters to function");
   int comma = i; // All before are parts of function, after params
   break;
}

Invalid power code

// (-7*(a**2)) + (2*(a**1)) + 1;
-7*pow(a,2) + 2*pow(a,1) + 1;
// or 
-7*a*a + 2*a + 1;
// or numerical best stability as 
(-7*a + 2)*a + 1;

float vs. double

Use float when space is critical or speed is critical over precision/range. In C, best to use double as the default floating point type.

Post compiler warnings/errors

OP's code fails to compile. Post your errors, warnings, and detailed questions about remaining issues.

If all the code was meant to be pseudo-code, post that at the beginning of code.