News:

Printed Amstrad Addict magazine announced, check it out here!

Main Menu

BASIC programming tips

Started by arnoldemu, 14:23, 22 May 09

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

AMSDOS

* Using the old Amstrad Languages :D   * with the Firmware :P
* I also like to problem solve code in BASIC :)   * And type-in Type-Ins! :D

Home Computing Weekly Programs
Popular Computing Weekly Programs
Your Computer Programs
Updated Other Program Links on Profile Page (Update April 16/15 phew!)
Programs for Turbo Pascal 3

Morri

Quote from: AMSDOS on 06:59, 12 March 17
5 DEFINT a-b
??
Not quite was I was meaning (but you are right). More like is there a BASIC command (i.e. MOD, XOR, AND etc...) that could do this sequence without the IF statements. I feel that is the section that slows the program down.
Keeping it Kiwi since 1977

remax

#77
perhaps use "x MOD y"



5 DEFINT a
10 a=1
20 a=a+1
30 PRINT a MOD 3+1
40 GOTO 20
Brain Radioactivity

ronaldo

Easiest way I can think of  :D
10 PRINT "1 2 3 2 ":GOTO 10


AMSDOS

#79
Quote from: Morri on 19:51, 12 March 17Not quite was I was meaning (but you are right). More like is there a BASIC command (i.e. MOD, XOR, AND etc...) that could do this sequence without the IF statements. I feel that is the section that slows the program down.
I've seen the functions MAX & MIN being used instead of IF statements to determine if a variable reaches a certain value. There's a little bit of information about it in Basically BASIC Amstrad Amstrad article in Issue 112. The idea there is MIN can be used so the variable never becomes larger than the number you want and MAX is used so the number never drops below the number you want.

Does that make sense?

Though the @remax example probably does the trick.  :D


Okay, so the example relax posted just goes 1 2 3 1 2 3 instead of 1 2 3 2 1 2 3. Using MAX & MIN there are a couple of ways of writing it, though it still involves IF statements and to be honest they are probably going to be slower than what you've written. So my examples look like this:



20 IF a=MAX(a,3) THEN b=-1
30 IF a=MIN(a,1) THEN b=1
40 PRINT a;
50 a=a+b



or:



20 IF MAX(a,3)=3 THEN b=-1
30 IF MIN(a,1)=1 THEN b=1



and so on...



Quote from: ronaldo on 21:05, 12 March 17
Easiest way I can think of 
10 PRINT "1 2 3 2 ":GOTO 10



It just needs a semi-column ";" then you will be in business.  :)
* Using the old Amstrad Languages :D   * with the Firmware :P
* I also like to problem solve code in BASIC :)   * And type-in Type-Ins! :D

Home Computing Weekly Programs
Popular Computing Weekly Programs
Your Computer Programs
Updated Other Program Links on Profile Page (Update April 16/15 phew!)
Programs for Turbo Pascal 3

remax

#80
My bad, i had read the goal a bit too fast...



5 DEFINT a
10 a=0
20 a=a MOD 4+1
30 PRINT 2-(8*a)/3+2*a*a-a*a*a/3
40 GOTO 20




:P
Brain Radioactivity

Urusergi

 The sequence without the IF statements... not sure if it's faster than yours

10 DEFINT a,b:a=3
20 DIM b(3):b(0)=1:b(1)=2:b(2)=3:b(3)=2
30 a=(a+1) MOD 4
40 PRINT b(a):GOTO 30

Urusergi

Quote from: remax on 22:35, 12 March 17
30 PRINT 2-(8*a)/3+2*a*a-a*a*a/3
a better approach...
30 PRINT ((a+2)*a*a+2) MOD 4

AMSDOS

The array approach is noticeably faster than the mathematical approach. I made some adjustments to @Urusergi routine to do a more complicated stringed approach to this problem:


10 DEFINT a:a=0
20 c$="1 2 3 2 "
30 a=a MOD 8+1
40 PRINT MID$(c$,a,1);:GOTO 30


Note the Spaces in Line 20 are important if you want the spacing right.  :laugh:
* Using the old Amstrad Languages :D   * with the Firmware :P
* I also like to problem solve code in BASIC :)   * And type-in Type-Ins! :D

Home Computing Weekly Programs
Popular Computing Weekly Programs
Your Computer Programs
Updated Other Program Links on Profile Page (Update April 16/15 phew!)
Programs for Turbo Pascal 3

Morri

Thanks for all the great answers guys. I knew there would be a formula to work this out.
I have gone for the combination of @remax and @Urusergi formula as it is the result I am after not the actual printing on the screen.

I must admit the array solutions were clever but of no use in what I am hoping to do.  :P
Keeping it Kiwi since 1977

mr_lou

Quote from: ZbyniuR on 22:02, 16 February 15
How to put machine code in Basic without FOR READ POKE NEXT DATA. Short and fast way, just in one PRINT. :D

Line 5 is need only once to generate 32 characters. Place them in quotes in PRINT in line 10. Now line 5 is no needed any more. Save and RUN and press any keys. ;)

This is LDIR demonstration. Working after reset or after SYMBOL AFTER 240. :)

Of course CAT is just tu show whatever on screen. Because CALL HIMEM+13 make copy from &c000 to &4000, and CALL HIMEM+1 opposite. :)

5 FOR a=1 TO 63 STEP 2:PRINT CHR$(1)CHR$(VAL("&"+MID$("19F02101401101C001D019F13FEDB0C92101C01119F2014001D03FEDB0C90432",a,2)));:NEXT:EDIT 10
10 PRINT"":CAT:CALL HIMEM+13:WHILE-1:CLS:CALL &BB18:CALL HIMEM+1:CALL &BB18:WEND

I'm rather intrigued by this technique.

Shouldn't it also be possible to achieve the same using variables?


code$="<the code string>"
address = peek(@code$+1)+peek(code$+2)*256
call address


The advantage of adding machine code this way is obvious, but what are the disadvantages (if any)?

roudoudou

Quote from: mr_lou on 12:39, 10 July 17The advantage of adding machine code this way is obvious, but what are the disadvantages (if any)?


this is specific to Amstrad, not compatible with all basic (or CPU)
My pronouns are RASM and ACE

mr_lou

Quote from: roudoudou on 14:14, 10 July 17
this is specific to Amstrad, not compatible with all basic (or CPU)

That's ok. I only have eyes for the CPC anyway.  ;)

ronaldo

Quote from: Morri on 00:58, 13 March 17
Thanks for all the great answers guys. I knew there would be a formula to work this out.
I have gone for the combination of @remax and @Urusergi formula as it is the result I am after not the actual printing on the screen.

I must admit the array solutions were clever but of no use in what I am hoping to do.  :P
Don't know what your exact goal is, but here you are 2 proposals that may be of interest:

10 DEFINT a-z
' Generate numbers consecutively
20 FOR i=1 to 3:?i:NEXT:?2:GOTO 20



10 DEFINT a-z
' initialize: i=generated num, m,n=generation parameters
20 i=1:m=3:n=2
30 GOSUB 100:?i:GOTO 30
' Generate next i number
100 i=i XOR m:m=m XOR n:n=n XOR 2:RETURN


Morri

Thanks @ronaldo
I like your use of the XOR to get the desired sequence.
I wanted something for an animation sequence (in BASIC) where I have 3 frames of animation but wanted frame 2 to be displayed every 2nd time. 1,2,3,2,1,2,3 etc...
I haven't ever had time to do anything with it though as I ended up doing other things instead.
Keeping it Kiwi since 1977

Morri

I have another question which I'm hoping is very simple.

I have a variable counting down from 100. I would like it to print as a 3 digit number therefore adding 0's in front if below 100.
i.e. 99 = 099 and 9 = 009.

I have tried using the PRINT USING command but can't get it to work.

Any ideas?
Keeping it Kiwi since 1977

mr_lou

Probably not the best solution, but....


10 for n=100 to 0 step -1
20 a$=right$("00"+mid$(str$(n),2),3)
30 print a$
40 next


AMSDOS

Quote from: Morri on 06:44, 30 July 17
I have another question which I'm hoping is very simple.

I have a variable counting down from 100. I would like it to print as a 3 digit number therefore adding 0's in front if below 100.
i.e. 99 = 099 and 9 = 009.

I have tried using the PRINT USING command but can't get it to work.

Any ideas?


I'm doing something like that with this program, though it's using the Condensed number set to count down.


So what I did was setup an Array n%(0) to n%(2) with the values, so in your case it would be n%(0)=1 n%(1)=0 n%(2)=0 the main guts of the program is Lines 610 to 710 to count down the values. To put those values from an the array into an integer Line 620 puts it into an Integer called Result, 630 to 650 converts the numbers from the Integer array, into a String Array and to impose the Condensed Number set I've added 220 to get the condensed set character.


But having said all that, PRINT USING I think is the proper approach (the reason I did the above related to my Pascal game and incorporating a Condensed Number counting system into it), unfortunately it's an technique I'm not too familiar with But if it can be done, then it would be somewhat faster than the approach I've used. The approach I used was also the technique used for my Silly Number Guessing Game.
* Using the old Amstrad Languages :D   * with the Firmware :P
* I also like to problem solve code in BASIC :)   * And type-in Type-Ins! :D

Home Computing Weekly Programs
Popular Computing Weekly Programs
Your Computer Programs
Updated Other Program Links on Profile Page (Update April 16/15 phew!)
Programs for Turbo Pascal 3

andycadley

Alas PRINT USING doesn't have any way of specifying leading zeroes in the format specifier, so you're best of with the MID$ option.

Urusergi

#94
Another arithmetic solution:

10 FOR i=100 TO 0 STEP -1
20 a$=HEX$(96*(i\100)+6*(i\10)+i,3)
30 PRINT a$
40 NEXT


If it's limited to 99 then we can delete the "96*(i\100)+" part

AMSDOS

Quote from: Morri on 01:45, 11 July 17
Thanks @ronaldo
I like your use of the XOR to get the desired sequence.
I wanted something for an animation sequence (in BASIC) where I have 3 frames of animation but wanted frame 2 to be displayed every 2nd time. 1,2,3,2,1,2,3 etc...
I haven't ever had time to do anything with it though as I ended up doing other things instead.


Ironically I found myself writing an INK animation sequence, though mine alternates between 2 colours going back and forth. Another line is added which begins with the opposite colour back and forth, so when INK is used with alternating colours it appears to be moving. It works through I think the coding could be improved.  ???
* Using the old Amstrad Languages :D   * with the Firmware :P
* I also like to problem solve code in BASIC :)   * And type-in Type-Ins! :D

Home Computing Weekly Programs
Popular Computing Weekly Programs
Your Computer Programs
Updated Other Program Links on Profile Page (Update April 16/15 phew!)
Programs for Turbo Pascal 3

SRS

One thing I just (re)learned today:

FOR I=0 to 1000:NEXT

ist FASTER than

FOR I=0 TO 1000:NEXT I

I sometimes forget we have interpreter not compiler ;)

Urusergi

This reminds me that depending on how you write the code you can save bytes in basic.

10 FOR I=100 TO 0 STEP -1:NEXT

this take 3 bytes more than:

10 FOR I=100TO 0STEP-1:NEXT

skylas

Hello all

I have a question regarding BASIC command REMAIN
I use AFTER command, and for disabling it i use <PRINT REMAIN(1)>, as it is said in Amstrad handbook.
I would like to ask if this number can be calculated as a variable.
I tried <PRINT REMAIN(1), X>, but this prints both remaining time and X variable.
I tried also <if PRINT REMAIN>1> or X=REMAIN but these does not help as it results in syntax error.
Is there a way to use this number on calculations?
Web: https://amstradsakis.blogspot.com
Twitter: https://twitter.com/AmstradSakis
My programs (only BASIC):
RETRO-LOADSHEET ON AMSTRAD CPC!
PENALTY KICKS!
CAPITAL QUIZ!
CAPITAL QUIZ 2! (Reverse edition)
HEADS OR TAILS (ΚΟΡΩΝΑ/ΓΡΑΜΜΑΤΑ)
HEART CHASER 1,2,3!
BARBOUTI!
STROOPIE!
BUDRUMI!
ART WAR!
BATTLE OF LENINGRAD!
AMSTABOO!
RODOLFO SKYLARRIENTE!

skylas

Ok sorry, my mistake! i just had to write x=REMAIN(1) just before entering PRINT REMAIN(1) and disabling it!
Web: https://amstradsakis.blogspot.com
Twitter: https://twitter.com/AmstradSakis
My programs (only BASIC):
RETRO-LOADSHEET ON AMSTRAD CPC!
PENALTY KICKS!
CAPITAL QUIZ!
CAPITAL QUIZ 2! (Reverse edition)
HEADS OR TAILS (ΚΟΡΩΝΑ/ΓΡΑΜΜΑΤΑ)
HEART CHASER 1,2,3!
BARBOUTI!
STROOPIE!
BUDRUMI!
ART WAR!
BATTLE OF LENINGRAD!
AMSTABOO!
RODOLFO SKYLARRIENTE!

Powered by SMFPacks Menu Editor Mod