Thread: functions using arrays

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    60

    retry

    Sorry...i have all of the errors and warnings on that my
    teacher told us to use (-ansi, -pedantic,-errors,-Werror,-Wall)
    I too am making a calculator and I looked the other threads
    about calculators and mine is not that complicated because
    we haven't learned some of those things but anyway.
    we have to do ours in functions. i've wriiten these functions
    using arrays because the input is coming at a max of 20 for
    all functions accept the power function. I think my power
    function is wrong because it's not doing the right thing
    also i think my other functions are missing something. any ideas?
    thank you.


    Code:
    float power_func(float firsval, int seconval){                                                                                                 
      int count;                                                                                                                                   
      float powerval;                                                                                                                              
      if (firsval < 0){                                                                                                                            
        firsval = -1;                                                                                                                              
        seconval= 1/seconval;                                                                                                                      
      }                                                                                                                                            
      for(count = 0; count < firsval; count++){                                                                                                    
        powerval *= seconval;                                                                                                                      
      }                                                                                                                                            
      return powerval;                                                                                                                             
    }    
                                                                                                                                                   
    float add_arr(float adarry[]){                                                                                                                 
      int count;                                                                                                                                   
      float sum = adarry[0];                                                                                                                       
      for(count = 1; count < MAX; count++){                                                                                                        
        sum += adarry[count];                                                                                                                      
      }                                                                                                                                            
      return sum;                                                                                                                                  
    }                                                                                                                                              
                                                                                                                                                                                                                                                                                            
    float subtract_arr(float subarry[]){                                                                                                           
      int count;                                                                                                                                   
      float difference = subarry[0];                                                                                                               
      for(count = 1; count < MAX; count++){                                                                                                        
        difference -= subarry[count];                                                                                                              
      }                                                                                                                                            
      return difference;                                                                                                                           
    }                                                                                                                                              
                                                                                                                                                                                                                                                                                                  
    float multiply_arr(float multarry[]){                                                                                                          
      int count;                                                                                                                                   
      float product = multarry[0];                                                                                                                 
      for(count = 1; count < MAX; count++){                                                                                                        
        product *= multarry[count];                                                                                                                
      }                                                                                                                                            
      return product;                                                                                                                              
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    (1) In power_func(), it appears you have firsval and seconval's roles reversed. seconval would need to be the exponent, since it's declared to be an int.

    (2) powerval is never set to an initial value, so when you multiply by seconval, powerval becomes some unknown value.

    (3) It would be better if the array functions were passed a count of the number of items in the array, instead of using a global like MAX. That way it could operate on any size array, and also maybe the whole array isn't filled, but only part of the array.

    (4) Unless you were instructed to use floats, I would use doubles instead, as this is the type used by most functions in the C math library.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing arrays of pointers into functions
    By ashley in forum C Programming
    Replies: 5
    Last Post: 01-13-2007, 06:48 PM
  2. storage class, arrays, functions and good layout
    By disruptivetech in forum C Programming
    Replies: 4
    Last Post: 12-02-2005, 02:34 PM
  3. functions and arrays
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 03-14-2002, 09:57 AM
  4. passing multidimensional arrays to functions
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 11-17-2001, 03:27 AM
  5. elements of arrays; functions
    By sballew in forum C Programming
    Replies: 6
    Last Post: 09-03-2001, 01:48 AM