not sure how to say this...

This is a discussion on not sure how to say this... within the C Programming forums, part of the General Programming Boards category; I have a loop going with an array - say array[i] for i=0 to i<10 - with conditions, and if ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    32

    not sure how to say this...

    I have a loop going with an array - say array[i] for i=0 to i<10 - with conditions, and if the conditions aren't met at a value in the array, I want to ignore the value. I have array[i-1] in the condition, so my problem is that if a value [i] is ignored in one cycle of the loop, I don't want it to be considered [i-1] in the next cycle. In that case I want [i-1] to refer to the last un-ignored value.
    I'm really unsure of how to do this. Can someone set me on the right track? Thanks.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Make a variable previous_value.

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    32
    Then how do i incorporate it into the loop? I was thinking of something like that, but I couldn't figure out how to define it.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Code:
    if (big condition) {
        do stuff
        previous_value = a[i];
    }

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    I suppose you are using a for-loop. Why not use a while-loop. Then you ll be able to have more control on the increase/decrease of i. That should solve your problem...

  6. #6
    Registered User
    Join Date
    Jun 2008
    Posts
    32
    All right, let me talk through this to see if I getting it...still not sure.
    -My main condition is if array[i]>array[i-1]-

    So should I start out by making previous_value = array[i-1]?

    Then go into the loop... have (condition) do stuff
    else previous_value=previous_value-1?

    I need it to still work if multiple values in a row are ignored.

    But then when the condition is met again, I want previous value to go back to being array[i-1]...so should I put that in with the "do stuff" - that previous_value = array[i-1]? Would that reset it all right?

  7. #7
    Registered User
    Join Date
    Jun 2008
    Posts
    32
    C_ntua - sorry, I'm not sure what you mean, could you explain a little more? Why would a while loop be better?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by sqytoad View Post
    But then when the condition is met again, I want previous value to go back to being array[i-1]...so should I put that in with the "do stuff" - that previous_value = array[i-1]? Would that reset it all right?
    This is not consistent with what you said before. Before you said that if the previous value didn't meet the condition, you wanted to ignore it. Now you say that if a[i] meets the condition, you no longer want to ignore a[i-1]. You'll have to pick one. In the original case, you need to set previous_value to a[i] so that the next time through the loop it's the right thing.

    Perhaps we should ask this question: What are you trying to do?

  9. #9
    Registered User
    Join Date
    Jun 2008
    Posts
    32
    What I'm saying is that if a value doesn't meet the condition, I want to ignore it - completely. So that it's not used in the next cycle of the loop.
    Let me try and go through this better.
    This is what I'm trying to do -
    For example, the loop is running, and the condition b[i]>b[i-1] (values of a[i] affect b[i]) is met for a[1], a[2], a[3]. It keeps running through.
    The the condition is NOT met for a[4].
    It ignores a[4].
    Then the next cycle starts, a[i] is a[5].
    *What I want to happen in this cycle is for "i-1" to refer to a[3], since that was the last value that met the condition. NOT a[4], as "i-1" actually would. This is my first problem.

    Let's say the condition wasn't met on this cycle either. So in the next cycle - a[i]=a[6], I still want "i-1" to refer to a[3].

    But this time the condition was met, so when the next cycle starts again, a[i]=a[7], I want "i-1" to refer to what it actually says, a[6].


    What I said was any time a[i] meets the condition I don't want it to be ignored on the next cycle as a[i-1]. But any time the condition is not met, it IS ignored. I don't think I was inconsistent. A little confusing, I'll grant you. :P But thus is the problem...

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by sqytoad View Post
    What I'm saying is that if a value doesn't meet the condition, I want to ignore it - completely. So that it's not used in the next cycle of the loop.
    Let me try and go through this better.
    This is what I'm trying to do -
    For example, the loop is running, and the condition b[i]>b[i-1] (values of a[i] affect b[i]) is met for a[1], a[2], a[3]. It keeps running through.
    The the condition is NOT met for a[4].
    It ignores a[4].
    Then the next cycle starts, a[i] is a[5].
    *What I want to happen in this cycle is for "i-1" to refer to a[3], since that was the last value that met the condition. NOT a[4], as "i-1" actually would. This is my first problem.

    Let's say the condition wasn't met on this cycle either. So in the next cycle - a[i]=a[6], I still want "i-1" to refer to a[3].

    But this time the condition was met, so when the next cycle starts again, a[i]=a[7], I want "i-1" to refer to what it actually says, a[6].


    What I said was any time a[i] meets the condition I don't want it to be ignored on the next cycle as a[i-1]. But any time the condition is not met, it IS ignored. I don't think I was inconsistent. A little confusing, I'll grant you. :P But thus is the problem...
    So, that's exactly what I typed up above. Since a[1] satisfies the condition, at the bottom of the loop set previous_value to a[1]. Then in the next loop, previous_value is a[1]. If a[2] DIDN'T meet the condition, you WOULDN'T set previous_value, so in the next loop previous_value is still a[1]. What's the problem here?

  11. #11
    Registered User
    Join Date
    Jun 2008
    Posts
    32
    The problem is I'm still a little confused about how to use what you're saying.
    So instead of having my condition be a[i]/a[i-1] I would have a[i]/previous_value?

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Right. Since a[i-1] is not the value you want to use, don't use it.

  13. #13
    Registered User
    Join Date
    Jun 2008
    Posts
    32
    Yeah, I was just using it as a placeholder for while I was working on it. I think I misunderstood what you were saying at first, so I wanted to really make sure I got what you were saying now that I thought I had it. lol
    I'm pretty sure I'm okay now. I'll be back if I still have trouble. :S
    Thanks a lot!

  14. #14
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,301
    You'll have to start your loop at 1 not 0, or you'll have to code it in such a way as to take into account the possibility of where there is no previous value. Otherwise you will have a buffer underrun bug.
    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"

  15. #15
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Well, I suggested while because you can have the i increased with every pass. You can have another one, lets say j, that shows the value not ignored. In your case j could show the last value not ingored.
    Lets say you start your calculations from a[1]. You have the following:
    Code:
    int j = 0;
    int i = 1;
    
    while (...) {
      if (a[i] compare a[j]) {
        do_stuff;
        j++;
       }
       i++;
    }
    First not that at the beggining j == i - 1
    a[j] and a[j] will be compared. If you get what you want then j would be increased. In any case i will also be increased. If you don't get what you want then j will not get increased. Isn't that what you want:
    Well thinking of it even better the previous value thing seems better:
    Code:
    for (i = 1; ...; i++) {
      if (a[i] compare a[previous]) {
        do_stuff;
        previous++;
      }
    }
    The same thing actually. Just proposed while as an idea. Gives you the opportunity to think more clearly about what you should do.

    Is that what you want?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

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