455,130 Members | 1,365 Online
Need help? Post your question and get tips & solutions from a community of 455,130 IT Pros & Developers. It's quick & easy.

# Sierpinski code problem with recursion

 P: n/a Hello, I have a recursive Sierpinski code here. The code is right and every line works fine by itself. I wish for all of them to call the function DrawSierpinski. But in this cae it only calls the first recusive function and at soon as n = 4 its stop. How do I go about so that all 8 lines get 4 recusion thanks ken int m= 0; void DrawSierpinski(HDC hDC, float a, float b, float c, float d) // add any more parameters you need { Rectangle( hDC, ((2 * a + c) / 3.0) , ((2 * b + d) / 3.0), ((a + 2 * c) / 3.0), (b + 2 * d) / 3.0); if (m< 4) { m++; DrawSierpinski(hDC, a , b , (2 * a + c) / 3.0, (2 * b + d) / 3.0); DrawSierpinski( hDC,(2 * a + c) / 3.0, b , (a + 2 * c) / 3.0, (2 * b + d) / 3.0); DrawSierpinski(hDC, (a + 2 * c) / 3.0, b , c , (2 * b + d) / 3.0); DrawSierpinski(hDC, a , (2 * b + d) / 3.0, (2 * a + c) / 3.0, (b + 2 * d) / 3.0); DrawSierpinski( hDC,(a + 2 * c) / 3.0, (2 * b + d) / 3.0, c , (b + 2 * d) / 3.0); DrawSierpinski(hDC, a , (b + 2 * d) / 3.0, (2 * a + c) / 3.0, d ); DrawSierpinski(hDC, (2 * a + c) / 3.0, (b + 2 * d) / 3.0, (a + 2 * c) / 3.0, d ); DrawSierpinski(hDC, (a + 2 * c) / 3.0, (b + 2 * d) / 3.0, c , d ); } } Jul 23 '05 #1
11 Replies

 P: n/a * Ken: int m= 0; Don't use globals. -- A: Because it messes up the order in which people normally read text. Q: Why is it such a bad thing? A: Top-posting. Q: What is the most annoying thing on usenet and in e-mail? Jul 23 '05 #2

 P: n/a "Alf P. Steinbach" wrote in message news:42****************@news.individual.net...* Ken: int m= 0; Don't use globals. -- A: Because it messes up the order in which people normally read text. Q: Why is it such a bad thing? A: Top-posting. Q: What is the most annoying thing on usenet and in e-mail? HUmm ok, I'll try that thank you Jul 23 '05 #3

 P: n/a Ken wrote: int m= 0; void DrawSierpinski(HDC hDC, float a, float b, float c, float d) // add any more parameters you need { Rectangle( hDC, ((2 * a + c) / 3.0) , ((2 * b + d) / 3.0), ((a + 2 * c) / 3.0), (b + 2 * d) / 3.0); if (m< 4) { m++; DrawSierpinski(hDC, a , b , (2 * a + c) / 3.0, (2 * b + d) / 3.0); DrawSierpinski( hDC,(2 * a + c) / 3.0, b , (a + 2 * c) / 3.0, (2 * b + d) / 3.0); As pointed out your problem is (probably, since I actually don't know what Sirpinski is) the global variable. With each recursion "m" is being incremented at a global context, not at a local recursive context. Once the first recursion is done "m" has the value 4, prohibiting further recursions. Depending on how you actually want the recursions to proceed, make "m" an argument with each function call: if( m < 4 ) { ++m; DrawSierpinski( m, ... ); DrawSierpinski( m, ... ); } Tobias -- IMPORTANT: The contents of this email and attachments are confidential and may be subject to legal privilege and/or protected by copyright. Copying or communicating any part of it to others is prohibited and may be unlawful. Jul 23 '05 #4

 P: n/a "Tobias Blomkvist" wrote in message news:1122153880.680543200303b9efecc660eed8b79188@t eranews... Ken wrote: int m= 0; void DrawSierpinski(HDC hDC, float a, float b, float c, float d) // add any more parameters you need { Rectangle( hDC, ((2 * a + c) / 3.0) , ((2 * b + d) / 3.0), ((a + 2 * c) / 3.0), (b + 2 * d) / 3.0); if (m< 4) { m++; DrawSierpinski(hDC, a , b , (2 * a + c) / 3.0, (2 * b + d) / 3.0); DrawSierpinski( hDC,(2 * a + c) / 3.0, b , (a + 2 * c) / 3.0, (2 * b + d) / 3.0); As pointed out your problem is (probably, since I actually don't know what Sirpinski is) the global variable. With each recursion "m" is being incremented at a global context, not at a local recursive context. Once the first recursion is done "m" has the value 4, prohibiting further recursions. Depending on how you actually want the recursions to proceed, make "m" an argument with each function call: if( m < 4 ) { ++m; DrawSierpinski( m, ... ); DrawSierpinski( m, ... ); } Well That did not work, as I tried it and did not work, I also tried a different variable (see below) to increment for each function and I get the exact same result as before with no compile error....weird everything after row 1 get done only once.... anyone ? ##################### int m= 0; int p=0; int q=0; int r=0; int s=0; int t=0; int u=0; int v=0; void DrawSierpinski(int m, HDC hDC, float a, float b, float c, float d) // add any more parameters you need { Rectangle( hDC, ((2 * a + c) / 3.0) , ((2 * b + d) / 3.0), ((a + 2 * c) / 3.0), (b + 2 * d) / 3.0); if (m< 4 && p<4 && q<4 && r<4 && s<4 && t<4 && u<4 && v<4) { DrawSierpinski(m= m+1, hDC, ......0); //row 1 DrawSierpinski(p++, hDC...); DrawSierpinski(q++, ............); DrawSierpinski(r++, .....); //row 2 DrawSierpinski(s++, ..........); DrawSierpinski(t++, ....... ); //row 3 DrawSierpinski(u++, hDC.............. ); DrawSierpinski(v++, ........ ); } } ######################## Jul 24 '05 #5

 P: n/a That still doesn't change the fact that the drawSierpinski function will interfere with its "parent" drawSierpinski function that called it. All you need to do (I *think*, dunno what the program is actually supposed to do) iis move int m=0; to inside the function, so each function has its own copy: void DrawSierpinski(HDC hDC, float a, float b, float c, float d) // add any more parameters you need { int m= 0; Rectangle( hDC, ((2 * a + c) / 3.0) , ((2 * b + d) / 3.0), ((a + 2 * c) / 3.0), (b + 2 * d) / 3.0); if (m< 4) { m++; DrawSierpinski(hDC, a , b , (2 * a + c) / 3.0, (2 * b + d) / 3.0); DrawSierpinski( hDC,(2 * a + c) / 3.0, b , (a + 2 * c) / 3.0, (2 * b + d) / 3.0); DrawSierpinski(hDC, (a + 2 * c) / 3.0, b , c , (2 * b + d) / 3.0); DrawSierpinski(hDC, a , (2 * b + d) / 3.0, (2 * a + c) / 3.0, (b + 2 * d) / 3.0); DrawSierpinski( hDC,(a + 2 * c) / 3.0, (2 * b + d) / 3.0, c , (b + 2 * d) / 3.0); DrawSierpinski(hDC, a , (b + 2 * d) / 3.0, (2 * a + c) / 3.0, d ); DrawSierpinski(hDC, (2 * a + c) / 3.0, (b + 2 * d) / 3.0, (a + 2 * c) / 3.0, d ); DrawSierpinski(hDC, (a + 2 * c) / 3.0, (b + 2 * d) / 3.0, c , d ); } } Jul 24 '05 #6

 P: n/a "Robert" wrote in message news:Ca*******************@news.xtra.co.nz... That still doesn't change the fact that the drawSierpinski function will interfere with its "parent" drawSierpinski function that called it. All you need to do (I *think*, dunno what the program is actually supposed to do) iis move int m=0; to inside the function, so each function has its own copy: void DrawSierpinski(HDC hDC, float a, float b, float c, float d) // add any more parameters you need { int m= 0; Rectangle( hDC, ((2 * a + c) / 3.0) , ((2 * b + d) / 3.0), ((a + 2 * c) / 3.0), (b + 2 * d) / 3.0); if (m< 4) { m++; DrawSierpinski(hDC, a , b , (2 * a + c) / 3.0, (2 * b + d) / 3.0); DrawSierpinski( hDC,(2 * a + c) / 3.0, b , (a + 2 * c) / 3.0, (2 * b + d) / 3.0); DrawSierpinski(hDC, (a + 2 * c) / 3.0, b , c , (2 * b + d) / 3.0); DrawSierpinski(hDC, a , (2 * b + d) / 3.0, (2 * a + c) / 3.0, (b + 2 * d) / 3.0); DrawSierpinski( hDC,(a + 2 * c) / 3.0, (2 * b + d) / 3.0, c , (b + 2 * d) / 3.0); DrawSierpinski(hDC, a , (b + 2 * d) / 3.0, (2 * a + c) / 3.0, d ); DrawSierpinski(hDC, (2 * a + c) / 3.0, (b + 2 * d) / 3.0, (a + 2 * c) / 3.0, d ); DrawSierpinski(hDC, (a + 2 * c) / 3.0, (b + 2 * d) / 3.0, c , d ); } Well In that case everytime the function is called, m is reset to zero so it keeps on looping forever. ken Jul 24 '05 #7

 P: n/a Ken wrote:if( m < 4 ){++m;DrawSierpinski( m, ... );DrawSierpinski( m, ... );} Well That did not work, as I tried it and did not work, I also tried a different variable (see below) to increment for each function and I get the exact same result as before with no compile error....weird everything after row 1 get done only once.... anyone ? ##################### int m= 0; int p=0; int q=0; int r=0; int s=0; int t=0; int u=0; int v=0; This... //--------------------------------------------------------------------------- #include #include #include #pragma hdrstop void DrawSierpinski( int m ) { std::cout<

 P: n/a "Ken" wrote in message news:zP****************@news20.bellglobal.com... Well In that case everytime the function is called, m is reset to zero so it keeps on looping forever. 'm' is the level of recursion you are currently processing, correct? The function 'needs to know' (see below), at which level of recursion it is at (so it can decide whether or not the recursion should end). As it is now, the function does not know that, which is why you are looping forever (everytime the function is called, it is assumed that the recursion just started). Now think about the hint 'add any more parameters you need' you were given .. and about the fact that parameters are things 'the function needs to know' (see above). I am trying to not give you the solution as this looks like homework ... and I do not want to spoil the learn-effect .. hth -- jb (reply address in rot13, unscramble first) Jul 24 '05 #9

 P: n/a "Jakob Bieling" wrote in message news:dc*************@news.t-online.com... "Ken" wrote in message news:zP****************@news20.bellglobal.com... Well In that case everytime the function is called, m is reset to zero so it keeps on looping forever. 'm' is the level of recursion you are currently processing, correct? The function 'needs to know' (see below), at which level of recursion it is at (so it can decide whether or not the recursion should end). As it is now, the function does not know that, which is why you are looping forever (everytime the function is called, it is assumed that the recursion just started). Now think about the hint 'add any more parameters you need' you were given .. and about the fact that parameters are things 'the function needs to know' (see above). I am trying to not give you the solution as this looks like homework .. and I do not want to spoil the learn-effect .. Ok thanks, So I AM keeping m as global variable.. Ken Jul 24 '05 #10

 P: n/a "Ken" wrote in message news:p7*****************@news20.bellglobal.com... So I AM keeping m as global variable.. No, imagine what happens if you keep m as a global (read this along with the source code): 1) first call to DrawSierpinski, and m is <4 2) increment m, so m=1 and call DrawSierpinski again 1) m is still <4 2) increment m, so m=2 and call DrawSierpinski again 1) m is still <4 2) increment m, so m=3 and call DrawSierpinski again 1) m is still <4 2) increment m, so m=4 and call DrawSierpinski again 1) m is =4, do not call DrawSierpinski any more and return 3) call DrawDrawSierpinski for the same level of recursion 1) m is =4, do not call DrawSierpinski any more and return 4) [the same as above, repeated a couple of times] 3) call DrawDrawSierpinski for the same level of recursion 1) m is =4, do not call DrawSierpinski any more and return And that is the error, because you are using a global variable. You need to 'tell' each DrawSierpinski call, in which recursion it is. Again, think about the hint in your source code: 'add any more parameters you need'. Do you need more parameters? (Hint: yes, you do) hth -- jb (reply address in rot13, unscramble first) Jul 24 '05 #11

 P: n/a Ken wrote: "Jakob Bieling" wrote in message news:dc*************@news.t-online.com... "Ken" wrote in message news:zP****************@news20.bellglobal.com... Well In that case everytime the function is called, m is reset to zero so it keeps on looping forever. 'm' is the level of recursion you are currently processing, correct? The function 'needs to know' (see below), at which level of recursion it is at (so it can decide whether or not the recursion should end). As it is now, the function does not know that, which is why you are looping forever (everytime the function is called, it is assumed that the recursion just started). Now think about the hint 'add any more parameters you need' you were given .. and about the fact that parameters are things 'the function needs to know' (see above). I am trying to not give you the solution as this looks like homework .. and I do not want to spoil the learn-effect .. Ok thanks, So I AM keeping m as global variable.. Ken Variables should be defined at a scope in which they are used (and even then, you will almost never need a global variable, even if you're not doing OO programming. You need another parameter, perferably one to designate the current loop iteration. You receive that variable, increment it, then pass it to the next function calls. Once the nested recursion is greater than 4, the recursion stops. X calls Y, X knows m is 0, so it increments it and passes it to Y. Y now knows that m is 1, so it calls A, B, and C and passed them 2. They all only know about Y and that recursion is 2, and continue onward down the line. The functions only depend on their parent function, not with any relation to each other, so you can simply pass the parameter, increment, and repass, without any dependancy on each other. Jul 24 '05 #12

### This discussion thread is closed

Replies have been disabled for this discussion.