Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472
[phpBB Debug] PHP Notice: in file /includes/functions.php on line 3368: Cannot modify header information - headers already sent by (output started at /includes/bbcode.php:472)
[phpBB Debug] PHP Notice: in file /includes/functions.php on line 3370: Cannot modify header information - headers already sent by (output started at /includes/bbcode.php:472)
[phpBB Debug] PHP Notice: in file /includes/functions.php on line 3371: Cannot modify header information - headers already sent by (output started at /includes/bbcode.php:472)
[phpBB Debug] PHP Notice: in file /includes/functions.php on line 3372: Cannot modify header information - headers already sent by (output started at /includes/bbcode.php:472)
OrbWorks Community Forum • View topic - Recursion difficulty

Recursion difficulty

Discuss PocketC for Palm OS (including Desktop Edition)

Postby SimeonB on Tue May 15, 2001 12:19 pm

Hello all,

I am brand new to the world of PocketC and am using it to try and master programming again (been off it for a few years). I have attempted a very simple program, which works successfuly, and goes like this:

// Test

main() {
int myInteger;
myInteger = gets("Enter a number: ");
if (confirm("Are you sure? Happy with " + myInteger))
alert( myInteger);
else
alert("Try again");

}

However, I wanted to make it really clever, and after the message box that says "Try Again" go back to where it asks for a number again. Is there any way of doing this simply, or is it difficult?

Many Thanks

Simeon
SimeonB
 
Posts: 4
Joined: Tue May 15, 2001 12:13 pm

Postby lale on Tue May 15, 2001 1:11 pm

Hi Simeon,

This should do the recursion:

// Test

main() {
int myInteger;
myInteger = gets("Enter a number: ");
if (confirm("Are you sure? Happy with " + myInteger))
alert( myInteger);
else{
alert("Try again");
main();
}
}

Regards,

Lars
lale
 
Posts: 54
Joined: Mon Apr 23, 2001 10:56 am
Location: Germany

Postby SimeonB on Tue May 15, 2001 1:38 pm

Many thanks, Lars. Obvious really - I was trying all sorts of things, but the simple solution didn't come to me :-)

Thanks again

Simeon
SimeonB
 
Posts: 4
Joined: Tue May 15, 2001 12:13 pm

Postby wolfgang on Tue May 15, 2001 3:04 pm

Hello,

the recursion solution has a possible shortcoming: each time you call the function, a new set of the local variables is created on the stack. I don't know how PocketC handles the stack and how large it is, but if you say "no" often enough in a row it could potentially run full and break the program. That said, a simpler solution is a simple loop:

main()
{
int myInteger, i_done;
i_done = 0;

while (!i_done)
{
myInteger = gets("Enter a number: ");
if (confirm("Are you sure? Happy with " + myInteger))
{
alert( myInteger);
i_done = 1;
}
else
{
alert("Try again");
}
} // end of while loop
}


Wolfgang
Composing music on the PocketPC! - http://www.pdamusician.com
wolfgang
 
Posts: 281
Joined: Tue Dec 12, 2000 4:22 am
Location: USA

Postby wossname on Tue May 15, 2001 3:39 pm

I hate to say this, but...
I wish PocketC had a Goto statement. It would make life easier, even though it can lead to "spagetti code" with overuse!

What do you guys think? Shall we picket outside the PocketC author's house until we get it? :)

Wossname :)
Wossname :)
Are you <i>local</i>?
wossname
 
Posts: 275
Joined: Sat Feb 24, 2001 7:18 am

Postby gjperker on Tue May 15, 2001 9:37 pm

I had a programming professor who would have thrown you out of class, through a window, for saying that.

TOP DOWN PROGRAMMING!! Goto was forbiden.

But then again, that's just one guy.

Greg Perkowski
greg@perkowski.net
Greg Perkowski
greg@perkowski.net

The code is out there.
Follow the code Luke....
Beam me up, there's no intelligent code here.
gjperker
 
Posts: 8
Joined: Fri May 04, 2001 12:34 am
Location: USA

Postby SimeonB on Wed May 16, 2001 5:13 am

Funnily enough, after I showed a programmer chum of mine the recursion solution, he "tut-tutted" and said exactly the same as Wolfgang - that many instances would be created in memory if you said "no" enough times. He also recommended a loop (he showed me how to do it in a slightly different way than Wolfgang, but I include it for reference). I don't supposse one way is better than the other, just different:

// Test 2

main() {
int myInteger;
int amhappy;

amhappy=0;
while (amhappy=0)
{
myInteger = gets("Enter a number: ");
myhappy = confirm("Are you sure? Happy with " + myInteger);
if myhappy=0 alert("Try again");
}
alert("Your number is: " + myInteger);
}

As for GOTO's, my old programming tutor would have said exactly the same thing Greg!

Simeon
SimeonB
 
Posts: 4
Joined: Tue May 15, 2001 12:13 pm

Postby wossname on Wed May 16, 2001 3:33 pm

I know, I know! :)

I was brought up with Basic programming (various platforms, from AtariST to TI to PalmOS to QuickBasic to Visual Basic (which I worship!)) but I hardly ever used goto, because it is easy to just create more sub's when needed (gosub) but with PocketC its a little more difficult due to the memory restrictions. What I mean is that Memo's are a pain in the ass to write programs on.

I think I could count the number of times i have used GOTO in the last 6 months on the fingers of one hand.

But it would be suitable for PocketC in my opinion, since we are forced to shed some of our ingrained paradigms anyway!

I think the following should be added to Pocketc in future versions:
- Unsigned Byte/Integer/Long data types
- Instantly definable multi-dimension arrays:

int LikeThis[5]<img src=icon_smile_8ball.gif border=0 align=middle>;

- then I would be happy!

I think the PToolboxLib goes a long way to make up for the shortcomings of PocketC though. What do you guys think?

Wossname :)
Wossname :)
Are you <i>local</i>?
wossname
 
Posts: 275
Joined: Sat Feb 24, 2001 7:18 am


Return to PocketC for Palm OS

Who is online

Users browsing this forum: No registered users and 2 guests

cron