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.
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.
What have you tried?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
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--; } }
Thank you for showing some effort.
You probably had a cut and paste problem. Here is your code reformated:
Some hints: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--; } }
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.
You could probably fit both: m < f and j < 12 in the same loop condition by changing it to: m >= f and j < 12
Also, the code you posted starts with
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 doCode:if (m > b) { k = 17; }
Code:if (m < b) { // while loop and all that other stuff } else { k = 17; }
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;
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;
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?
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.
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.
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;
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.
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.
Yes, once the loop exits, you can safely set k=17.
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.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.