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 );
}
}  
Share this Question
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: Topposting.
Q: What is the most annoying thing on usenet and in email?  
P: n/a

"Alf P. Steinbach" <al***@start.no> 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: Topposting. Q: What is the most annoying thing on usenet and in email?
HUmm ok, I'll try that
thank you  
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.  
P: n/a

"Tobias Blomkvist" <vo**@void.void> 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++, ........ );
}
}
########################  
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 );
}
}  
P: n/a

"Robert" <wi******@nobody.com> 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  
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 <ostream>
#include <string>
#include <conio>
#pragma hdrstop
void DrawSierpinski( int m ) {
std::cout<<std::string(m,' ')<<"Sierpinski!\n";
if( m < 4 )
{
++m;
DrawSierpinski( m );
DrawSierpinski( m );
}
}
#pragma argsused
int main(int argc, char* argv[])
{
DrawSierpinski(0);
getch();
return 0;
}
//
....produces the following recursion, which is as far as I know similar
to what you are looking for (one space for each level of recursion):
Sierpinski!
Sierpinski!
Sierpinski!
Sierpinski!
Sierpinski!
Sierpinski!
Sierpinski!
Sierpinski!
Sierpinski!
Sierpinski!
Sierpinski!
Sierpinski!
Sierpinski!
Sierpinski!
Sierpinski!
Sierpinski!
Sierpinski!
Sierpinski!
Sierpinski!
Sierpinski!
Sierpinski!
Sierpinski!
Sierpinski!
Sierpinski!
Sierpinski!
Sierpinski!
Sierpinski!
Sierpinski!
Sierpinski!
Sierpinski!
Sierpinski!
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.  
P: n/a

"Ken" <le******@REMOVEsympatico.ca> 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 learneffect ..
hth

jb
(reply address in rot13, unscramble first)  
P: n/a

"Jakob Bieling" <ar****************@rot13.com> wrote in message
news:dc*************@news.tonline.com... "Ken" <le******@REMOVEsympatico.ca> 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 learneffect ..
Ok thanks,
So I AM keeping m as global variable..
Ken  
P: n/a

"Ken" <le******@REMOVEsympatico.ca> 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)  
P: n/a

Ken wrote: "Jakob Bieling" <ar****************@rot13.com> wrote in message news:dc*************@news.tonline.com... "Ken" <le******@REMOVEsympatico.ca> 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 learneffect ..
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.   This discussion thread is closed Replies have been disabled for this discussion.   Question stats  viewed: 3423
 replies: 11
 date asked: Jul 23 '05
