Connecting Tech Pros Worldwide Forums | Help | Site Map

Sierpinski code problem with recursion

Ken
Guest
 
Posts: n/a
#1: Jul 23 '05
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 );


}



}



Alf P. Steinbach
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Sierpinski code problem with recursion


* Ken:[color=blue]
>
> int m= 0;[/color]

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?
Ken
Guest
 
Posts: n/a
#3: Jul 23 '05

re: Sierpinski code problem with recursion



"Alf P. Steinbach" <alfps@start.no> wrote in message
news:42e2aa25.315400984@news.individual.net...[color=blue]
>* Ken:[color=green]
>>
>> int m= 0;[/color]
>
> 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?[/color]

HUmm ok, I'll try that

thank you


Tobias Blomkvist
Guest
 
Posts: n/a
#4: Jul 23 '05

re: Sierpinski code problem with recursion


Ken wrote:[color=blue]
> 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);[/color]

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.
Ken
Guest
 
Posts: n/a
#5: Jul 24 '05

re: Sierpinski code problem with recursion



"Tobias Blomkvist" <void@void.void> wrote in message
news:1122153880.680543200303b9efecc660eed8b79188@t eranews...[color=blue]
> Ken wrote:[color=green]
>> 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);[/color]
>
> 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, ... );
> }
>[/color]


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++, ........ );
}
}
########################


Robert
Guest
 
Posts: n/a
#6: Jul 24 '05

re: Sierpinski code problem with recursion


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 );


}



}
Ken
Guest
 
Posts: n/a
#7: Jul 24 '05

re: Sierpinski code problem with recursion



"Robert" <withheld@nobody.com> wrote in message
news:CaGEe.3014$PL5.298759@news.xtra.co.nz...[color=blue]
> 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 );
>
>
> }
>[/color]


Well In that case everytime the function is called, m is reset to zero so it
keeps on looping forever.

ken


Tobias Blomkvist
Guest
 
Posts: n/a
#8: Jul 24 '05

re: Sierpinski code problem with recursion


Ken wrote:[color=blue][color=green]
>>
>>if( m < 4 )
>>{
>>++m;
>>DrawSierpinski( m, ... );
>>DrawSierpinski( m, ... );
>>}
>>[/color]
>
>
>
> 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;[/color]

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.
Jakob Bieling
Guest
 
Posts: n/a
#9: Jul 24 '05

re: Sierpinski code problem with recursion


"Ken" <lerameur@REMOVEsympatico.ca> wrote in message
news:zPLEe.43$q23.17217@news20.bellglobal.com...
[color=blue]
> Well In that case everytime the function is called, m is reset to zero
> so it keeps on looping forever.[/color]


'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)


Ken
Guest
 
Posts: n/a
#10: Jul 24 '05

re: Sierpinski code problem with recursion



"Jakob Bieling" <argfhesNGtzkQBGarg@rot13.com> wrote in message
news:dc07g7$7ag$01$1@news.t-online.com...[color=blue]
> "Ken" <lerameur@REMOVEsympatico.ca> wrote in message
> news:zPLEe.43$q23.17217@news20.bellglobal.com...
>[color=green]
>> Well In that case everytime the function is called, m is reset to zero so
>> it keeps on looping forever.[/color]
>
>
> '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 ..
>[/color]

Ok thanks,

So I AM keeping m as global variable..

Ken


Jakob Bieling
Guest
 
Posts: n/a
#11: Jul 24 '05

re: Sierpinski code problem with recursion


"Ken" <lerameur@REMOVEsympatico.ca> wrote in message
news:p7PEe.319$q23.53301@news20.bellglobal.com...
[color=blue]
> So I AM keeping m as global variable..[/color]

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)


Josh Mcfarlane
Guest
 
Posts: n/a
#12: Jul 25 '05

re: Sierpinski code problem with recursion


Ken wrote:[color=blue]
> "Jakob Bieling" <argfhesNGtzkQBGarg@rot13.com> wrote in message
> news:dc07g7$7ag$01$1@news.t-online.com...[color=green]
> > "Ken" <lerameur@REMOVEsympatico.ca> wrote in message
> > news:zPLEe.43$q23.17217@news20.bellglobal.com...
> >[color=darkred]
> >> Well In that case everytime the function is called, m is reset to zero so
> >> it keeps on looping forever.[/color]
> >
> >
> > '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 ..
> >[/color]
>
> Ok thanks,
>
> So I AM keeping m as global variable..
>
> Ken[/color]

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.

Closed Thread


Similar C / C++ bytes