Thread: functions using arrays

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    60

    functions using arrays

    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 because it seems to easy. 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
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I have a large widescreen monitor set the maximum resolution and I still can't read your post without scrolling. Fix it please!
    Hilight the text and you'll see the problem. Why the hell are there so many spaces after the text on every line?

    Turn on your compiler warnings and pay attention to them. They should get you most of the way to fixing this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    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;                                                                                                                              
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    trprince, FIX YOUR EDITOR.
    It's pointlessly padding every single line with trailing spaces out to 144 characters.
    That's why your code triggers horizontal scroll bars.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    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.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    sorry once again but i don't know how to fix my editor. this doesn't happen at my computer at home. if you tell me how then i will

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by trprince View Post
    sorry once again but i don't know how to fix my editor. this doesn't happen at my computer at home. if you tell me how then i will
    What exactly do you do to copy'n'paste the text into the page here. Are you by any chance copying and pasting out of a command prompt window? In that case, perhaps you could copy'n'paste through for example notepad instead - copying from command prompt will make the entire line of spaces that are "after the end of your text" be spaces - which makes the text REALLY WIDE.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    does this way work to do the power_func?
    and for the others is sizearr better to use then the global MAX?
    Do i have that correct?
    THanks


    Code:
    float power_func(float firsval, int seconval){                                                                                                 
      int count;                                                                                                                                   
      float powerval =0.0;                                                                                                                              
      if (seconval < 0){                                                                                                                            
        firsval = -1;                                                                                                                              
        seconval= 1/seconval;                                                                                                                      
      }                                                                                                                                            
      for(count = 0; count < seconval; count++){                                                                                                    
        powerval *= firsval;                                                                                                                      
      }                                                                                                                                            
      return powerval;                                                                                                                             
    }    
                                                                                                                                                   
    float add_arr(float adarry[]){                                                                                                                 
      int count, sizearr;                                                                                                                                   
      float sum = adarry[0];                                                                                                                       
      for(count = 1; count < sizearr; count++){                                                                                                        
        sum += adarry[count];                                                                                                                      
      }                                                                                                                                            
      return sum;                                                                                                                                  
    }

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    oh ok..i see..i will fix it

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > this doesn't happen at my computer at home.
    So you're not at home now, but you're copy/pasting from some ssh terminal window perhaps?

    See matsp's answer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > firsval = -1;
    I don't think you want to make firsval equal to -1. If you do that, you've lost the value you want raised to the power.

    >and for the others is sizearr better to use then the global MAX?
    Don't you think sizearr should be something passed to the function? So the caller can do:
    Code:
    sum = add_arr(array, num_elements);

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by trprince View Post
    does this way work to do the power_func?
    and for the others is sizearr better to use then the global MAX?
    Do i have that correct?
    THanks


    Code:
    float power_func(float firsval, int seconval){
      int count;
      float powerval =0.0;
      if (seconval < 0){
        firsval = -1;
        seconval= 1/seconval;
      }
      for(count = 0; count < seconval; count++){
        powerval *= firsval;
      }
      return powerval;
    }
    
    float add_arr(float adarry[]){
      int count, sizearr;
      float sum = adarry[0];
      for(count = 1; count < sizearr; count++){
        sum += adarry[count];
      }
      return sum;
    }
    Ehm, the sizearr should be an argument to the function. As it is now, it's a "random" number based on what happens to be in that location of memory - which is most likely not the size of your array.

    You set powerval to zero, then multiply it. What's zero multiplied by something?

    Seconval is an integer. "Inverting" that by 1/x is leading to zero if abs(x) != 1 - since it's an integer divide. I agree with the other poster that said "you've got your values swapped"

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Oct 2007
    Posts
    60

    fixed editor

    ok so i think i may have fixed it. here is the functions that I am working on

    Code:
    void fill_arr(float farry[], int *size){
         int fills = 0;
         while (fills < *size && scanf(“%f”, &farry) !=EOF)
              fills ++;
              *size = fills;
          }
      }
    
    float ascend_arr(float aarry[]){    
      int ascend, sizearr; 
      float order; 
      int next = aarry[0];
      for(ascend = 1; ascend < sizearr; ascend ++){   
        if (next < aary[ascend]) 
          order = 1; 
        else  
          order = -1; 
      } 
      return (order);                                                                                                                              
    } 
    
    float descend_arr(float darry[]){ 
      int descend, sizearr;  
      float order; 
      int next = darry[0];  
      for(descend = 1; descend < sizearr; descend ++){ 
      if (next > darry[descend]) 
          order = 1; 
        else 
          order = -1; 
      } 
      return (order);  
    }    
    
    float power_func(float firsval, int seconval){  
      int count;  
      float powerval =0.0; 
      if (seconval < 0){ 
        firsval = -1;  
        seconval= 1/seconval;  
      } 
      for(count = 0; count < seconval; count++){  
        powerval *= firsval;  
      }  
      return powerval; 
    } 
       
    float add_arr(float adarry[]){    
      int count, sizearr;      
      float sum = adarry[0];    
      for(count = 1; count < sizearr; count++){ 
        sum += adarry[count];   
      }  
      return sum; 
    } 
    
    float subtract_arr(float subarry[]){
      int count, sizearr; 
      float difference = subarry[0]; 
      for(count = 1; count < sizearr; count++){ 
        difference -= subarry[count]; 
      } 
      return difference;   
    } 
    
    float multiply_arr(float multarry[]){ 
      int count, sizearr; 
      float product = multarry[0];   
      for(count = 1; count < sizearr; count++){ 
        product *= multarry[count]; 
      } 
      return product; 
    } 
    
    float division_arr(float divarry[]){
       int count, size;
       float quotient = divarry[0];
       for(count = 1; count < size; count++){
             quotient /= divarry[count];
       }
       return quotient;
    }

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That's much better - no spaces at end of line.

    Is this a "find five faults competition" ? [That is a joke, I know it's definitely not easy as a beginner - which is why I'm posting some comments to help you fix it up]

    Code:
    void fill_arr(float farry[], int *size){
         int fills = 0;
         while (fills < *size && scanf(“%f”, &farry) !=EOF)
              fills ++;
              *size = fills;
          }
      }
    You don't really want to update *size inside the loop and use it as a loop limit at the same time. I'd update *size when the loop is finished.

    Code:
    float ascend_arr(float aarry[]){    
      int ascend, sizearr; 
      float order; 
      int next = aarry[0];
      for(ascend = 1; ascend < sizearr; ascend ++){   
        if (next < aary[ascend]) 
          order = 1; 
        else  
          order = -1; 
      } 
      return (order);                                                                                                                              
    }
    As per my previous post: sizearr should be passed in, not a local variable - as it stands, it's going to crash your program, most likely - sooner or later at least.
    Different name to the parameter into your function

    I'm not entirely sure what the purpose of this function is - it returns 1 or -1 depending on the first and last entry.

    Code:
    float descend_arr(float darry[]){ 
      int descend, sizearr;  
      float order; 
      int next = darry[0];  
      for(descend = 1; descend < sizearr; descend ++){ 
      if (next > darry[descend]) 
          order = 1; 
        else 
          order = -1; 
      } 
      return (order);  
    }
    Same functionality as above - just different sign - so perhaps return "-ascend()"??

    Code:
    float power_func(float firsval, int seconval){  
      int count;  
      float powerval =0.0; 
      if (seconval < 0){ 
        firsval = -1;  
        seconval= 1/seconval;  
      } 
      for(count = 0; count < seconval; count++){  
        powerval *= firsval;  
      }  
      return powerval; 
    }
    I've already done this one in a previous post - please review that part.

    Code:
    float add_arr(float adarry[]){    
      int count, sizearr;      
      float sum = adarry[0];    
      for(count = 1; count < sizearr; count++){ 
        sum += adarry[count];   
      }  
      return sum; 
    } 
    
    float subtract_arr(float subarry[]){
      int count, sizearr; 
      float difference = subarry[0]; 
      for(count = 1; count < sizearr; count++){ 
        difference -= subarry[count]; 
      } 
      return difference;   
    } 
    
    float multiply_arr(float multarry[]){ 
      int count, sizearr; 
      float product = multarry[0];   
      for(count = 1; count < sizearr; count++){ 
        product *= multarry[count]; 
      } 
      return product; 
    } 
    
    float division_arr(float divarry[]){
       int count, size;
       float quotient = divarry[0];
       for(count = 1; count < size; count++){
             quotient /= divarry[count];
       }
       return quotient;
    }
    These all have the same problem with a "uninitialized local sizearr" - and you should really use the same name for same use variables, don't call it size in one place and sizearr in all the others - consistency is a VERY good thing in programming - it just makes everyones life much easier [and no, I don't always achieve it either].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Registered User
    Join Date
    Oct 2007
    Posts
    60

    i'm trying

    ok...thankeveryone again for helping me..I'm so slow at this and trying to undersand what yall are saying. ok so here is the edited code. The ascend function returns 1 if it does ascend and -1 if it does not. the same is for the descend.
    Code:
    void fill_arr(float farry[], int *size){
         int fills = 0;
         while (fills < *size && scanf(“%f”, &farry) !=EOF)
              fills ++;
          } 
          *size = fills;
      }
    
    float ascend_arr(float aarry[], int sizearr){    
      int ascend; 
      float order; 
      int next = aarry[0];
      for(ascend = 1; ascend < sizearr; ascend ++){   
        if (next < aarry[ascend]) 
          order = 1; 
        else  
          order = -1; 
      } 
      return (order);                                                                                                                              
    } 
    
    float descend_arr(float darry[], int sizearr){ 
      int descend;  
      float order; 
      int next = darry[0];  
      for(descend = 1; descend < sizearr; descend ++){ 
      if (next > darry[descend]) 
          order = 1; 
        else 
          order = -1; 
      } 
      return (order);  
    }    
    
    float power_func(float firsval, int seconval){  
      int count;  
      float powerval =1.0; 
      if (seconval < 0){ 
        firsval = 1.0;  
        seconval= 1/firsval;  
      } 
      for(count = 0; count < seconval; count++){  
        powerval *= firsval;  
      }  
      return powerval; 
    } 
       
    float add_arr(float adarry[], int sizearr){    
      int count;      
      float sum = adarry[0];    
      for(count = 1; count < sizearr; count++){ 
        sum += adarry[count];   
      }  
      return sum; 
    } 
    
    float subtract_arr(float subarry[], int sizearr){
      int count; 
      float difference = subarry[0]; 
      for(count = 1; count < sizearr; count++){ 
        difference -= subarry[count]; 
      } 
      return difference;   
    } 
    
    float multiply_arr(float multarry[], int sizearr){ 
      int count; 
      float product = multarry[0];   
      for(count = 1; count < sizearr; count++){ 
        product *= multarry[count]; 
      } 
      return product; 
    } 
    
    float division_arr(float divarry[], int sizearr){
       int count;
       float quotient = divarry[0];
       for(count = 1; count < sizearr; count++){
             quotient /= divarry[count];
       }
       return quotient;
    }

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