QBasic and QB64 Discussion Board

[QB Forum Archives (1999-2009)/ ] [QB FAQ] [QB Links and Downloads] [Subforums and Chat Room] [Search]

QB64.Net Homepage   QB/QB64 Keywords   QB Graphics Forum   Homework Policy



Simple DARN recursive circles fractal !

by (no login)

Beware, this is TRUE : All you wanted to know about recursivity without darning to ask, in a demo. Ye it is unbelievable.

I would be glad to see a *clear* code for this with nested loops :) Or anything ITERATIVE.

Does QB64 run recursive code ?

And don't run this with n = 32, this ends never on my P III 750 Mhz, while n = 16 is still ok.

=============== CODE =================

DECLARE SUB CIRCLES (X, Y, R, C, n)
COMMON SHARED n, X, Y, R, C

SCREEN 12

n = 16

X = 320
Y = 240
R = 220
C = 1

PSET (X, Y)
CIRCLE (X, Y), R, C

CALL CIRCLES(X, Y, R, C, n)

a$ = INPUT$(1)

SYSTEM
END

SUB CIRCLES (X, Y, R, C, n)

IF n <= 0 THEN EXIT SUB

CIRCLE (X, Y), R, C

CALL CIRCLES(X - (R / 2), Y, (R / 2), 1, n - 1)

CALL CIRCLES(X + (R / 2), Y, (R / 2), 5, n - 1)

END SUB

Posted on Sep 28, 2010, 8:14 AM

Respond to this message   

Return to Index


Re: Simple DARN recursive circles fractal !

by Anonymous (no login)

http://www.network54.com/Forum/648955/message/1252828047/This+method+will+be+hard+to+better---

Posted on Sep 28, 2010, 8:30 AM

Respond to this message   

Return to Index


Sorry but this is not the spirit of recursion ;-)

by Lisztfr (no login)

Is recursion only the fact of calling one's self ? no... In that case it can be replaced by a loop.

I tried something like this before posting my code, like here : the counter was put inside the sub. This didn't make any sens.

My code here is a level beyond that :-) :

1) the counter is put inside the set of variables called by the sub (n)
2) the sub calls itself twice.

In fact circles are draw in 2 ^ n times. The recursion is true, it's not a loop... each time we have x 2 more circles. This is exponential calling.

If a function is calling it self, it also like f(n - 1).




Posted on Sep 28, 2010, 8:48 AM

Respond to this message   

Return to Index


*Read about the Ackermann function

by (Login Mikrondel)
Moderator

Posted on Oct 1, 2010, 8:57 PM

Respond to this message   

Return to Index


Triangle fractal recursiv

by (no login)

DECLARE SUB TRI (X, BY, B, H, C, n)

COMMON SHARED B, BY, C

SCREEN 12

n = 8

CONST EQ = .8860254#
B = 200 ' half edge
H = (B * 2 * EQ) ' high
X = 320 ' Sommet 0
C = 15 ' Color
BY = 480 - ((480 - (B * 2 * EQ)) / 2) ' Base line

' coordinates :

'BL = (320 - B, BY) ' base left
'BR = (320 + B, BY)
'S = (320, BY - (B * 2 * .866)) ' peak

'PSET (320, 240)

LINE (X - B, BY)-(X + B, BY), C
LINE -(X, BY - H), C
LINE -(X - B, BY), C

'Upside Down :

'S = (320, BY)
'RU = (320 + B / 2, BY - ((B * 2 * EQ)) / 2)
'LU = (320 - B / 2, BY - ((B * 2 * EQ)) / 2)

LINE (X, BY)-(X + B / 2, BY - H / 2), C
LINE -(X - B / 2, BY - H / 2), C
LINE -(X, BY), C

CALL TRI(X, BY, B, H, C, n)

a$ = INPUT$(1)

SYSTEM

END

SUB TRI (X, BY, B, H, C, n)

IF n <= 1 THEN EXIT SUB

LINE (X, BY)-(X + B / 2, BY - H / 2), C
LINE -(X - B / 2, BY - H / 2), C
LINE -(X, BY), C

CALL TRI(X - B / 2, BY, B / 2, H / 2, C, n - 1)
CALL TRI(X + B / 2, BY, B / 2, H / 2, C, n - 1)

CALL TRI(X, BY - H / 2, B / 2, H / 2, C, n - 1)

END SUB

Posted on Sep 28, 2010, 12:19 PM

Respond to this message   

Return to Index


Beautiful! ...

by (Login qb432l)
R

The triangles remind me of lace or jewelry -- btw, both the circle and triangle examples run fine in QB64 -- and are completed instantly!

-Bob

P.S. I'd hate to have to guess the number of triangles on the latest example!

Posted on Sep 28, 2010, 1:47 PM

Respond to this message   

Return to Index


Colors

by (no login)

Have you notice one can tweak the colors ? like here :

CALL TRI(X - B / 2, BY, B / 2, H / 2, 1, n - 1)
CALL TRI(X + B / 2, BY, B / 2, H / 2, 2, n - 1)

CALL TRI(X, BY - H / 2, B / 2, H / 2, 3, n - 1)

Replace C with any color code.

It is then really nice. But i'm much more amazed by beauty of the code : Only 3 lines to draw a fractal, easy readable code, working like a charm !

The number of triangles grows by a factor 3 with n :)

The total number.. ok.

I'm just wondering if i can do the arithmetic challenge i posted few days ago, with recursivity...




Posted on Sep 28, 2010, 2:13 PM

Respond to this message   

Return to Index


*The colors thing is great, and yes, I should have commented on the code -- very elegant!

by (Login qb432l)
R

*

Posted on Sep 28, 2010, 7:53 PM

Respond to this message   

Return to Index


*Thanks, but not running in XP ?

by Lisztfr (no login)

*

Posted on Sep 29, 2010, 12:14 PM

Respond to this message   

Return to Index


I have Windows 7...

by (Login qb432l)
R

I tried it under DOSBox and it ran fine -- a bit slower than QB64 but fine. Like I said, it's instantaneous in QB64.

Not sure why it won't run in XP. Do other graphics programs run? I've run QBasic graphics programs in XP 32-bit just fine. XP 64-bit, they won't work and you must use a DOS emulator like DOSBox. Fortunately, with QB64 those days are behind me.

-Bob

Posted on Sep 29, 2010, 6:00 PM

Respond to this message   

Return to Index


Vesa stuff .. ?

by Lisztfr (no login)

I guess it's an XP problem like here, and maybe it was Clippy who made that "patch" ? (bugerXXXX)

=> http://www.qbasiiicstation.com/index.php?c=p_member&downloadid=443

i didn't try it yet... it's my dad's PC.

Posted on Sep 30, 2010, 3:43 AM

Respond to this message   

Return to Index


I didn't make the patch. Found it at Phatcode.net.

by (Login burger2227)
R

Another fix is to copy Autoexec.NT found in Repair folder if QB won't run correctly:

COPY C:\Windows\Repair\AutoExec.nt C:\Windows\System32

Just make a BAT file or run from command line. The file in the Repair folder is an original copy. As are the other files in that folder.

Posted on Sep 30, 2010, 9:42 AM

Respond to this message   

Return to Index


Micro$oft's goal is to eventually create operating systems that any idiot can run...

by (Login The-Universe)
Admin


But for now we're content to create operating systems that any idiot can own.

Posted on Sep 30, 2010, 8:07 PM

Respond to this message   

Return to Index


*I would love to see how many they would sell, if they didn't come bundled with new comps!

by (Login qb432l)
R

*

Posted on Oct 1, 2010, 11:55 AM

Respond to this message   

Return to Index


on error goto

by (no login)

I'm trying to delete some .tmp files from one directory, everything is ok but when there is no file i got an error, how I can skip that? I tried this.

on error goto salir
kill ("C:\test\*.tmp")
print "Files Deleted"
sleep 3
end
salir:
print "There are no files to delete"
on error goto 0
resume next
end

Posted on Sep 27, 2010, 12:37 PM

Respond to this message   

Return to Index


on error goto 0 ??

by Lisztfr (no login)

What for ? i would delet that.

Posted on Sep 27, 2010, 1:52 PM

Respond to this message   

Return to Index


Deleted

by Anonymous (no login)

Ok, deleted, the main problem is: when I try to compile my .bas the compiler says

warning errors 0
severe errors 2

on error goto salida
^missing on error (/E) option
resume next
^ Missing resume next (/X)

Posted on Sep 27, 2010, 2:03 PM

Respond to this message   

Return to Index


Use those options when compiling with BC.exe

by (Login burger2227)
R

SHELL "BC MODULE1.BAS /O/S/E/X;"

or command line: BC MODULE1.BAS /O/S/E/X;

Posted on Sep 27, 2010, 2:51 PM

Respond to this message   

Return to Index


Open each FOR APPEND before killing them

by (Login burger2227)
R

Open each file name with APPEND. It will create files that don't exist.

OPEN file$ FOR APPEND AS #1
CLOSE #1
KILL file$

WHY are you trying to kill non-existant files? That's silly!

Posted on Sep 27, 2010, 3:00 PM

Respond to this message   

Return to Index


Re: Open each FOR APPEND before killing them

by Lenfras (no login)

I don't want to kill non-existant files, but what happen if I run my .exe one time? everything is good right? what about twice? that's the error I don't want, I tried to skip error the way I showed you but now the problem is compiling .bas, Ok, I will follow up your advice, I create at least one file before kill.

thank you very much to all of you, and sorry for my english!!

Posted on Sep 29, 2010, 12:46 PM

Respond to this message   

Return to Index


Unfortunately you gotta OPEN them to check em

by (Login burger2227)
R

You don't want to OPEN FOR INPUT ...error if not exist!

OPEN FOR OUTPUT blanks the file, so APPEND, BINARY or RANDOM are the only safe modes to use if you want to see if they exist.

THAT creates a file if it DID NOT exist, but you are gonna kill them anyhow.

WHAT would you suggest and if so, WHY ASK US?????????

WARNING! Using KILL *. ANYTHING is NOT a good idea unless you KNOW what you are doing! YOU DON'T APPARENTLY!

Posted on Sep 29, 2010, 3:24 PM

Respond to this message   

Return to Index


I would change the code a bit, too...

by (Login The-Universe)
Admin


on error goto salir
kill ("C:\test\*.tmp")
on error goto 0
of er1 = 0 the print "Files Deleted" else print "There are no files to delete"
sleep 3
end

salir:
er1 = ERR
resume next
end

---------------------------------

Pete

PS BC with compiler switches /O/S/E/X; For some reason, I never forget them. happy.gif If you auto-compile with QB, they will be automatically added when the compiler detects the error handler statement.

PPS Clippy uses them too, but he compiles by hand. :O

Posted on Sep 28, 2010, 12:00 AM

Respond to this message   

Return to Index


Correct way to check for file existence:

by Solitaire (Login Solitaire1)
S

There are two different approaches. The first is by using an error handler:

CLS
flag = 0
INPUT "Enter filename: ", filenom$
ON ERROR GOTO check
OPEN filenom$ FOR INPUT AS #1
IF flag = 1 THEN
    PRINT "File does not exist."
    flag = 0
ELSE
    PRINT "File "; filenom$; "exists."
    CLOSE #1
END IF
END

check:
flag = 1
RESUME NEXT

========================================================================

The second way is much simpler. However, if the file exists but is empty, it will be deleted:

CLS
INPUT "Enter file name: ", filenom$
OPEN filenom$ FOR APPEND AS #1
N = LOF(1)
CLOSE #1
IF N = 0 THEN
    PRINT "File does not exist"
    KILL filenom$
ELSE
    PRINT "File "; filenom$; exists; ""
END IF
END

Posted on Sep 28, 2010, 12:09 PM

Respond to this message   

Return to Index


* ? exists; "" :-)

by Clippy (Login burger2227)
R

Posted on Sep 29, 2010, 9:25 PM

Respond to this message   

Return to Index


access qbasic

by (no login)

I have windows xp. I am assuming that I have qbasic installed as part of windows. I bought the book qbasic for dummies. It tells me to enter qbasic.exe in the directory, but I don't know how to get to the directory. Thank you for your help.

Posted on Sep 26, 2010, 8:32 AM

Respond to this message   

Return to Index


Wrong

by Lisztfr (no login)

False assuming

Then install Locate32.

Posted on Sep 26, 2010, 8:51 AM

Respond to this message   

Return to Index


Don't assume anything.

by Solitaire (no login)

QBasic is NOT installed with Windows, never was. It came with MS-DOS version 5. It's a DOS program and is no longer compatible with the newer versions of Windows.

You need to download QBasic and install it yourself, in a directory of your choosing. This program is text-based. You must be familiar with the Command prompt and DOS commands in order to proceed with installation.

You can get to the Command prompt in Windows XP by clicking Start in the bottom left corner of the desktop. Then click Run and type CMD. At this point you need to start entering DOS commands. To exit the Command window, type EXIT and press the Enter key.

Note: A DOS directory is equivalent to a Windows folder. You can bypass the Command prompt window by copying the QBasic program to a folder. In order to run the program, you need to click on the QB.EXE file. This is NOT a regular Windows installation. All DOS programs are simply copied to a folder/directory and run directly from the EXE file.


Posted on Sep 26, 2010, 10:33 AM

Respond to this message   

Return to Index


Or

by unseen Machine (no login)

Download QB64 - QBasic for modern operating systems. www.qb64.net

Theres a active forum where you can ask any questions you want, and QB64 makes doing a lot of stuff a lot easier. It can play mp3s, load images with one command and even use TCP/IP!! There are loads of demos and programs on the forum, have a look around ask anything you want, and have fun coding!!!

Posted on Sep 26, 2010, 11:57 AM

Respond to this message   

Return to Index


Random Number Help

by Bob (no login)

I need help writing random numbers. It must calculate a random number 1-18 and the outputs all numbers that add up to that number. Example: if a random number is 5 the output should look like:

1
2
3
4
5


Sorry for the amateur question but I've just started QB. Thanks for the help!

Posted on Sep 25, 2010, 8:07 PM

Respond to this message   

Return to Index


* Read the Homework link above. No code, no help!

by (Login burger2227)
R

Posted on Sep 25, 2010, 9:36 PM

Respond to this message   

Return to Index


Look up

by unseenmachine (no login)

Firstly, clippys right, i cant give you code, however i can point you in the right direction.

Look in the wiki, or qb helpfile if your using original qbasic for the following

RND
RANDOMIZE
TIMER

For the counting up to the number bit, use could use a FOR loop. Start at 1 and loop until your random number is reached.

Have a go and post your code, then i can help more.

Posted on Sep 25, 2010, 9:56 PM

Respond to this message   

Return to Index


Some nice fractal

by (no login)

' DECLARE SUB TRI 'later...

SCREEN 12


CONST EQ = .8860254#
COT = 200 ' half edge
BY = 480 - ((480 - (COT * 2 * EQ)) / 2) ' Base line

' coordinates :

'BL = (320 - COT, BY) ' base left
'BR = (320 + COT, BY)
'S = (320, BY - (COT * 2 * .866)) ' peak

'PSET (320, 240)

LINE (320 - COT, BY)-(320 + COT, BY)
LINE -(320, BY - (COT * 2 * EQ))
LINE -(320 - COT, BY)

'Upside Down :

'S = (320, BY)
'RU = (320 + COT / 2, BY - ((COT * 2 * EQ)) / 2)
'LU = (320 - COT / 2, BY - ((COT * 2 * EQ)) / 2)

LINE (320, BY)-(320 + COT / 2, BY - ((COT * 2 * EQ)) / 2)
LINE -(320 - COT / 2, BY - ((COT * 2 * EQ)) / 2)
LINE -(320, BY)

' Low left :

LINE (320 - COT / 2, BY)-(320 - COT / 4, BY - ((COT * 2 * EQ)) / 4)
LINE -(320 - (COT / 4) * 3, BY - ((COT * 2 * EQ)) / 4)
LINE -(320 - COT / 2, BY)

' Low right :

LINE (320 - COT / 2 + COT, BY)-(320 - COT / 4 + COT, BY - ((COT * 2 * EQ)) / 4)
LINE -(320 - (COT / 4) * 3 + COT, BY - ((COT * 2 * EQ)) / 4)
LINE -(320 - COT / 2 + COT, BY)

' Up :

LINE (320 - COT / 2 + (COT / 2), BY - ((COT * 2 * EQ)) / 2)-(320 - COT / 4 + (COT / 2), BY - ((COT * 2 * EQ)) / 4 - ((COT * 2 * EQ)) / 2)
LINE -(320 - (COT / 4) * 3 + (COT / 2), BY - ((COT * 2 * EQ)) / 4 - ((COT * 2 * EQ)) / 2)
LINE -(320 - COT / 2 + (COT / 2), BY - ((COT * 2 * EQ)) / 2)


END

=================

Hand made, - depth ? not much.

The goal is to increase the depth using a RECURS(ed)IVE SUB...












Posted on Sep 24, 2010, 12:28 PM

Respond to this message   

Return to Index


The Sorcerer's Apprentice

by Lisztfr (no login)

I searched for recursion in the archives also. It seems that it was not innvestigated as something different or special... But it is :

http://german.about.com/library/blgzauberl.htm


Now I'll throw myself upon you,
and the sharpness of my axe
I will test, o spirit, on you.
Well, a perfect hit!
See how he is split!
Now there's hope for me,
and I can breathe free!

Woe is me! Both pieces
come to life anew,
now, to do my bidding
I have servants two!
Help me, o great powers!
Please, I'm begging you!

And they're running! Wet and wetter
get the stairs, the rooms, the hall!
What a deluge! What a flood!
Lord and master, hear my call!

CALL !

Posted on Sep 25, 2010, 8:04 AM

Respond to this message   

Return to Index


You've given new meaning to recursion...

by (Login qb432l)
R

Who knew that black magic was a capability in QBasic.

BTW, your fractal triangles look promising. The output reminds me of that riddle: "How many triangles do you see?".

-Bob

Posted on Sep 25, 2010, 8:32 AM

Respond to this message   

Return to Index


Sierpinsky recursiv

by (no login)

DECLARE SUB TRI (X, BY, B, H, C, n)

COMMON SHARED B, BY, C

SCREEN 12

n = 8

CONST EQ = .8860254#
B = 200 ' half edge
H = (B * 2 * EQ) ' high
X = 320 ' Sommet 0
C = 15 ' Color
BY = 480 - ((480 - (B * 2 * EQ)) / 2) ' Base line

' coordinates :

'BL = (320 - B, BY) ' base left
'BR = (320 + B, BY)
'S = (320, BY - (B * 2 * .866)) ' peak

'PSET (320, 240)

LINE (X - B, BY)-(X + B, BY), C
LINE -(X, BY - H), C
LINE -(X - B, BY), C

'Upside Down :

'S = (320, BY)
'RU = (320 + B / 2, BY - ((B * 2 * EQ)) / 2)
'LU = (320 - B / 2, BY - ((B * 2 * EQ)) / 2)

LINE (X, BY)-(X + B / 2, BY - H / 2), C
LINE -(X - B / 2, BY - H / 2), C
LINE -(X, BY), C

CALL TRI(X, BY, B, H, C, n)

a$ = INPUT$(1)

SYSTEM

END

SUB TRI (X, BY, B, H, C, n)

IF n <= 1 THEN EXIT SUB

LINE (X, BY)-(X + B / 2, BY - H / 2), C
LINE -(X - B / 2, BY - H / 2), C
LINE -(X, BY), C

CALL TRI(X - B / 2, BY, B / 2, H / 2, C, n - 1)
CALL TRI(X + B / 2, BY, B / 2, H / 2, C, n - 1)

CALL TRI(X, BY - H / 2, B / 2, H / 2, C, n - 1)

END SUB

Posted on Sep 28, 2010, 12:18 PM

Respond to this message   

Return to Index


*That's Sierpiński to you.

by (Login Mikrondel)
Moderator

Posted on Sep 30, 2010, 6:05 PM

Respond to this message   

Return to Index


* I did not know that Australians used oomlauts! :-P

by Clippy (Login burger2227)
R

Posted on Sep 30, 2010, 6:08 PM

Respond to this message   

Return to Index


* Tak, rozumiem, miniej więcej

by Lisztfr (no login)

*

Posted on Oct 1, 2010, 3:24 PM

Respond to this message   

Return to Index


* It is a fractal. Looking like Sierpinki's, very close.

by Lisztfr (no login)

*

Posted on Oct 1, 2010, 3:28 PM

Respond to this message   

Return to Index


problem with GOTO commmand inside SUB

by YanR (no login)

hello ppl,

i'll make it short
i use a QBX 7.1 qbasic compiler.
and i'm programming a progam that need's to
open a sub routine inspect which button pressed or mouse
and go to the defined choosen label outside of the sub!
in the main program area.

but when i type let's say "goto NEW" new is name of the label
inside the sub it gives me "error 8" label not defined something
i must add that label "NEW" is located outside of the sub in
the main program area.

i need to go to the label "new" from inside of the sub
pl'z help

YanR,
10x.

Posted on Sep 23, 2010, 12:26 PM

Respond to this message   

Return to Index


GOTO

by Lisztfr (no login)

You have to exit the sub and return before the label, with a flag for example to tell the main what to do. At least in QB 4,5.

Or put the code you want to run in the main into another SUB and call it from your SUB.


Posted on Sep 23, 2010, 12:45 PM

Respond to this message   

Return to Index


GOTO inside SUB

by YanR (no login)

I don't quite get it, if i exit the sub
it will exit to commands under the sub.

and what is a flag and how do i put it?

i can't put it into anthor sub it's a big program,
with lot's of integers and strings.

please show QB code with example next time, if u can.

is there maybe a command to make the label shared or something like that?

YanR
10x

Posted on Sep 23, 2010, 12:57 PM

Respond to this message   

Return to Index


2 cents..

by Lisztfr (no login)

If someone can tell better ?

Please look at Buffer (TXT1$). The only way i have to go to Start0 label i want to, is to wait for the SUB has returned from where it was called, line 3. In QB 4.5 i don't know an other way ! But after RETURNed, i check the flag (line 4, 5) and then from this place i can GOTO the label...

IF PRINTX > linelength% THEN
GOSUB LineCut
Buffer (TXT1$)
IF flags% = 1 THEN GOTO Start0 'new start after search routin
IF flagtf% = 1 THEN GOTO Start0
GOTO PRINTALL
ELSE
Buffer (linebuf$)
IF flags% = 1 THEN GOTO Start0 'same
IF flagtf% = 1 THEN GOTO Start0
END IF

Posted on Sep 23, 2010, 2:13 PM

Respond to this message   

Return to Index


even better dont use GOTO

by Unseen Machine (no login)

It is old and outdated, and viewed by most as bad coding practice. It makes it hard for others to read your code, and can make it difficult in debugging. Try using a main do loop with triggered calls to subs that perform specific tasks. Other than that i cant help sorry, but best of luck.

Posted on Sep 23, 2010, 7:26 PM

Respond to this message   

Return to Index


Sure

by Lisztfr (no login)

I know other bad practices :

-Nested loops, :)
-No comments :)

To prevent people accidentally understanding your code... Same as the Hight Priest in ancient Egypt : The secret must be kept away from profane.




Posted on Sep 24, 2010, 12:55 AM

Respond to this message   

Return to Index


Nested loops are necessary,

by Solitaire (no login)

especially when working with arrays, or validating data within a loop.

Posted on Sep 26, 2010, 3:33 PM

Respond to this message   

Return to Index


* Another thing to avoid...being French! Nasty people! :-P

by (Login burger2227)
R

Posted on Sep 26, 2010, 8:38 PM

Respond to this message   

Return to Index


Please help with looping

by Luke (no login)

hey guys, I'm really confused about qbasic. I need to learn how to loop, and in doing so create a line of circles going across the middle of the screen. I have no idea how to do this, can anyone help me?

Posted on Sep 22, 2010, 10:02 PM

Respond to this message   

Return to Index


* Homework? Post what you have done so far.

by Clippy (Login burger2227)
R

Posted on Sep 22, 2010, 10:29 PM

Respond to this message   

Return to Index


Loop demo

by Unseen Machine (no login)

First, you should post your code. Second here's a Do style loop that exits when you press the Esc key

DO:LOOP UNTIL INKEY$ = CHR$(27)

if you need to do something a set number of times you can use a FOR loop

for i = 1 to 100
print i
next

will print 1 to 100 on the screen

in older basic you could (qbasic supports it)

i = 0
while i < 100
i = i + 1
print i
wend

this will also print 1 to 100 on the screen

for the drawing of circles, you will need to be in a graphics display mode (any screen mode above 0) and know how to draw circles. Look up CIRCLE, PSET, DRAW, and PAINT all available in the qb64 wiki http://qb64.net/wiki/index.php?title=Main_Page , or if your using original qbasic, press shift and F1 and use the index.

Posted on Sep 23, 2010, 7:19 PM

Respond to this message   

Return to Index


Stupid program

by Lisztfr (no login)

All what i do with recursion is like looping, nothing else. Can anyone actually write a graphical program using recursion, where it's usefull ?

DECLARE SUB RPSET (z%, y%)

SCREEN 12

Z% = 6
y% = 5

CALL RPSET(z%, y%)

END

SUB RPSET (z%, y%)

IF z% = 0 THEN EXIT SUB

PSET (z% * 10, Y%)

CALL RPSET((z% - 1), y% + 10)

END SUB

====================

To draw with PSET i need number argument, but since i used PSET while recalling, PSET is not a numbre. The reverse is when i use FUNCTION

DECLARE FUNCTION sum (Z%)
COMMON SHARED s%

SCREEN 12

Z% = 6

s% = sum(Z%)

' PRINT s%

END

FUNCTION sum (Z%)

IF Z% = 0 THEN EXIT FUNCTION

PSET (sum(Z% - 1) + Z%, 10)

END FUNCTION

Posted on Sep 22, 2010, 12:56 PM

Respond to this message   

Return to Index


"For every recursive algorithm, there is an iterative algorithm."

by (no login)

http://vb.mvps.org/hardcore/html/iterativequicksort.htm

Scott’s code proves two points. First, iterative algorithms aren’t necessarily faster than recursive ones. If you check the two versions in the TimeIt application, you’ll see that the recursive version wins by half a hair. But in a race that close you have to give the advantage to the code that uses the least resources. The iterative version gets the nod as the official SortArray procedure in the VBCore component because it can never run out of stack space.

The second point is that recursive algorithms can be a lot simpler than iterative ones. In fact, I think of the recursive version as documentation for the iterative version. You can read the recursive code and it makes sense. Not so the iterative code. I stepped through it and added comments that attempt to explain what’s going on, but I still don’t really understand the details. Mainly I just pray that it works.

Posted on Sep 23, 2010, 3:24 AM

Respond to this message   

Return to Index


Are you sure (*URL)

by qbguy (no login)

http://en.wikipedia.org/wiki/Ackermann_function

Posted on Sep 24, 2010, 6:45 AM

Respond to this message   

Return to Index


Yes I'm sure

by (Login Mikrondel)
Moderator

Ackermann function ain't primitive recursive but it can be implemented with a while loop and an array. The array might need to be quite large.

Posted on Oct 10, 2010, 1:08 AM

Respond to this message   

Return to Index


REAL RECURSIVITY (working example)

by Lisztfr (no login)

Sometimes my recursivity examples here was fake, just another way to loop : decreasing a variable was the only thing the call-it-self did.

Now, recursif means not only change a variable but also change the data segment on which it applies, and treat all of these subsequent segments...

So inside the SUB, the SUB needs to call itself more than one time, because that IS recursivity. Else it is a LOOP.

EXAMPLE : each time the following routine calls itself, z% increases by 1, so the routine was called 16 times (for n = 4), 2 ^ 4



======================================

DECLARE SUB ADD (n)
COMMON SHARED z%

n = 4
z% = 1

CALL ADD(n)

PRINT z%

END


SUB ADD (n)

IF n < 1 THEN EXIT SUB

z% = z% + 1

CALL ADD(n - 1)
CALL ADD(n - 1)

END SUB

Posted on Sep 23, 2010, 5:12 AM

Respond to this message   

Return to Index


No stack space issue ... !

by (no login)

DECLARE SUB ADD (n)
COMMON SHARED z&

n = 20
z& = 1

CALL ADD(n)

PRINT z&

END

SUB ADD (n)

IF n < 1 THEN EXIT SUB

z& = z& + 1

CALL ADD(n - 1)
CALL ADD(n - 1)

END SUB

Posted on Sep 23, 2010, 5:22 AM

Respond to this message   

Return to Index


* 3 ^ 4 ? not working...

by Lisztfr (no login)

*

Posted on Sep 23, 2010, 6:03 AM

Respond to this message   

Return to Index


How to compute cubics with that method ?

by (no login)

DECLARE SUB ADD (n)
COMMON SHARED z&

'SCREEN 12

n = 8
z& = 1

FOR i = 1 TO n
CALL ADD(i)
PRINT z&
z& = 1
NEXT i

END

SUB ADD (n)

IF n < 1 THEN EXIT SUB

z& = z& + 1

'PSET (n * 10, z& * 10)

CALL ADD(n - 1)
CALL ADD(n - 1)
'CALL ADD(n - 1)
'CALL ADD(n - 1)

END SUB

======================

After power of 2, for example uncomment one CALL statement will not produce a 3 ^ n...

Posted on Sep 23, 2010, 6:29 AM

Respond to this message   

Return to Index


Fibonacci in a SUB... !

by (no login)

DECLARE SUB ADD (n)
COMMON SHARED z&

'SCREEN 12

n = 8
z& = 1

FOR i = 1 TO n
CALL ADD(i)
PRINT z&
z& = 1
NEXT i

END

SUB ADD (n)

IF n < 1 THEN EXIT SUB

z& = z& + 1

'PSET (n * 10, z& * 10)


CALL ADD(n - 1)
CALL ADD(n - 2)


'CALL ADD(n - 1)
'CALL ADD(n - 1)

END SUB

=============================

But still want a cubic !

Posted on Sep 23, 2010, 6:44 AM

Respond to this message   

Return to Index


Recursive hell

by Lisztfr (no login)

I think nobody use it very much. But it feels smart and i hate to have a thing and can't do any thing uselful with it.


Posted on Sep 23, 2010, 12:49 PM

Respond to this message   

Return to Index


More stuff

by (no login)

Random programing : try to bring the most confusion possible in variables.


DECLARE SUB ADD (n, X, Y)
COMMON SHARED z&, s&, a&, b&, c&, n

SCREEN 12

n = 8
z& = 1

X = 320: Y = 1

FOR i = 0 TO 8
CALL ADD(i, X, Y)
' PRINT a&; b&; c&; z&
z& = 1

NEXT i

END

SUB ADD (n, X, Y)

IF n < 1 THEN
' n = 0: 'z& = z& - 1
EXIT SUB
END IF

z& = z& + 1

'LINE (1, n * 10)-(X * 10, Y * 10)

PSET (X * 10, Y * 10)


'IF Y < 400 THEN PSET (X, Y)

X = X / 2: Y = Y

CALL ADD(n - 1, X, Y): 'a& = a& + 1
CALL ADD(n - 1, Y + 10, X + 10): 'b& = b& + 1
CALL ADD(n - 1, X * Y, 20): 'c& = c& + 1


END SUB

Posted on Sep 24, 2010, 3:47 AM

Respond to this message   

Return to Index


Recursion unexpected

by (no login)

DECLARE SUB think (a)

CLS

a = 16

think (a)

END

SUB think (a)
PRINT a
IF a > 1 THEN
think (a / 2)
END IF
PRINT a
END SUB

===============================

16
8
4
2
1
1
2
4
8
16

Subroutine "think" first prints the variable it is passed. Then, if the value is greater than 1, it calls itself again, but passes the variable divided by 2. At the point where the value passed is 1, "think" prints "1", decides not to call itself again, prints "1" again, and then terminates. This terminataion returns program flow to the previous call, which is where the value passed was 2. "think" prints "2" again and terminates, and so on.

???

I understand why 2 will be printed since 2 was the previous value to 1, but when a = 2 then it should ot terminate but call it self again... Gosh. Will watch it step by step...



Posted on Sep 21, 2010, 1:50 AM

Respond to this message   

Return to Index


Floating point numbers do not exactly equal the corresponding integer value.

by Solitaire (no login)

The actual value of a floating point number is very slightly greater than indicated. For example, 1 may actually be 1.000000000000000000000001 or some such thing. Therefore, it is greater than 1.

I'm on my son's computer now so I can't test it myself, but try declaring a as an integer and see what happens.

Posted on Sep 21, 2010, 2:27 AM

Respond to this message   

Return to Index


Same thing with integer

by (no login)

I will try to replace the / 2 (divide) by a negative (-1) to see if there is a limit in the recursiveness

Posted on Sep 21, 2010, 3:37 AM

Respond to this message   

Return to Index


* Try using integer division \

by Solitaire (no login)

Posted on Sep 21, 2010, 8:49 AM

Respond to this message   

Return to Index


a = 80 : stack overflow

by (no login)

DECLARE SUB think (a)

DIM a AS INTEGER

CLS

a = 80

think (a)

END

SUB think (a)
PRINT a
IF a > 1 THEN
think (a - 1)
END IF
PRINT a
END SUB

Posted on Sep 21, 2010, 3:41 AM

Respond to this message   

Return to Index


You have to exit ALL recursions

by angros47 (no login)

DECLARE SUB think (a)

CLS

a = 16

think (a)

END

SUB think (a)
PRINT a
IF a > 1 THEN
think (a / 2)
END IF
PRINT a
END SUB


Let's see what happens:

a=16
think is called:

it does:


PRINT 16
IF 16 > 1 THEN
think 8
END IF
PRINT 16


think 8 means:

PRINT 8
IF 8 > 1 THEN
think 4
END IF
PRINT 8

So, actually "think 16" does:

PRINT 16
IF 16 > 1 THEN

'this is "think 8"
PRINT 8
IF 8 > 1 THEN
think 4
END IF
PRINT 8

' now we are back at "think 16"
END IF
PRINT 16


and so on.

Basically, when you exit from a SUB, you return to the calling procedure: and if you entered the same procedure many times (as you do in a recursion), you have to exit many times: and every time, the PRINT is executed.

To avoid it, you could do something like:

SUB think (a)
PRINT a
IF a > 1 THEN
think (a / 2)
ELSE
END 'Program will stop here.

END IF
PRINT a
END SUB

Posted on Sep 21, 2010, 2:11 PM

Respond to this message   

Return to Index


Function

by (no login)

Thanks, i understand it better now...

But your finale code doesn't "exit sub" ? My opinion so is far, there is no infinite recursion possible,, while calling a SUB, but also it's doesn't make so much sens.. This recursion effect seems more proper applied to FUNCTION. Since the variable is the function it self, in theory we need less stack space... ?

Posted on Sep 21, 2010, 2:45 PM

Respond to this message   

Return to Index


*the final command exit current sub, not all subs.

by angros47 (no login)

Posted on Sep 21, 2010, 2:53 PM

Respond to this message   

Return to Index


findspacce function i wrote some day

by Lisztfr (no login)

DECLARE FUNCTION findspace (aline$, numb%) 'find num% rank space in a string
COMMON SHARED num%

CLS
aline$ = "abc efg hij"
a% = INSTR(INSTR(aline$, " ") + 1, aline$, " ")
PRINT a%

SLEEP

num% = 2
PRINT findspace(aline$, num%)


END

FUNCTION findspace (aline$, num%)

IF num% = 0 THEN exit function

findspace = INSTR(findspace(aline$, num% - 1) + 1, aline$, " ")

END FUNCTION

Posted on Sep 22, 2010, 1:28 AM

Respond to this message   

Return to Index


Sum function

by (no login)

DECLARE FUNCTION sum (z%)
COMMON SHARED s%

Z% = 3

s% = sum(z%)

PRINT s%

END

FUNCTION sum (z%)

IF z% = 0 THEN EXIT FUNCTION

sum = sum(z% - 1) + z%

END FUNCTION

============

This is working since the function is like f(n) = f(n - 1) + Variable, but what about a function like f(n) depends on 2 previous states...

n - 1 and n - 2.



Posted on Sep 22, 2010, 5:43 AM

Respond to this message   

Return to Index


Or you could do

by C. F. Gauss (no login)

'Ligget se
FUNCTION sum(z%)
sum = (z%^2 + z%) / 2
END FUNCTION

Posted on Sep 22, 2010, 12:36 PM

Respond to this message   

Return to Index


Good example here

by (no login)

http://progopedia.com/implementation/qbasic/

DECLARE FUNCTION fibonacci (n)

FOR i = 1 TO 16:
PRINT fibonacci(i); ", "
NEXT i
PRINT "..."

FUNCTION fibonacci (n)
IF (n <= 2) THEN
fibonacci = 1
ELSE
fibonacci = fibonacci(n - 1) + fibonacci(n - 2)
END IF
END FUNCTION

===================

That's very nice. I really wonder how that is working, since the first call returns to the function call, so.. Will have a step-by-step look.

It's nice that the program looks like the math formula.





Posted on Sep 22, 2010, 2:11 PM

Respond to this message   

Return to Index


Strange n values

by (no login)

FUNCTION fibonacci (n)
PRINT n;
IF (n <= 2) THEN
fibonacci = 1
ELSE
fibonacci = fibonacci(n - 1) + fibonacci(n - 2)
END IF
END FUNCTION

I just added the PRINT n;

1 1 ,
2 1 ,
3 2 1 2 ,
4 3 2 1 2 3 ,
5 4 3 2 1 2 3 2 1 5 ,
6 5 4 3 2 1 2 3 2 1 4 3 2 1 2 8

Example, 6, decrease until 1, then return to the data flow because the Function ENDs, so we have 2 again then END, then 3. So it decrease again...when it reachs 1, now it goes directly to 4 (not used in previous loop).

I tried n = 50, that's very long.

It needs to do all that stuff..

This is for fibonacci = fibonacci(n - 1) + fibonacci(n - 2) + fibonacci (n - 3)

1 1 ,
2 1 ,
3 2 1 0 3 ,
4 3 2 1 0 2 1 5 ,
5 4 3 2 1 0 2 1 3 2 1 0 2 9 ,
6 5 4 3 2 1 0 2 1 3 2 1 0 2 4 3 2 1 0 2 1 3 2 1 0 17

hm... Well...

Posted on Sep 22, 2010, 2:51 PM

Respond to this message   

Return to Index


More Questions Regarding operators, data types indenting, etc...

by Negative (no login)

Thanks for the answers on the last post. After that I came across some more questions Google couldn't answer. =]

-Are AND/OR considered to be operators like +,-,*,/..?

-Is a Boolean a data type like strings, integers, etc.?

-Is an exponent/power (^) considered to be an arithmetic operator?

-In the below example everything inside the DO..LOOP is indented. Is there a certain name for commands that usually have things inside them indented? I know the indenting is not required, but usually the same commands like DO...LOOP, IF...ELSE, etc. get indented.
Example:
DO
PRINT "Hi"
PRINT "Hello"
LOOP

-Is there a way to input quotation marks inside strings (so they'll be shown on the output screen)?

-Arguments & Parameters: "Functions/Commands exchange information by means of parameters and arguments. A parameter refers to any declaration within the parentheses following the function name in a function declaration or definition; argument refers to any expression within the parentheses of a function call."
That quote is a little too hard to understand. What I got from it was: A parameter is anything in parenthesis (...) after a command; and an argument is any expression within parenthesis.
If my definition isn't right, what exactly is a parameter and argument, and what are their differences?

Posted on Sep 20, 2010, 3:10 PM

Respond to this message   

Return to Index


Some answers --

by Solitaire (Login Solitaire1)
S

There are several types of operators used in programming. The following are available in QBasic:

Math operators: +, -, *, /, \, Mod, ^

Relational operators: =, >, etc. (Note: This forum has trouble displaying the less-than symbol. There are 6 altogether.)

Logical operators: AND, OR, NOT, XOR, EQV, IMP

================================================================

QBasic does not have a separate Boolean type but Visual Basic does. The values of a Boolean type are either True or False. In QBasic, it's equivalent to the integer values of 0 (False) or -1 (True) or any non-zero number (True).

For example, if you run this code:
PRINT 4 > 5
The output will be 0.

PRINT 5 > 4
The output will be -1

=================================================================

A DO--LOOP is a structure block. Blocks use opening and closing statements and the contents inside the block are usually indented. In Visual Basic, the indentation of a structure block is automatic.

==================================================================

Quotation marks can be shown inside a string by using the ASCII value.
For example:

PRINT "He said, "; CHR$(34); "Hello."; CHR$(34)

====================================================================

An argument is an expression placed within parentheses following a call from the main program (or it could even be a call from a sub). The expression can be either a variable, a literal value, or a more complicated expression including an operator. A list of several arguments uses a comma separator. For example:

DIM first AS STRING, age AS INTEGER
first = "Joe"
age = 21
CALL mysub(first, age, age + 1)

A parameter is a variable name placed within parentheses in the sub that was called. It can use only variables and must include the variable type. A list of several parameters uses a comma separator. If an expression is used in the argument list, it must be replaced by a variable in the parameter list. For example:

SUB mysub (first AS STRING, age AS INTEGER, year AS INTEGER)
PRINT "Your name is "; first; " and your age is"; age
PRINT "Next year you will be"; year
END SUB

It is very important that the argument list and parameter list correspond in number, order, and type. The name of the variables in the parameter list is not important as long as they correspond. You can use different variable names in the parameter list and it will still work. For example:

SUB mysub (nom AS STRING, old AS INTEGER, year AS INTEGER)
PRINT "Your name is "; nom; " and your age is"; old
PRINT "Next year you will be"; year
END SUB

Note that the parameter list must include only variables, but the argument list may also include literal values or expressions.

Also note that as soon as you save a program with subs, the DECLARE statement automatically appears at the top of the main program. This statement includes the "signature," which is the complete parameter list. You never need to type in the declare statement manually.

=========================================================================

I hope this answers your questions.



Posted on Sep 20, 2010, 4:03 PM

Respond to this message   

Return to Index


Please Help Me :(

by (no login)

I am begginer with Qbasic,and I need to do this program(donno how) can you please help me :(


- Generate 20 random numbers(from 10-99)
- show odd and even numbers
- recommit odd and recommit even numbers
- show sum of odd and even number
- show then in file

Posted on Sep 20, 2010, 11:43 AM

Respond to this message   

Return to Index


Warning - Can not post this message. Please login and try again.

by (no login)

WTF ?

Posted on Sep 20, 2010, 12:04 PM

Respond to this message   

Return to Index


* READ the Homework Policy in the header! Post your code!

by Clippy (Login burger2227)
R

Posted on Sep 20, 2010, 1:39 PM

Respond to this message   

Return to Index


Draw a circle using only PSET and integers: no SIN/COS, no SQRT...

by angros47 (no login)

Here you are:

SCREEN 12

CONST R=100

DIM X AS INTEGER
DIM Y AS INTEGER
DIM Y2 AS INTEGER
DIM TMP AS INTEGER

FOR X=-R TO R
Y2=R*R-X*X
TMP=1 : Y=Y2
DO
TMP=(Y+TMP)/2
Y=Y2/TMP
LOOP UNTIL ABS(Y-TMP)<2
PSET (320+X,240+Y): PSET (320+X,240-Y):
NEXT
SLEEP

This program uses Pythagorean theorem (the radius of the circle is the hypotenuse, knowing it and X it calculates Y), and a trick to get square root, without using built-in functions.

It's completely useless, but writing it was fun.

Posted on Sep 19, 2010, 1:03 PM

Respond to this message   

Return to Index


Here's one from me

by Unseen Machine (no login)

SCREEN 12

FOR i = 1 TO 8
x = 0
FOR j = 1 TO 8
READ clr
IF clr = 1 THEN PSET (x, y), 15
x = x + 1
NEXT
y = y + 1
NEXT

DATA 0,0,0,0,0,0,0,0
DATA 0,0,1,1,1,1,0,0
DATA 0,1,0,0,0,0,1,0
DATA 0,1,0,0,0,0,1,0
DATA 0,1,0,0,0,0,1,0
DATA 0,1,0,0,0,0,1,0
DATA 0,0,1,1,1,1,0,0
DATA 0,0,0,0,0,0,0,0

Posted on Sep 19, 2010, 2:51 PM

Respond to this message   

Return to Index


Re: Draw a circle using only PSET and integers: no SIN/COS, no SQRT...

by Ben (no login)

x = 100 'radius
y = 0
d = -x

x1 = 100 'coordinates
y1 = 200

SCREEN 12
DO WHILE x > y
y = y + 1
d = d + y + y
IF d > 0 THEN
x = x - 1
d = d - x - x
END IF
PSET (x + x1, y + y1)
PSET (x + x1, -y + y1)
PSET (-x + x1, y + y1)
PSET (-x + x1, -y + y1)

PSET (y + x1, x + y1)
PSET (-y + x1, x + y1)
PSET (y + x1, -x + y1)
PSET (-y + x1, -x + y1)
LOOP

Posted on Sep 19, 2010, 2:51 PM

Respond to this message   

Return to Index


The Breshenham algorithm revisited

by David (no login)

Drawing a circle (or any other function) on the screen we are not trying for high accuracy analogue values. The trick is to determine which is the closest pixel to the function you want to represent so we can work with integers rather than floating point numbers.

All functions will at some point have gradients (grad<1) such that increasing x by one will cause y to either not change or also increase by one. All the algorithm has to do is to change the x value (in pset) by one and decide whether y should change (by one) or stay the same.

Note a circle also has high symmetry so that only one octant has to be calculated and the other octants and be generated by axes reflections.

Below is the challenge to draw 1000 (random sized filled) as quickly as possible without using "CIRCLE".



DEFINT A-S, U-Z
SCREEN 12
RANDOMIZE TIMER
tt = TIMER
m = 1
FOR jj = 1 TO 1000
xoff = INT(RND * 640): yoff = INT(RND * 480)
c = INT(RND * 16)
y = 2 + INT(RND * RND * RND * RND * 130)
d = 0
x = -1
WHILE x < y
x = x + m
d = d + x + x + m
IF d > 0 THEN d = d - y - y + m: y = y - m
FOR k = 0 TO 7
IF k AND 4 THEN a = -x ELSE a = x
IF k AND 2 THEN b = -y ELSE b = y
IF k AND 1 THEN SWAP a, b
q2 = a: q3 = 0
IF a < 0 THEN SWAP q2, q3
FOR q1 = q3 TO q2
PSET (q1 + xoff, b + yoff), c
PSET (640 - q1 - xoff, 480 - b - yoff), c
NEXT q1
NEXT k
WEND
NEXT jj
PRINT TIMER - tt

Posted on Sep 20, 2010, 4:40 AM

Respond to this message   

Return to Index


could you use LINE?

by Ben (no login)

Posted on Sep 20, 2010, 6:30 AM

Respond to this message   

Return to Index


Re: could you use LINE?

by Ben (no login)

RANDOMIZE TIMER
DEFINT A-Z

SCREEN 12

t! = TIMER
FOR i = 1 TO 1000
x = 2 + INT(RND * RND * RND * RND * 130) 'radius
y = -1
d = -x

x1 = INT(RND * 640) 'coordinates
y1 = INT(RND * 480)
COLOR INT(RND * 16)
DO WHILE x > y
y = y + 1
d = d + y + y

IF d > 0 THEN
x = x - 1
d = d - x - x
END IF
'PSET (x + x1, y + y1)
'PSET (x + x1, -y + y1)
'PSET (-x + x1, y + y1)
'PSET (-x + x1, -y + y1)
'PSET (y + x1, x + y1)
'PSET (-y + x1, x + y1)
'PSET (y + x1, -x + y1)
'PSET (-y + x1, -x + y1)
LINE (x + x1, -y + y1)-(-x + x1, -y + y1)
LINE (x + x1, y + y1)-(-x + x1, y + y1)
LINE (y + x1, x + y1)-(-y + x1, x + y1)
LINE (y + x1, -x + y1)-(-y + x1, -x + y1)
LOOP
NEXT
PRINT TIMER - t!


Posted on Sep 20, 2010, 8:53 AM

Respond to this message   

Return to Index


* What trick? There are no non-integer pixel positions!

by Clippy (Login burger2227)
R

Posted on Sep 20, 2010, 8:33 AM

Respond to this message   

Return to Index


More fun

by Lisztfr (no login)

DO
TMP=(Y+TMP)/2
Y=Y2/TMP
LOOP UNTIL ABS(Y-TMP)<2

This looks like it could be put in a recursive function...? (that call itself)






Posted on Sep 20, 2010, 11:43 AM

Respond to this message   

Return to Index


Another challenge

by Lisztfr (no login)

The knight challenge on a chess board. Make the knight go overe all the fieds once, and only once.

64 nested loops ?

Posted on Sep 15, 2010, 2:24 AM

Respond to this message   

Return to Index


*URL

by David (no login)

http://www.network54.com/Forum/202193/message/1206121178/Knight's+Tour

Posted on Sep 15, 2010, 4:09 AM

Respond to this message   

Return to Index


Heuristic in the knight tour

by Lisztfr (no login)

I'm affraid that in the knight's tour there isn't much heuristic, beside of that from the initial poster : The next step should offer the less moves.

Other variant : the 2nd next step should offer the less moves.

But for example, in 3 steps we should get far away from place 1, or, the board should remain center-weighted (each step = 1 unit weight) after 10 steps, or the surface of the 3-5 moves (draw on board) should be maximal, .. this all doesn't make sens.

Random search is silly, with backtracking is little better.

Posted on Sep 16, 2010, 1:45 AM

Respond to this message   

Return to Index


Using Warnsdorff's algorithm

by David (no login)

CLS
DIM q(-10 TO 18, -10 TO 18) AS INTEGER, z(65) AS INTEGER
DIM cx(9) AS INTEGER, cy(9) AS INTEGER, rx(9) AS INTEGER, ry(9) AS INTEGER
r = 4: c = 4 'starting point
q(r, c) = 1
FOR i = 1 TO 8
FOR k = 1 TO 8
LOCATE i, k * 2
PRINT CHR$(176) + CHR$(176);
NEXT k
NEXT i
LOCATE r, c * 2
PRINT CHR$(219) + CHR$(219);
num = 1
z(num) = r + (c - 1) * 8
q(r, c) = 1
WHILE num < 64
rx(1) = r - 1: cx(1) = c - 2
rx(2) = r + 1: cx(2) = c - 2
rx(3) = r - 1: cx(3) = c + 2
rx(4) = r + 1: cx(4) = c + 2
rx(5) = r - 2: cx(5) = c - 1
rx(6) = r + 2: cx(6) = c - 1
rx(7) = r - 2: cx(7) = c + 1
rx(8) = r + 2: cx(8) = c + 1
pmin = 20
FOR i = 1 TO 8
IF rx(i) > 0 AND rx(i) < 9 AND cx(i) > 0 AND cx(i) < 9 AND q(rx(i), cx(i)) = 0 THEN
ry(1) = rx(i) - 1: cy(1) = cx(i) - 2
ry(2) = rx(i) + 1: cy(2) = cx(i) - 2
ry(3) = rx(i) - 1: cy(3) = cx(i) + 2
ry(4) = rx(i) + 1: cy(4) = cx(i) + 2
ry(5) = rx(i) - 2: cy(5) = cx(i) - 1
ry(6) = rx(i) + 2: cy(6) = cx(i) - 1
ry(7) = rx(i) - 2: cy(7) = cx(i) + 1
ry(8) = rx(i) + 2: cy(8) = cx(i) + 1
'END IF
p = 0
FOR k = 1 TO 8
IF ry(k) > 0 AND ry(k) < 9 AND cy(k) > 0 AND cy(k) < 9 AND q(ry(k), cy(k)) = 0 THEN p = p + 1
NEXT k
IF p < pmin THEN pmin = p: imin = i
END IF
NEXT i
pmin = 20
num = num + 1
r = rx(imin): c = cx(imin)
z(num) = r + (c - 1) * 8
IF z(num) > 64 THEN PRINT "off the board": END
q(r, c) = 1
LOCATE r, c * 2
PRINT CHR$(219) + CHR$(219);
tt = TIMER
WHILE TIMER - tt < .2: WEND '*******DELAY*******
WEND
WHILE h$ = ""
h$ = INKEY$
WEND
LOCATE 10, 1
FOR i = 1 TO 64
PRINT USING "###"; z(i);
IF i MOD 8 = 0 THEN PRINT
NEXT i

Posted on Sep 16, 2010, 11:58 AM

Respond to this message   

Return to Index


Ok i will have to jump into that matter, soon i remember some qbasic stuff.

by Lisztfr (no login)

Something unexpected ?

28 11 1 18 3 9 26 41
58 43 33 50 60 54 64 47
32 15 5 20 10 25 35 52
62 56 39 24 7 22 37 27
17 2 12 6 16 31 48 63
53 59 49 34 44 61 46 29
14 8 23 38 21 4 19 13
30 40 55 45 51 36 42 57

"2" is next the 17

nice animation :=) (not reflecting this)

Posted on Sep 16, 2010, 3:31 PM

Respond to this message   

Return to Index


Wolfram has an article on this with example results

by Zom-B (Login Zom-B)
R

Posted on Sep 19, 2010, 4:37 AM

Respond to this message   

Return to Index


A challenge

by (no login)

A math problem my Dad told me today...

1, 2, 3, 4, 5, 6, 7, 8, 9

Between these numbers we can set 3 operators :

+, -, and nothing, then the combination is the 2 digit number.

Nothing means for example, 2 3 = 23

Also 123 is allowed, any combination. All numbers must be used one time, in the above order.

Example : 123 -4 + 56 -7 + 89

The result must be 100.

estimated possibilities :

3 ^ 9 : 19683


Posted on Sep 12, 2010, 11:55 AM

Respond to this message   

Return to Index


* WE DO have a CHALLENGE FORUM ya know....

by (Login burger2227)
R

LA Challenge Fooruum

Posted on Sep 12, 2010, 8:16 PM

Respond to this message   

Return to Index


* Dammit, N54 lost my name again!

by (Login burger2227)
R

Posted on Sep 12, 2010, 8:17 PM

Respond to this message   

Return to Index


* I bet Galleon wished he had a spiffy forum host like we do.

by (Login The-Universe)
Admin

Posted on Sep 12, 2010, 8:41 PM

Respond to this message   

Return to Index


Yes but who is looking at the challenge forum ... ?

by (no login)

Still not solved ? lol ...

Problem is to not omit some combination so, we need a scanning routine.

The sign in front of 1 is always "+", i forgot to tell.

So given the start position like all signs are "+".

Hum... this smells bad.

Feels like needing to build a tree. Or store some variable (hate that)

=================

The 2nd sign should take all 3 value, then again for the 3rd sing is taking all 3 values... How to keep that dam thing small ?





Posted on Sep 13, 2010, 3:47 AM

Respond to this message   

Return to Index


There are 3^8 combinations

by David (no login)

If I've done it correctly there are only 11 solutions

1 + 2 + 3 - 4 + 5 + 6 + 78 + 9 = 100
1 + 2 + 34 - 5 + 67 - 8 + 9 = 100
1 + 23 - 4 + 5 + 6 + 78 - 9 = 100
1 + 23 - 4 + 56 + 7 + 8 + 9 = 100
12 + 3 + 4 + 5 - 6 - 7 + 89 = 100
12 + 3 - 4 + 5 + 67 + 8 + 9 = 100
12 - 3 - 4 + 5 - 6 + 7 + 89 = 100
123 + 4 - 5 + 67 - 89 = 100
123 + 45 - 67 + 8 - 9 = 100
123 - 4 - 5 - 6 - 7 + 8 - 9 = 100
123 - 45 - 67 + 89 = 100

And yes we've had a similar challenge before (see October 2005).

Posted on Sep 13, 2010, 6:26 AM

Respond to this message   

Return to Index


This is correct

by Lisztfr (no login)

My Dad put the problem on CASTEM progam and also found only 11 solutions. CASTEM is a kind of language for ... yep ; Resistence of material. So this is evil as one can expext. It's like Qbasic with less possibilities, Qbasic less GOTO, mixed with... fortran's mind.

Posted on Sep 13, 2010, 6:53 AM

Respond to this message   

Return to Index


*Congratulations, David -- any chance of seeing your code?

by (Login qb432l)
R

*

Posted on Sep 13, 2010, 7:03 AM

Respond to this message   

Return to Index


The orginal challenge had / and * as well.

by David (no login)

DEFINT A-R, T-Z
DEFLNG S
DIM n(10) AS LONG
FOR i = 1 TO 9
nn(i) = i
NEXT i
FOR i1 = 1 TO 3
FOR i2 = 1 TO 3
FOR i3 = 1 TO 3
FOR i4 = 1 TO 3
FOR i5 = 1 TO 3
FOR i6 = 1 TO 3
FOR i7 = 1 TO 3
FOR i8 = 1 TO 3
z = z + 1
j(1) = i1: j(2) = i2: j(3) = i3: j(4) = i4
j(5) = i5: j(6) = i6: j(7) = i7: j(8) = i8
FOR k1 = 1 TO 9
n(k1) = nn(k1)
NEXT k1
m = 8
mm = m: k = 0
WHILE k < mm
k = k + 1
IF j(k) = 3 THEN
n(k) = n(k) * 10 + n(k + 1)
FOR k1 = k + 1 TO mm + 1
j(k1 - 1) = j(k1)
n(k1) = n(k1 + 1)
NEXT k1
m = m - 1
k = k - 1
END IF
WEND
sum = n(1)
FOR k = 1 TO m
IF j(k) = 1 THEN sum = sum + n(k + 1)
IF j(k) = 2 THEN sum = sum - n(k + 1)
NEXT k
IF sum = 100 THEN
FOR k3 = 1 TO m
IF n(k3) > 11 AND n(k3) < 100 THEN PRINT USING "##"; n(k3);
IF n(k3) < 11 THEN PRINT USING "#"; n(k3);
IF n(k3) > 100 THEN PRINT USING "###"; n(k3);
IF j(k3) = 1 THEN PRINT " + ";
IF j(k3) = 2 THEN PRINT " - ";
NEXT k3
PRINT n(k3); " = 100"
sum2 = sum2 + 1
END IF
NEXT i8
NEXT i7
NEXT i6
NEXT i5
NEXT i4
NEXT i3
NEXT i2
NEXT i1
PRINT : PRINT "number of solutions= "; sum2; " combinations= "; z

Posted on Sep 13, 2010, 8:16 AM

Respond to this message   

Return to Index


*Very nice! Thanks.

by (Login qb432l)
R

*

Posted on Sep 13, 2010, 1:33 PM

Respond to this message   

Return to Index


* Nice program, David. I think I actually remembered it from long ago.

by (Login The-Universe)
Admin

Posted on Sep 13, 2010, 12:00 PM

Respond to this message   

Return to Index


Very nice, foremost because there isn't a single comment !

by (no login)

1) Very good program.

2) I like nested loops.

Posted on Sep 13, 2010, 3:06 PM

Respond to this message   

Return to Index


* IF you had looked in the Challenge forum, you could have seen it!

by (Login burger2227)
R

Posted on Sep 13, 2010, 3:59 PM

Respond to this message   

Return to Index


Ok, but :

by Lisztfr (no login)

I will soon, promise

Is it possible to do it without nested loops, - i guess not.... So we are limited by the limitation of Qbasic. Imagine i would have complicated the prob with 100 numbers.

We need a Montecarlo algorithm, obviously.

Posted on Sep 14, 2010, 2:34 AM

Respond to this message   

Return to Index


without nested loops

by David (no login)

Hope you like the GOTO slipped in at the end to add to the confusion.
I think nested loops are better.


DEFINT A-R, T-Z
DEFLNG S
DIM n(10) AS LONG, j(10) AS INTEGER, jj(10) AS INTEGER, nn(10) AS INTEGER
FOR i = 1 TO 9
nn(i) = i
NEXT i
FOR i = 2 TO 8
j(i) = 1
NEXT i
20 j(1) = j(1) + 1
FOR i = 1 TO 8
IF j(i) = 4 THEN j(i) = 1: j(i + 1) = j(i + 1) + 1
jj(i) = j(i)
NEXT i
IF j(9) = 1 THEN 10
z = z + 1
FOR k1 = 1 TO 9
n(k1) = nn(k1)
NEXT k1
m = 8
mm = m: k = 0
WHILE k < mm
k = k + 1
IF jj(k) = 3 THEN
n(k) = n(k) * 10 + n(k + 1)
FOR k1 = k + 1 TO mm + 1
jj(k1 - 1) = jj(k1)
n(k1) = n(k1 + 1)
NEXT k1
m = m - 1
k = k - 1
END IF
WEND
sum = n(1)
FOR k = 1 TO m
IF jj(k) = 1 THEN sum = sum + n(k + 1)
IF jj(k) = 2 THEN sum = sum - n(k + 1)
NEXT k
IF sum = 100 THEN
FOR k3 = 1 TO m
IF n(k3) > 11 AND n(k3) < 100 THEN PRINT USING "##"; n(k3);
IF n(k3) < 11 THEN PRINT USING "#"; n(k3);
IF n(k3) > 100 THEN PRINT USING "###"; n(k3);
IF jj(k3) = 1 THEN PRINT " + ";
IF jj(k3) = 2 THEN PRINT " - ";
NEXT k3
PRINT n(k3); " = 100"
sum2 = sum2 + 1
END IF
GOTO 20
10 PRINT : PRINT "number of solutions= "; sum2; " combinations= "; z

Posted on Sep 14, 2010, 4:31 AM

Respond to this message   

Return to Index


Nested loop limit ?

by (no login)

Until 17 it's OK :

a& = 0

Z1 = TIMER

PRINT Z1

FOR i1 = 1 TO 3
FOR i2 = 1 TO 3
FOR i3 = 1 TO 3
FOR i4 = 1 TO 3
FOR i5 = 1 TO 3
FOR i6 = 1 TO 3
FOR i7 = 1 TO 3
FOR i8 = 1 TO 3
FOR i9 = 1 TO 3
FOR i10 = 1 TO 3
FOR i11 = 1 TO 3
FOR i12 = 1 TO 3
FOR i13 = 1 TO 3
FOR i14 = 1 TO 3
FOR i15 = 1 TO 3
FOR i16 = 1 TO 3
FOR i17 = 1 TO 3

a& = a& + 1

IF (a& MOD 1000) = 0 THEN PRINT a&


NEXT i17
NEXT i16
NEXT i15
NEXT i14
NEXT i13
NEXT i12
NEXT i11
NEXT i10
NEXT i9
NEXT i8
NEXT i7
NEXT i6
NEXT i5
NEXT i4
NEXT i3
NEXT i2
NEXT i1

PRINT a

Z2 = TIMER

PRINT Z1 - Z2


=======================================

Output, on my super fast 750 Mhz Pentium III, Dell's flagship (Latitude CPx with better cpu ):

129138000
129139000
129140000
0
-370.0313

Only about 6 mn 10 s

Posted on Sep 14, 2010, 5:40 AM

Respond to this message   

Return to Index


question about variable output...

by Sail (no login)

im just learning QB and i have an assignment due...

we have to write a program that figures out sales tax on a given item...
when it runs, if any amount that needs to be displayed ends in zero, ie 15.50 it will drop off the zero and display 15.5

i understand that the computer sees no reason to display the zero, and as it stands i will recieve full points for the assignment...but for asthetic reasons id like the zero to stay...any help would be appreciated

Posted on Sep 10, 2010, 10:12 PM

Respond to this message   

Return to Index


heres my code btw...

by Sail (no login)

'cis 115
'8 sep 10
'assignment 3

'the purpose of this program is to find the
'purchase price of an item with sales tax

CLS

PRINT " this is the Q Sales Tax Calculator"
PRINT "===================================="
PRINT ""
PRINT "cost of the item you are purchasing:"
PRINT ""
PRINT "what is the sales tax for your area:"
PRINT " please exclude the '%' sign"

LOCATE 4, 38
INPUT "", itemPrice
LOCATE 6, 38
INPUT "", salesTax

salesTax = salesTax * .01
total = itemPrice + (itemPrice * salesTax)
total = INT(total * 100 + .5) / 100

CLS
LOCATE 1, 1
PRINT "base cost of item:"
PRINT "sales tax in area:"
PRINT "sales tax on item:"
PRINT "item w/ sales tax:"

LOCATE 1, 19
PRINT INT(itemPrice * 100 + .5) / 100
LOCATE 2, 19
PRINT salesTax / .01; "%"
LOCATE 3, 19
PRINT INT((salesTax * itemPrice) * 100 + .5) / 100
LOCATE 4, 19
PRINT total

END

Posted on Sep 10, 2010, 10:28 PM

Respond to this message   

Return to Index


Re: question about variable output...

by codeguy (Login codeguy)
R

print using "###.##" will let you keep those aesthetic 0's

Posted on Sep 10, 2010, 11:30 PM

Respond to this message   

Return to Index


thanks...

by Sail (no login)

title says it all...

Posted on Sep 11, 2010, 3:55 AM

Respond to this message   

Return to Index

 Copyright © 1999-2014 Network54. All rights reserved.   Terms of Use   Privacy Statement