Thread: Help to figure out a loop

  1. #1
    Registered User
    Join Date
    Jan 2012
    Location
    Stara Zagora, Bulgaria, Bulgaria
    Posts
    6

    Help to figure out a loop

    Hello everyone,

    I have trouble figuring out a loop from a block diagram, I need just this segment of a program. As I see it, it may be an endless loop but anyway I have to write the code for it. I appreciate any help, thanks in advance.
    Help to figure out a loop-0ae18f0f2182fa64-jpg

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What have you tried?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2012
    Location
    Stara Zagora, Bulgaria, Bulgaria
    Posts
    6
    While loop with several "if, else if" but seems to be wrong for me just don't know why. If you need I can copy my solution.

  4. #4
    Registered User
    Join Date
    Jan 2012
    Location
    Stara Zagora, Bulgaria, Bulgaria
    Posts
    6
    I'm really sorry for the repost, just i guess I don't have permission to edit my own posts. This is my solution for now but am not sure if it's 100% right.
    Code:
    if(m>b)
    {
        k=17;}    while(m<b){    j=4;        d[i]=m;    j++;    if(m<f)    {        k=m%i;        m--;    }    else if (j<12)    {        d[i]=m;        j++;    }    else    {        k=m%i;        m--;    } }

  5. #5
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    143
    Thank you for showing some effort.

    You probably had a cut and paste problem. Here is your code reformated:
    Code:
    if(m>b)
    {
        k=17;
    }
    while(m<b){
        j=4;
        d[i]=m;
        j++;
        if(m<f)
        {
            k=m%i;
            m--;
        }
        else if (j<12)
        {
            d[i]=m;
            j++;
        }
        else
        {
            k=m%i;
            m--;
        }
    }
    Some hints:

    1) Your flow chart has two loops. You only show one.
    2) C programming language has three loop types while, do ... while, and the for ( ; ; ) loop. The for loop can basically defined using a while loop. You will probably want to use two different types of loops.
    3) For one of your loops, you will need to do a complex exit (leave) condition.
    4) Will k=17 only be executed if you do not loop?
    5) I would assume the lecturer showed you some flow chart subsets and their equivalent written in C. If I am right, look back at those notes.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You could probably fit both: m < f and j < 12 in the same loop condition by changing it to: m >= f and j < 12

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Also, the code you posted starts with
    Code:
    if (m > b)
    {
        k = 17;
    }
    which is incorrect. The correct version in english is "if m is NOT less than b, k is 17", which is the same as "if m is greater than OR EQUAL TO b, k is 17". Alternatively you could just do
    Code:
    if (m < b)
    {
        // while loop and all that other stuff
    }
    else
    {
        k = 17;
    }

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Should it really be an else condition? As soon as: m >= b, k should be 17 as far as I can tell.

    Code:
    while( m < b ) {
        /* the rest goes here */
    }
    
    k = 17;

  9. #9
    Registered User
    Join Date
    Jan 2012
    Location
    Stara Zagora, Bulgaria, Bulgaria
    Posts
    6
    Thank you all! I really appreciate it! Am I getting any closer to the truth? ->>>>

    Code:
        while (m<b)
        {
            for(j=4;j<12;d[i]=m,j++)
            {
                if(m<f) break; 
            }
            k=m%i;
            m--;
        }
        k=17;

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Subsonics View Post
    Should it really be an else condition? As soon as: m >= b, k should be 17 as far as I can tell.
    We don't actually have enough information, particularly the intended types for m and b. You only do the loop when m is less than b. The only thing that changes the value of m in that loop is m--, which still leaves it less than b, meaning you go back into the loop. There's no way to get a m greater than or equal to b unless we let it wrap around, but that still depends on the type and value of b. If b is a type with more bits than m, and b is greater than the maximum possible value for m, m can never be bigger than b and thus it's an infinite loop.

    @ZdravkoTsv: Can we get a little more info? What does the rest of the flow chart look like? Was there any info provided on what types to use for each variable?

    Quote Originally Posted by ZdravkoTsv View Post
    Thank you all! I really appreciate it! Am I getting any closer to the truth? ->>>>
    As for your most recent attempt, you're a little closer. I generally find while and do-while loops a bit more flexible and easier to translate than for loops. One issue with your for loop is that the d[i]=m and j++ are always executed after the loop body is done. Also, it's possible that for loops (and while loops) never execute the loop body or increment statement. But in the flow chart, we see that d[i]=m and j++ are always executed at least once. The decision blocks that tell you to loop are farther down in the flow chart, i.e. they happen after d[i]=m and j++. You probably want a do-while then, since it is the only loop that always executes at least once.

    Try writing it out in something closer to english first. Note that a "no" decision on m < f causes you to loop, so you loop when m is NOT less than f. The opposite of less than is greater than or equal to. Also note that you must pass through both the m<f decision block AND the j<12 decision block to get to the top of the loop. Thus you need an AND (&&) in your loop condition.

  11. #11
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by anduril462 View Post
    We don't actually have enough information, particularly the intended types for m and b. You only do the loop when m is less than b. The only thing that changes the value of m in that loop is m--, which still leaves it less than b, meaning you go back into the loop.
    Since we don't have the type information of m and b, all we have is the flow chart. Every time the loop starts over ( m < b ) is evaluated, if that is not true k should be 17.

    Assuming that m is signed, it will actually become larger than b once it underflows.


    Edit: erh, actually it should not matter if it's signed or not. Only if b is UINT_MAX or INT_MAX in case of ints.
    Last edited by Subsonics; 01-28-2012 at 01:43 PM.

  12. #12
    Registered User
    Join Date
    Jan 2012
    Location
    Stara Zagora, Bulgaria, Bulgaria
    Posts
    6
    Quote Originally Posted by anduril462 View Post
    @ZdravkoTsv: Can we get a little more info? What does the rest of the flow chart look like? Was there any info provided on what types to use for each variable?
    Thank you for the help, but no I don't have more info. All I have is this segment of a flowchart and nothing else, just assignment to write the code for the loop. I used "do...while" as you suggested and it looks good, but I'm not so sure.

    Code:
    while (m<b)
    {
        j=4;
        do
        {
            d[i]=m;
            j++;
        } while (j<12 && m>=f);
        k=m%i;
        m--;
    }
    k=17;

  13. #13
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    That looks right to me. I guess the whole point of the exercise is to translate the flowchart to code, not code that actually makes sense.

  14. #14
    Registered User
    Join Date
    Jan 2012
    Location
    Stara Zagora, Bulgaria, Bulgaria
    Posts
    6
    Yes it doesn't make much sense to me either, but what I know I'm just a newbie. Yes the assignment is just to translate to code. Thanks a lot for the tips again, guys it was VERY helpful.

  15. #15
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Subsonics View Post
    Since we don't have the type information of m and b, all we have is the flow chart. Every time the loop starts over ( m < b ) is evaluated, if that is not true k should be 17.
    Yes, once the loop exits, you can safely set k=17.

    Assuming that m is signed, it will actually become larger than b once it underflows.


    Edit: erh, actually it should not matter if it's signed or not. Only if b is UINT_MAX or INT_MAX in case of ints.
    Your first sentence there only applies if m's width is greater than or equal to that of b. b doesn't have to be <type>_MAX if b is of greater width than m. For example, if m is a char (totally possible) and b an int equal to 300 (also possible), it will never exceed b on an underflow, regardless of signed/unsigned or whether b is <type>_MAX. It only matters is if b is bigger than m's maximum value.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trying to figure out a simple loop....
    By chadsxe in forum C++ Programming
    Replies: 9
    Last Post: 01-05-2006, 01:31 PM
  2. cant figure out where to put the loop....
    By seal in forum C Programming
    Replies: 2
    Last Post: 08-30-2005, 10:10 AM
  3. can't figure out a loop...
    By bcianfrocca in forum C++ Programming
    Replies: 7
    Last Post: 09-20-2004, 06:17 AM
  4. ahh i can't figure out this loop
    By blindleaf in forum C Programming
    Replies: 1
    Last Post: 03-18-2003, 09:42 AM
  5. I can't figure out why this doesn't exit the loop, MAN!
    By Shadow12345 in forum C++ Programming
    Replies: 2
    Last Post: 10-02-2002, 08:47 AM