functions using arrays

This is a discussion on functions using arrays within the C Programming forums, part of the General Programming Boards category; The ascend function returns 1 if it does ascend and -1 if it does not. the same is for the ...

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The ascend function returns 1 if it does ascend and -1 if it does not. the same is for the descend.
    Not exactly - the function returns an undefined value for a size of 1, and a 1 or -1 if the LAST element of the array is "ascending" or "descending" in relation to the first element. But it says nothing at all meaningfull about the elements in between. So if you have 10 12 13 14 15 17 19 9, then it's not ascending according to your function, whilst 10 1 2 3 4 5 6 12 is - I don't think either is ascending properly - but maybe I don't understand what ascending and descending arrays are correctly - I never studied math at a higher level.


    Code:
     if (seconval < 0){ 
        firsval = 1.0;  
        seconval= 1/firsval;  
      }
    This is still quite wrong. I think you can see that if you actually read those lines.

    Other than those few things, I think you're pretty close. Have you actually tested your code with some real data, and checked the results?

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

  2. #17
    Registered User
    Join Date
    Oct 2007
    Posts
    60

    Smile hope this is it

    Code:
    if (seconval < 0){ 
        firsval = 1.0;  
        seconval= 1/firsval;  
      }

    ok so this code is saying that if seconval is less than zero then firsval is equal to one and seconval is equal to a=firsval which is one. so the answer here would be 1. and then in the for loop
    Code:
     for(count = 0; count < seconval; count++){  
        powerval *= firsval;  
      }
    count won't ever be less then 1 and it never gets entered. i know i need this condition in case the exponent(meaning seconval) is negative. to fix this i need to alter the firsval becuase a number raised to the negative exponent is really 1 divided by the number raised to the absolute value of the exponent. so would the new code be
    Code:
    float power_func(float firsval, int seconval){  
      int count;  
      float powerval =1.0; 
      if (seconval < 0){ 
        firsval = 1/firsval;
        seconval *= -1;   
      } 
      for(count = 0; count < seconval; count++){  
        powerval *= firsval;  
      }  
      return powerval; 
    }

  3. #18
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That looks much better.

    Multiplying by -1 may not work quite as well as "x = -x" when it comes to speed in the compiler - not that it really makes any difference here, but if you can do things SIMPLE, why make it complicated?

    And by the way, "count will never be > 1" is not true. If "firsval" is 0.25 for example, count would be 4. But the result would of course not at all be the right answer.

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

  4. #19
    Registered User
    Join Date
    Oct 2007
    Posts
    60

    Smile

    ok..making progress..thank yoiu so much. I had a little victory dance. anyway. to tackle the ascend and descend. I see what you mean I think..next needs to be increneted too in order for each element to be checked. is this right?
    Code:
    float ascend_arr(float aarry[], int sizearr){    
      int ascend; 
      float order; 
      int next = aarry[0];
      for(ascend = 1; ascend < sizearr; ascend ++){   
          next++;
          if (next < aarry[ascend]) 
             order = 1; 
          else  
             order = -1; 
          } 
      return order;                                                                                                                              
    }

  5. #20
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I still don't know what ascend actually does, but first of all, I'm a bit dubious that "next" should be an integer, when the array contain floats.

    Is an array that contains 1.0, 1.1, 1.2, 1.3, 1.4 ascending or not? If it is, then your code doesn't work.

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

  6. #21
    Registered User
    Join Date
    Oct 2007
    Posts
    60

    Unhappy

    yes...that example is ascending. ok. what is that i need to see? I'm not quite sure. I put "next" as an int because it is the position of the element. can the position of an element be of type float? or is that wrong all together too.

  7. #22
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, maybe you should just compare the "current" with the "next" element in the array. If at any point it the elements don't follow the rule, you break the loop and return "not ascending". If you finish the loop without finding any "offending" items, then you return "is ascending".

    When I say "next", I don't mean the "next" variable you have, but rather array[x] and array[x+1].

    There should be no need for a variable like your "next".

    --
    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. #23
    Registered User
    Join Date
    Oct 2007
    Posts
    60

    back to it

    back to fixing this today..so here is another try on the ascend function
    Code:
    float ascend_arr(float aarry[], int sizearr){ 
      int ascend; 
      float order; 
      for(ascend = 1; ascend < sizearr; ascend ++){   
          if (aarry[ascend] < aarry[ascend + 1]) 
             order = 1; 
          else  
             order = -1; 
       } 
      return order; 
    }

  9. #24
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, but you still need to "stop" looping when you find that it's not ascending, otherwise you'll have "false positives" if the last two numbers are ascending, even if all the others where descending, for example.

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

  10. #25
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    Ok i see what you mean...do i just a break statement inthe else like this
    Code:
    float ascend_arr(float aarry[], int sizearr){ 
      int ascend; 
      float order; 
      for(ascend = 1; ascend < sizearr; ascend ++){   
          if (aarry[ascend] < aarry[ascend + 1]) 
             order = 1; 
          else{
             order = -1;
             break;
          } 
       } 
      return order; 
    }
    or something else because i think that might not be it. do I do the result (1 or -1) outside of the loop? do you always hve to have something after the if statement? what if i do this
    Code:
    float ascend_arr(float aarry[], int sizearr){ 
      int ascend; 
      float order; 
      for(ascend = 1; ascend < sizearr;){   
          if (aarry[ascend] < aarry[ascend + 1]) 
             ascend++; 
          else{
             order = -1;
             break;
          }
       } 
       order =1;
      return order; 
    }

  11. #26
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    also what does this mean? i don't know how to fix it?
    errors:
    cc1: warnings being treated as errors
    calculator.c: In function 'fill_arr':
    calculator.c:123: warning: format '&#37;f' expects type 'float *', but
    argument 2 has type 'float **'


    code:
    Code:
    void fill_arr(float farry[], int *sizearr){
         int fills = 0;
         while (fills < *size && scanf(“%f”, &farry) !=EOF)
              fills ++;
          } 
          *sizearr = fills;
      }
    Last edited by trprince; 11-17-2007 at 12:56 PM. Reason: i figured out why i was etting this error. i still ned help with the array in the previous post

  12. #27
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You could do:
    Code:
    float ascend_arr(float aarry[], int sizearr){ 
      int ascend; 
      for(ascend = 1; ascend < sizearr;){   
          if (aarry[ascend] < aarry[ascend + 1]) 
             ascend++; 
          else{
             return -1;
          }
       } 
      return 1; 
    }
    By the way, why is the function returning a float - for C++ I would use bool, for C an int would be the correct return type.

    Code:
    void fill_arr(float farry[], int *sizearr){
         int fills = 0;
         while (fills < *size && scanf(%f, &farry) !=EOF)
              fills ++;
          } 
          *sizearr = fills;
      }
    When you pass an array to another function, you are actually passing a pointer to the first element. So farry[] is the same as *farry. If you take the address of a *farry, it becomes a pointer to a pointer (**farry), right?

    There are two problems here:
    1. Your code is filling in to the address of the pointer to the array - which of course is not the same as the array.
    2. You are not actually filling in your ARRAY itself.

    You want to pass &farry[fills] to scanf.

    Good to see that you use "-Wall" and "-Werror" - they are both good for stopping simple errors that can take hours of debugging to find.

    --
    Mats

    --
    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. #28
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    thank you... so now my fill array function works. I put in what you had for the ascend array and it works but not in that it returns a -1 for the ascending correctly not a 1

  14. #29
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by trprince View Post
    thank you... so now my fill array function works. I put in what you had for the ascend array and it works but not in that it returns a -1 for the ascending correctly not a 1
    Ah, classic fence-post error (twice actually). [1] You are starting at element 1 and comparing with element 2. And so on, untill you come to sizearray-1, which you compare to sizearray - which is one BEYOND what you have filled in. You should start your loop at 0 and check until sizearray-1. So your "end condition" should be sizearray-1.

    Also, put the "ascend++" in the for-loop and reverse the if-statement - that way you can do away with the else altogether, and it makes the whole thing a lot simpler.

    [1] The term "fence-post error" comes from "how many posts do you need to X <unit of length> of fence if you put a post every Y <unit of length>", where the answer depends on whether you start with a post at the end, or in the middle of Y units, and also wether you need one at the end or not.

    --
    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. #30
    Registered User
    Join Date
    Oct 2007
    Posts
    60

    Smile

    ok...that works...thank you so much..sorry for the multiple question thing. thank you for heping to learn and not just giving me the answer eventhough that may have been a quick fix. i actually learned from all your help.

Page 2 of 3 FirstFirst 123 LastLast
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, 05:48 PM
  2. storage class, arrays, functions and good layout
    By disruptivetech in forum C Programming
    Replies: 4
    Last Post: 12-02-2005, 01:34 PM
  3. functions and arrays
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 03-14-2002, 08:57 AM
  4. passing multidimensional arrays to functions
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 11-17-2001, 02:27 AM
  5. elements of arrays; functions
    By sballew in forum C Programming
    Replies: 6
    Last Post: 09-03-2001, 01:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21