Thread: Expected expression... error

  1. #1
    Registered User
    Join Date
    Sep 2020
    Posts
    15

    Expected expression... error

    I've revised calc_abv function proposed by laseright and hodor by shifting the array of coefficients to main(). In this manner I can use the same code for the inverse function, calc abw for abv. Just different coefficients.


    It looks like it should work but I can't find the error at the end of main(): "error: expected expression before ')' token".
    A search tells me it's likely a naming/type problem or syntax error. By the message, I'd assume it's in main() but I'm not seeing it. What am I missing??
    Thanks.



    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    #define NUM_MIXTURE_PERCENTAGES 101
    #define MOLAR_MASS_WATER 18.0153   /*Grams per mol*/
    #define MOLAR_MASS_ETHANOL 46.0684 /*Grams per mol*/
    
    
    /*Function Prototype*/
    double convert(double abw_to_abv[], double abw); /*converts alcohol by weight %mass-%mass to %vol-%vol*/
    double calc_abw(double mol_mass_e, double mol_mass_w, double mol_frac1, double mol_frac2);/*converts from mol% to %mass, mol fractions of x or mol fractions of y*/
    
    
    int main()
    {
    int index = 0;
    double x1 = 0;          /*x1 is liquid molar fraction of ethanol*/
    double x2 = 1-x1;         /*x2 is liquid molar fraction of water*/
    double boiler_abv = 0;
    double abw = 0;
    
    
    double abw_to_abv[9] = /*Array of E Croissants coefficients for conversion of alcohol by mass to alcohol by volume*/
    {
    -0.000039705486746795932, 1.2709666849144778, -0.40926819348115739, 2.0463351302912738,
    -7.8964816507513707, 15.009692673927390, -15.765836469736477, 8.8142267038252680, -2.0695760421183493
    };
       for (int index = 0; index < NUM_MIXTURE_PERCENTAGES; index++)
       {
       abw = calc_abw(MOLAR_MASS_ETHANOL, MOLAR_MASS_WATER, x1, x2);
    
    
       boiler_abv = convert(abw_to_abv, abw);
    
    
       printf("\nfraction       \tabw         \tboiler_abv\n");
       printf("%1.2f      %1.2f      %1.2f\n",
                x1,      abw,      boiler_abv);
    
    
       x1 += 0.01;
       x2 += 0.01;
       }
    return();
    }
    /********************************FUNCTIONS******************************/
    double calc_abw(double mm_e, double mm_w, double mf1, double mf2)
    {
    double result = 0;
    result = mf1/(mf1 + mm_w/mm_e * mf2);
    return (result);
    }
    
    
    /**********************************************************************/
    /*Calculates alcohol by volume from alcohol by weight. Source: On the Conversion of Ethanol by Edwin Croissant 02-14-2016 */
    /*Result between 0 and 1. Valid only for input between 0 and 1 and valid only at 20 degrees C*/
    /*Initial polynomial y = a + b*x + c*x^2 + d*x^3 + f*x^4 + g*x^5 + h*x^6 + i*x^7 + j*x^8*/
    
    
    double convert(double abw_to_abv[], double abw)
    {
    double abv = 0;
    
    
    abv = abw_to_abv[0];
        double abw_mult = abw;
        for (size_t i = 1; i < sizeof(abw_to_abv) / sizeof(abw_to_abv[0]); ++i)
    
    
        {
            abv = abv + abw_to_abv[i] * abw_mult;
            abw_mult *= abw;
        }
        return abv;
    }

  2. #2
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    193
    Quote Originally Posted by Buckeye Bing View Post
    I've revised calc_abv function proposed by laseright and hodor by shifting the array of coefficients to main(). In this manner I can use the same code for the inverse function, calc abw for abv. Just different coefficients.


    It looks like it should work but I can't find the error at the end of main(): "error: expected expression before ')' token".
    A search tells me it's likely a naming/type problem or syntax error. By the message, I'd assume it's in main() but I'm not seeing it. What am I missing??
    Thanks.



    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    #define NUM_MIXTURE_PERCENTAGES 101
    #define MOLAR_MASS_WATER 18.0153   /*Grams per mol*/
    #define MOLAR_MASS_ETHANOL 46.0684 /*Grams per mol*/
    
    
    /*Function Prototype*/
    double convert(double abw_to_abv[], double abw); /*converts alcohol by weight %mass-%mass to %vol-%vol*/
    double calc_abw(double mol_mass_e, double mol_mass_w, double mol_frac1, double mol_frac2);/*converts from mol% to %mass, mol fractions of x or mol fractions of y*/
    
    
    int main()
    {
    int index = 0;
    double x1 = 0;          /*x1 is liquid molar fraction of ethanol*/
    double x2 = 1-x1;         /*x2 is liquid molar fraction of water*/
    double boiler_abv = 0;
    double abw = 0;
    
    
    double abw_to_abv[9] = /*Array of E Croissants coefficients for conversion of alcohol by mass to alcohol by volume*/
    {
    -0.000039705486746795932, 1.2709666849144778, -0.40926819348115739, 2.0463351302912738,
    -7.8964816507513707, 15.009692673927390, -15.765836469736477, 8.8142267038252680, -2.0695760421183493
    };
       for (int index = 0; index < NUM_MIXTURE_PERCENTAGES; index++)
       {
       abw = calc_abw(MOLAR_MASS_ETHANOL, MOLAR_MASS_WATER, x1, x2);
    
    
       boiler_abv = convert(abw_to_abv, abw);
    
    
       printf("\nfraction       \tabw         \tboiler_abv\n");
       printf("%1.2f      %1.2f      %1.2f\n",
                x1,      abw,      boiler_abv);
    
    
       x1 += 0.01;
       x2 += 0.01;
       }
    return();
    }
    /********************************FUNCTIONS******************************/
    double calc_abw(double mm_e, double mm_w, double mf1, double mf2)
    {
    double result = 0;
    result = mf1/(mf1 + mm_w/mm_e * mf2);
    return (result);
    }
    
    
    /**********************************************************************/
    /*Calculates alcohol by volume from alcohol by weight. Source: On the Conversion of Ethanol by Edwin Croissant 02-14-2016 */
    /*Result between 0 and 1. Valid only for input between 0 and 1 and valid only at 20 degrees C*/
    /*Initial polynomial y = a + b*x + c*x^2 + d*x^3 + f*x^4 + g*x^5 + h*x^6 + i*x^7 + j*x^8*/
    
    
    double convert(double abw_to_abv[], double abw)
    {
    double abv = 0;
    
    
    abv = abw_to_abv[0];
        double abw_mult = abw;
        for (size_t i = 1; i < sizeof(abw_to_abv) / sizeof(abw_to_abv[0]); ++i)
    
    
        {
            abv = abv + abw_to_abv[i] * abw_mult;
            abw_mult *= abw;
        }
        return abv;
    }
    Your return statement is malformed. The main() function should return 0 on success or nonzero (usually 1) on error.

    Also pay attention to warnings. The expression

    Code:
    sizeof(abw_to_abv) / sizeof(abw_to_abv[0])
    won't do what you want because an array "decays" to a pointer when passed as an argument to a function. In other words, you've effectively written

    Code:
    sizeof(double*) / sizeof(double)
    You'll need to pass along the size of the array to convert().

  3. #3
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    193
    You could also simplify things quite a bit. Most of the arguments to your functions are actually just constants, so they could really be consolidated inside those functions:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    #define NUM_MIXTURE_PERCENTAGES 101
    #define MOLAR_MASS_WATER 18.0153   /*Grams per mol*/
    #define MOLAR_MASS_ETHANOL 46.0684 /*Grams per mol*/
    
    /*converts from mol% to %mass, mol fractions of x or mol fractions of y*/
    double calc_abw(double mf1, double mf2)
    {
     const double mm_e = MOLAR_MASS_WATER;
     const double mm_w = MOLAR_MASS_ETHANOL;
     const double ratio = mm_w/mm_e;
     return mf1/(mf1 + ratio * mf2);
    }
    
    /*Calculates alcohol by volume from alcohol by weight. Source: On the Conversion of Ethanol by Edwin Croissant 02-14-2016 */
    /*Result between 0 and 1. Valid only for input between 0 and 1 and valid only at 20 degrees C*/
    /*Initial polynomial y = a + b*x + c*x^2 + d*x^3 + f*x^4 + g*x^5 + h*x^6 + i*x^7 + j*x^8*/
    double convert(double abw)
    {
     static const double abw_to_abv[9] = /*Array of E Croissants coefficients for conversion of alcohol by mass to alcohol by volume*/
     {
      -0.000039705486746795932, 
      1.2709666849144778, 
      -0.40926819348115739, 
      2.0463351302912738,
      -7.8964816507513707, 
      15.009692673927390, 
      -15.765836469736477, 
      8.8142267038252680, 
      -2.0695760421183493
     };
     static const size_t length = sizeof(abw_to_abv) / sizeof(abw_to_abv[0]);
     double abv = abw_to_abv[0];
     double abw_mult = abw;
     for (size_t i = 1; i < length; ++i)
     {
      abv = abv + abw_to_abv[i] * abw_mult;
      abw_mult *= abw;
     }
     return abv;
    }
    
    int main()
    {
     int index = 0;
     double x1 = 0;          /*x1 is liquid molar fraction of ethanol*/
     double x2 = 1-x1;         /*x2 is liquid molar fraction of water*/
     double boiler_abv = 0;
     double abw = 0;
     for (int index = 0; index < NUM_MIXTURE_PERCENTAGES; index++)
     {
      abw = calc_abw(x1, x2);
      boiler_abv = convert(abw);
    
      printf("\nfraction       \tabw         \tboiler_abv\n");
      printf("%1.2f      %1.2f      %1.2f\n", 
              x1,         abw,      boiler_abv);
    
      x1 += 0.01;
      x2 += 0.01;
     }
     return 0;
    }

  4. #4
    Registered User
    Join Date
    Sep 2020
    Posts
    15
    Code:
    Your return statement is malformed.

    Think I'd know better by now.

    Also a bit chagrined I've omitted some of the keywords handed to me on a platter.

    Your other corrections and comments are very helpful to continued learning and I'm very appreciative of them. This stuff can be a little humbling.

    Thank you.

  5. #5
    Registered User
    Join Date
    Sep 2020
    Posts
    15
    I'm misinterpreting something. Getting errors I don't understand. Doesn't help that I'm still new to arrays/pointers. Punching above my weight, but trying to learn.


    Goal is to pass the array of coefficients, along with another argument to the convert function.


    Prototype:
    Code:
    double convert(double abw_to_abv[9], double abw);

    Array declaration/initialize in main():
    Code:
    static const double abw_to_abv[9] = /*Array of E Croissants coefficients for conversion of alcohol by mass to alcohol by volume*/
    {
    -0.000039705486746795932, 1.2709666849144778, -0.40926819348115739, 2.0463351302912738,
    -7.8964816507513707, 15.009692673927390, -15.765836469736477, 8.8142267038252680, -2.0695760421183493
    };

    Function calls:
    Code:
    boiler_abv[index] = convert(abw_to_abv[9], boiler_abw);
    vapor_abv[index] = convert(abw_to_abv[9], vapor_abw);
    Definition:
    Code:
    double convert(double abw_to_abv[9], double abw)
    
     static const size_t length = sizeof(abw_to_abv) / sizeof(abw_to_abv[0]);
     double abv = abw_to_abv[0];
     double abw_mult = abw;
     for (size_t i = 1; i < length; ++i)
     {
      abv = abv + abw_to_abv[i] * abw_mult;
      abw_mult *= abw;
     }
     return abv;

    Thanks for help/advice.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,079
    Firstly, you should be aware that this:
    Code:
    double convert(double abw_to_abv[9], double abw);
    is equivalent to:
    Code:
    double convert(double abw_to_abv[], double abw);
    which is equivalent to:
    Code:
    double convert(double *abw_to_abv, double abw);
    That is, the parameter abw_to_abv is a pointer to double, but we might use the first two versions to indicate that we expect this pointer to double to point to the first element of an array of doubles, or even more specifically to the first element of an array of 9 doubles. But neither of these conditions are enforced by the type system.

    Next, you should be aware that this:
    Code:
    boiler_abv[index] = convert(abw_to_abv[9], boiler_abw);
    accesses abw_to_abv out of bounds. abw_to_abv is an array of 9 doubles, so abw_to_abv[9] doesn't exist. What you probably wanted to write is:
    Code:
    boiler_abv[index] = convert(abw_to_abv, boiler_abw);
    the reason is that when passed as an argument, an array is converted to a pointer to its first element, so the array of 9 doubles named abw_to_abv is converted to double*, which is exactly the type of the abw_to_abv parameter of convert. Perfect.

    Well, almost. Next, let's take a look at this line in the definition of convert:
    Code:
    static const size_t length = sizeof(abw_to_abv) / sizeof(abw_to_abv[0]);
    Now, if abw_to_abv were an array, this code would work as expected. However, the parameter named abw_to_abv is a pointer to double, so sizeof(abw_to_abv) results in the size of a pointer to double. Clearly, this is independent of how many elements there are in the underlying array.

    Therefore, what I would do is to add a parameter to convert:
    Code:
    double convert(double abw_to_abv[], size_t abw_to_abv_len, double abw);
    Now, you can call it like this:
    Code:
    boiler_abv[index] = convert(abw_to_abv, sizeof(abw_to_abv) / sizeof(abw_to_abv[0]), boiler_abw);
    So, you still have the same "divide sizeof the array by sizeof an element" pattern, but this time it is placed where abw_to_abv really is an array, not a pointer used as if it were an array.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Sep 2020
    Posts
    15

    Success

    Thank you for the detailed lesson. All seems so simple now. Also understand clearly now how sizeof is used to determine array length and size_t is used as a for loop variable. Good learning there.

    For a bit I was pulling my hair out; compile with errors- lots of errors- what can I be doing wrong- over and over. Then I put the curly brackets in the convert function...

    Much thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 07-26-2019, 04:22 AM
  2. Expected expression error
    By Peter Barnett in forum C Programming
    Replies: 3
    Last Post: 09-10-2013, 11:16 AM
  3. error: expected an expression
    By cdmstr in forum C++ Programming
    Replies: 3
    Last Post: 02-12-2011, 02:00 PM
  4. Compiling error expected expression.
    By tasmod in forum C Programming
    Replies: 10
    Last Post: 08-12-2010, 04:26 PM
  5. Error: expected an expression!
    By bobthebullet990 in forum C Programming
    Replies: 2
    Last Post: 01-05-2007, 09:05 AM

Tags for this Thread