473,772 Members | 2,552 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sierpinski code problem with recursion

Ken
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 3904
* 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
Ken

"Alf P. Steinbach" <al***@start.no > wrote in message
news:42******** ********@news.i ndividual.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
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
Ken

"Tobias Blomkvist" <vo**@void.void > wrote in message
news:1122153880 .680543200303b9 efecc660eed8b79 188@teranews...
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
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
Ken

"Robert" <wi******@nobod y.com> wrote in message
news:Ca******** ***********@new s.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
Ken wrote:

if( m < 4 )
{
++m;
DrawSierpinsk i( m, ... );
DrawSierpinsk i( 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.
Jul 24 '05 #8
"Ken" <le******@REMOV Esympatico.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 learn-effect ..

hth
--
jb

(reply address in rot13, unscramble first)
Jul 24 '05 #9
Ken

"Jakob Bieling" <ar************ ****@rot13.com> wrote in message
news:dc******** *****@news.t-online.com...
"Ken" <le******@REMOV Esympatico.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 learn-effect ..


Ok thanks,

So I AM keeping m as global variable..

Ken
Jul 24 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3423
by: Peri | last post by:
I'm trying to create Python parser/interpreter using ANTLR. Reading grammar from language refference I found: or_expr::= xor_expr | or_expr "|" xor_expr For me it looks like infinite recursion. And so it says ANTLR. Maybe I don't understand EBNF notation. For me it should look like this. or_expr::= xor_expr | xor_expr "|" xor_expr and in ANTLR grammar file like this:
7
1692
by: Kamilche | last post by:
I want to convert a dict into string form, then back again. After discovering that eval is insecure, I wrote some code to roll a Python object, dict, tuple, or list into a string. I've posted it below. Does anyone know an easier way to accomplish this? Essentially, I want to avoid doing an 'eval' on a string to get it back into dict form... but still allow nested structures. (My current code doesn't handle nested structures.) I conked...
12
2493
by: Paul | last post by:
Hi, Global operator new and delete are overloaded and I am using stl map to store pointers, but this code crashes, can some one shed some light??? Compiler: MS VC++ 6.0 STL: Shipped with Visual Studio. #include <malloc.h> #include <map>
19
3816
by: snowdy | last post by:
I am using Interactive C with my handboard (68HC11) development system but I've got a problem that I am asking for help with. I am not new to C but learning all the time and I just cant see how to overcome this problem. Symptom: Programs fail ' Runtime error' which equates to a stack error. This is because my code is like going around and around (a snake chasing its tail is a term I've seen used) but I cant quite come to grips with how...
6
4554
by: Einar ?rn | last post by:
Hi all, is there a good way to detect recursive C code in large systems? A method or a free tool? Best regards, E
75
5642
by: Sathyaish | last post by:
Can every problem that has an iterative solution also be expressed in terms of a recursive solution? I tried one example, and am in the process of trying out more examples, increasing their complexity as I go. Here's a simple one I tried out: #include<stdio.h> /* To compare the the time and space cost of iteration against
30
8307
by: Jeff Bigham | last post by:
So, it appears that Javascript has a recursion limit of about 1000 levels on FF, maybe less/more on other browsers. Should such deep recursion then generally be avoided in Javascript? Surprisingly, deep recursion actually isn't that slow for what I'm doing. Programming this task recursively is so much more straightforward to me, but currently I'm forced to use an ugly hack to avoid going over the 1000 level limit. Of course, it could...
10
5493
by: slix | last post by:
Recursion is awesome for writing some functions, like searching trees etc but wow how can it be THAT much slower for computing fibonacci- numbers? is the recursive definition counting fib 1 to fib x-1 for every x? is that what lazy evaluation in functional languages avoids thus making recursive versions much faster? is recursive fibonacci in haskell as fast as an imperative solution in a procedural language?
35
4741
by: Muzammil | last post by:
int harmonic(int n) { if (n=1) { return 1; } else { return harmonic(n-1)+1/n; } } can any help me ??
0
9621
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10039
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9914
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7461
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6716
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5355
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4009
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.