Search code examples
ccommand-linepolynomials

Read in command line arguments to determine a polynomial


I am using C language to read in command arguments of an unknown size and determine the coefficients of the polynomial, its range and the degree of the polynomial. I'm going to use the coefficients to reconstruct the polynomial and do numerical analysis on it but I am having issues just reading in the command line arguments.

For example;

./filename 1.4 2.2 3.3 4.45 5.65 12 14

where 1.4 2.2 3.3 4.45 5.65 are the coefficients of the polynomial and 12 and 14 is the range of polynomial.

I have been struggling with this for a bit now and was able to implement code which utilized fgets and then ran a for loop to count the number of spaces in the string to determine the deg of polynomial and the number of coefficients but this code utilized the terminal and I felt like that was the wrong approach.

I am sure this has something to do with pointers but I have always struggled to master this concept

I'm curious if what I need to do is run a for loop as follows

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

#define EPSILON 0.01

void main(int argc, char *argv[]){
    int i,count;
    float a,b,c;

    count =0;

    for(i=1;i<argc;i++){
    if(argc != '\0')
    count++;
    }

    deg = count - 2;

    b= argv[count];
    a= argv[count -1];

    for(i=1;i<=deg;i++){
    str[i] = argv[i];
   }

}

I'm pretty much dumbfounded at this point and any advice into the right direction would greatly be appreciated.


Solution

  • You need to take this step by step.

    First clearly define the format of the command line. For example, we can say that there is the program name (argv[0]), n coefficients, and two numbers, with the constraint that n > 0. Hence we have argc > 3 and n = argc - 3.

    The code needs to first check the command line and extract its content to variables of the appropriate types.

    At this point you are no longer working with strings. You might need to perform additional input validation.

    Finally you can process the input.

    void usage ()
    {
        fprintf (stderr, "usage: ...");
        exit (EXIT_FAILURE);
    }
    
    // do the actual work
    void run (double coeff[], int n, double range1, double range2)
    {
        int deg;
    
        if (n > 1) {
            deg = n - 1;
        }
        else if (coeff[0] != 0) {
            deg = 0;
        }
        else {
            // the degree of the 0 polynomial is not defined
            ...
        }
        ...
    }
    
    int main (int argc, char **argv)
    {
        // Process command line arguments
    
        if (argc <= 3) {
            usage ();
        }
    
        int n = argc - 3;
    
        // The coefficients are stored from right to left so that
        // coeff[i] is the coefficient of x^i
        double coeff[n];
    
        double range1, range2;
    
        // FIXME: use strtod instead of atof to detect errors
        for (int i = 0; i < n; i++) {
            coeff[n - i - 1] = atof (argv[i + 1]);
        }
        range1 = atof (argv[n + 1]);
        range2 = atof (argv[n + 2]);
    
        // At this point you work only with coeff, n, range1 and range2
    
        // Additional input validation
        if (range1 >= range2) {
            ...
        }
    
        // do the actual work
        run (coeff, n, range1, range2);
    
        return EXIT_SUCCESS;
    }