简体   繁体   中英

How do I input an array to a C program on the the Mac terminal?

I'm on OSX 10.11 writing a pair of functions in C to compute the magnitude and angle of a sum of phasors in polar form and I'm having some trouble. The two main questions I have are:

1) How do I give two arrays as inputs (the magnitudes and the angles of the phasors I want to sum) to the function in the terminal?

2) How can I do this while being forced by C to have a main, argc, and argv?

Here is the code:

float PhasMag(float *coef,float *angle);
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argc, char *argv)
{
    if(argc != 2){
        printf("Incorrect number of arguments, please input an array of magnitudes and an array of angles, exiting\n");
        return -1;
    }
    return PhasMag(argv[1],argv[2]);
}

float PhasMag(float *coef,float *angle)
{
    float R = 0,Im = 0, Mag; //real and imag. parts of answer
    float *countM;
    int sc = 0 ,sa;
    countM = coef;
    while(countM != NULL){
        countM = countM + 1;
        sc = sc + 1;
    }
    sa = sc;
    for(int i = 0;i<sc;i++){//computes real part
        coef = coef + i; angle = angle + i;
        R = R + (*coef)*cos(*angle);
    }
    coef = coef - sc + 1; angle = angle - sa + 1;
    for(int k = 0;k<sa;k++){//computes imaginary part
        coef = coef + k; angle = angle + k;
        Im = Im + (*coef)*sin(*angle);
    }
    return sqrt(R*R+Im*Im);
}

What you get from the command line arguments (the stuff in argv[i]) are strings (char *). You will have to parse these as floats yourself in C.

If you wanted to have your program take two arrays as two separate inputs, you could specify it like this

 ./yourprogram '1.0 2.0 3.0 4.0' '5.0 6.0 7.2 8.1'

Note the quotes around the two arrays of numbers, that's important for the shell to pass these as two arguments to your program rather than as 8 individual floats.

You'll have to parse out the floats in each of those strings manually, splitting on space characters, and using a function like scanf or atof to parse a floating-point number from parts of a string.

Alternatively you could specify the command line interface to be

  ./yourprogram 5 a1 a2 a3 a4 a5 b1 b2 b3 b4 b5

And then in your program run through the 10 floating point values in argv. There's lots of ways to do it. I would personally have the program read from stdin in a format like "first line contains size of the arrays N, next line contains N floating-point values coeff's, and the next line contains N floating point values for angle's. Then you can more easily have other programs that generate inputs to this program for example.

HTH,

Command line arguments are passed as an array of pointers to "strings", actually as const char* argv[] . So what you get is always an array of strings, never one or even two arrays of something else, and also not a float.

I think you have three choices:

  1. surround the coef and angle arguments by " or ' , thereby packing a list strings into one string, respectively,
  2. don't pass the coefs and angles as parameters but store them in a file and pass the filename as parameter, or
  3. divide the (single) list of arguments into two groups, the coefs and the angles, and parse them as floats.

I'll just present option 3 here; If you prefer the other ones, don't hesitate to ask to get a hint or simply search the web;

Option 3 relies on an even amount of parameters, the first half denoting the coefs, the second half being the angles. Call it as follows:

myprg 1.0 2.0 43.0 180.0
--> 0: 1.000000/43.000000   1: 2.000000/180.000000
myprg
--> Error: please pass float values (an even amount) as parameters
myprg 1.0 2.0 43.0
--> Error: please pass float values (an even amount) as parameters
myprg 1.0 2.0 43.0 foo
--> Error parsing parameters; foo is not a valid float value

And here's the code:

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

float PhasMag(float *coef,float *angle, int n) {

    // your actual code goes here...
    for (int i=0; i<n; i++) {
        printf("%d: %f/%f\n",i, coef[i], angle[i]);
    }
    return 0.0;
}

int scanParams(float* result, int amount, const char* params[]) {
    for (int i=0; i<amount; i++) {
        if (sscanf(params[i], "%f", result) != 1) {
            printf("Error parsing parameters; %s is not a valid float value", params[i]);
            return 0;
        }
        result++;
    }
    return 1;
}


int main(int argc, const char *argv[])
{

    int nrOfArguments = argc -1;
    if (nrOfArguments %2 > 0 || nrOfArguments == 0) {
        printf("Error: please pass float values (an even amount) as parameters");
        return 1;
    }

    int sizeOfArray = nrOfArguments / 2;
    float coef[sizeOfArray];
    if (!scanParams(coef,sizeOfArray, argv+1))
        return 1;

    float angle[sizeOfArray];
    if (!scanParams(angle,sizeOfArray, argv+1+sizeOfArray))
        return 1;

    PhasMag(coef,angle,sizeOfArray);

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM