Menu

Please Help: 'invalid lvalue in assignment'

2007-08-13
2012-09-26
  • Nobody/Anonymous

    Hi guys
    I am a univeristy student taking Music Technology, which for some reason includes a module on C Programming. I am currently working through some problems as a re-sit piece of coursework; the one I am stuck on is asking me to write a program to read in an integer and a floating point number, and send them to a function which calculates the floating point no raised to the power of the integer. I have the bare bones of the program written, but am having problems when I try to compile it. I am receiving error messages reading 'invalid lvalue in assignment', referring to the lines on which I have assigned values to storage locations. Please see below for the program of the function itself.

    float raise_float (float base, int power)
    {
    int count;

      if (power == 0)
         raise_float = 1;
      else if (power == 1)
         raise_float = base;
    
      else 
      raise_float = 999;
    
      return raise_float;
    

    }

    I believe I also read in the guidelines to copy in my compile log? please see below for this.

    C:\Documents and Settings\Chris Jordan\Desktop\C-prog Resit Coursework\6. Powers (incomplete).c: In function `raise_float':
    C:\Documents and Settings\Chris Jordan\Desktop\C-prog Resit Coursework\6. Powers (incomplete).c:28: error: invalid lvalue in assignment
    C:\Documents and Settings\Chris Jordan\Desktop\C-prog Resit Coursework\6. Powers (incomplete).c:30: error: invalid lvalue in assignment
    C:\Documents and Settings\Chris Jordan\Desktop\C-prog Resit Coursework\6. Powers (incomplete).c:33: error: invalid lvalue in assignment
    C:\Documents and Settings\Chris Jordan\Desktop\C-prog Resit Coursework\6. Powers (incomplete).c:35: error: incompatible types in return

    Execution terminated

    If anybody could help me at all with this; even just telling me what the error message means; it would be incredibly appreciated! many thanks, Chris

     
    • Anonymous

      Anonymous - 2007-08-17

      They are variables. Here 'i' is used as a loop counter, and 'x' is used to accumulate and ultimately return the value of the function. I imagine where he has used 'i' you intended to use 'count' which is unused in your code.

      The respondent has made a (probably fair) assumption about what your code was supposed to do and completed it for you. If this was your homework assignment perhaps he should have not been quite so "helpful". He has also used a 'nasty' practice of having more that one return in a function. The correction to your code (without any such assumptions) would look like this:

      float raise_float (float base, int power)
      {
      float result ;

      if (power == 0)
      {
      result = 1.0f ;
      }
      else if (power == 1)
      {
      result = base ;
      }
      else
      {
      result = 999.0f;
      }

      return result ;
      }

      It would be better her to use a switch:

      float raise_float (float base, int power)
      {
      float result ;

      switch( power )
      {
      case 0 :
      result = 1.0f ;
      break ;

      case 1 :
      result = base ;
      break ;

      default :
      result = 999.0f ;
      break ;
      }

      return result ;
      }

      The switch version is only possible because 'power' is an integer - this is not the general case in mathematics where an exponent (the correct term) is a real number (a floating point value being a machine approximation of a real number).

      Note that there is a function in the standard library that performs this function far more efficiently and with real exponents using hardware FPU operations requiring no iteration (so the calculation time is constant and not a function of the exponent).

      Clifford

       
    • Nobody/Anonymous

      You are trying to assign a value to the raise_float function. You need to define a float variable for raise_float and use it in the calculation. Return the variable, not the function name.

      lvalue is the left side value in an assignment. The statement
      raise_float = 1;
      is invalid because the variable raise_float is not defined (it is a function).

       
    • Nobody/Anonymous

      try this instead ...

      float raise_float (float base, int power)
      {

      if (power == 0) return 0;

      if (power == 1) return base;

      float x:=1.0;
      int i=0;

      if (power > 1 ) * power is positive /
      for( i=0; i<power; ++i) x = x
      base; / or shorter code is x = base; */

      else / power is negative /
      for( i=0; i<power; ++i) x = x/base; / or shorter code is x /= base; /

      return x;
      }

       
    • rattatouie

      rattatouie - 2007-08-16

      thankyou very much both of you; its starting to fall into place now. The example above looks like it should be useful, but can't work out what the 'x' and 'i' are for, and where I need to declare them? thanks again

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.