PDA

View Full Version : BASIC problem! :-o

CP/M User
July 9th, 2008, 03:16 PM
According to this book I've got (Programming is fun) the following can be produced using Nested Loops and presumibly an IF - THEN statement:

#
###
#####
#######
#########
#######
#####
###
#

What I'm wonderning is how, without a TAB statement - which is in the next chapter in the book! :-o

Mike Chambers
July 9th, 2008, 03:32 PM
you could use the LOCATE statement. i never use TAB. or just append spaces to the beginning of what you're PRINTing...

carlsson
July 10th, 2008, 12:42 AM
10 FOR I=-4 TO 4
20 IF I<>0 THEN FOR X=0 TO ABS(I)-1:PRINT" ";:NEXT X
30 FOR X=1+2*ABS(I) TO 9:PRINT"#";:NEXT X
40 PRINT:NEXT I

Hail the absolute value function! If you can live with an extra leading space, the IF statement can be removed as well.

CP/M User
July 10th, 2008, 01:42 AM
"Mike Chambers" wrote:

you could use the LOCATE statement. i never use TAB. or just append spaces to the beginning of what you're PRINTing...

Yeah sorry I didn't really explain properly I guess and while indeed the whole thing could be done with a series of PRINT, LOCATE within a Nested Loop, it wasn't what the book was asking for. It just puzzles me that some output as posted above could be done with so little. The output itself doesn't thrill me like the last program I posted here did, though just by looking at it because it's possible to archieve with so little, apart from some Maths and Print statements and an IF that it's possible it really got me asking how's it done! :-o

CP/M User
July 10th, 2008, 01:50 AM
carlsson wrote:

10 FOR I=-4 TO 4
20 IF I<>0 THEN FOR X=0 TO ABS(I)-1:PRINT" ";:NEXT X
30 FOR X=1+2*ABS(I) TO 9:PRINT"#";:NEXT X
40 PRINT:NEXT I

Hail the absolute value function! If you can live with an extra leading space, the IF statement can be removed as well.

Yes, you obviously have a better grip with BASIC than what I do Carlsson - you've probably just answered the problem for the book too because they didn't have an answer for it, maybe because they forgot to describe the ABS function. Programming is Fun looks like a really good reference book for BASIC though (and was setout to target the 13-17 age group, though anyone wanting to build on their BASIC could use it I suppose!), and prior to that question, it highlights, FOR - NEXT statements, IF statements, SQR Function, PRINT is there of course and it also applies maths in those early chapters - which is why I'm stumbling across this problem in particular.

carlsson
July 10th, 2008, 05:26 AM
The next exercise is to rewrite the program so it doesn't use ABS. It is probably doable, but will require more IF statements and possibly temporary variable assignments.

CP/M User
July 10th, 2008, 09:42 PM
carlsson wrote:

The next exercise is to rewrite the program so it doesn't use ABS. It is probably doable, but will require more IF statements and possibly temporary variable assignments.

Unfortunately my book doesn't state how many IF...THEN statements you can have, which gives me the impression you can have only one! Perhaps they should have included some PSEUDOCODE to make the problem look clearer. The way I see it, my book gives you a lot of maths problems (specifically algebra problems) to deal with and then asks you to write some programs or interpret them into BASIC equivalents, I'm guessing that one or two of those problems you can use them to draw up a Diamond shape on the screen - though you could go really nutty over these sorts of problems instead of doing this:

10 PRINT" #"
20 PRINT" ###"
30 PRINT" #####"
40 PRINT" #######"
50 PRINT"#########"
60 PRINT" #######"
70 PRINT" #####"
80 PRINT" ###"
90 PRINT" #"
100 END

Which anyone can do! :-(

carlsson
July 10th, 2008, 11:42 PM
10 FOR I=1 TO 9
15 IF I<>5 THEN FOR X=1 TO (I-5)*(1+2*(I<5)):PRINT" ";:NEXT X
20 FOR X=1 TO 1-((I>1)*2*(I-1))+((I>5)*4*(I-5)):PRINT"#";:NEXT X
30 PRINT:NEXT I
This program relies on that 1=1 equals -1 and 1=0 equals 0. Microsoft Basics usually work this way, while other implementations may differ.

CP/M User
July 11th, 2008, 02:55 AM
No that's amazing, both of those programs work in Locomotive BASIC on my CPC - I'm more compelled to use the ABS version though (all that maths in the second program makes my head spin!), though am still having a hard time coming to grips what it all means. I can see in the ABS version at least that you've setup I (which could be Y I suppose) to define the lines and obviously the widest line the center of it being 9 Hashes across, though the second FOR statement in line 30 is puzzling me a bit (could that be X=3*ABS(I) TO 9 I'll test that out) and in line 20 on the first program you've got a condition meaning that if I isn't equal to 0 to print a space so 0 is the centre! :-D

carlsson
July 11th, 2008, 04:06 AM
Yes, without the IF statement you would get an extra space at the beginning of each line.

10 FOR I=-4 TO 4
20 FOR X=1 TO ABS(I)-1:PRINT" ";:NEXT X
30 FOR X=1+2*ABS(I) TO 9:PRINT"#";:NEXT X
40 PRINT:NEXT I

Multiplication has precedence over addition, this is universal both in general math and programming languages. It means that 1+2*ABS(I) is not equal to 3*ABS(I). If you wish, you can rewrite that formula as 2*ABS(I)+1 or for more clarity (2*ABS(I))+1.

In my second listing, I am also taking advantage of the precedence rules to eliminate unneccessary parenthesis. However in some cases it becomes required:

1-((I>1)*2*(I-1))+((I>5)*4*(I-5))

1 minus (if I>1 then return -2 times (I-1), else 0) plus (if I>5 then return -4 times (I-5), else 0)

Here you see that -2 * (I-1) and -2*I - 1 are not the same thing. The first formula could be rewritten as (I-1) * -2 and still is not the same thing as I - 1*-2.

I hope you follow this simple algebra. Otherwise just insert a value to the variable I and work it out:

I = 3 => -2 * (3-1) = -2 * 2 = -4
I = 3 => (-2*3) - 1 = -6 - 1 = -7

CP/M User
July 11th, 2008, 04:32 AM
Ah yes I need to stick the old BOMDAS rule in front of my computer! :-D

I must confess I'm one of those 2*ABS(I)+1 type people!

Your second program becomes tricky though cause you have Brackets inside Brackets though in a line like this:

(1+2*(I<5))

I guess (2*(I<5)+1) is also acceptable and is multiplied by (I-5) depending on whatever's in I which can be from -4 onwards! :-D

Just wondered though in line 30 where you've got 2*(I-1), I would presume that whatever is in I is deducted by 1 first and then multiplied by 2, in order to keep with the rules of BOMDAS, Brackets superseeds no Brackets.

carlsson
July 11th, 2008, 08:31 AM
So let's evaluate those formulas:

(I-5)*(1+2*(I<5)) (to create leading spaces)
I=1 => (-4) * (1+2*(-1)) = -4 * -1 = 4
I=2 => (-3) * (1+2*(-1)) = -3 * -1 = 3
...
I=5 => (0) * (1+2*(0)) = 0 * 1 = 0 [NB]
I=6 => (1) * (1+2*(0)) = 1 * 1 = 1
...
I=9 => (4) * (1+2*(0)) = 1 * 4 = 4

[NB] The FOR statement always executes once, even if the end argument is met from the beginning. Thus the IF statement to avoid it printing out a leading space.

1-((I>1)*2*(I-1))+((I>5)*4*(I-5)) (number of hashes)
I=1 => 1 - (0*2*0) + (0*4*-4) = 1
I=2 => 1 - (-1*2*1) + (0*4*-3) = (1 - -2) + 0 = 3
I=3 => 1 - (-1*2*2) + (0*4*-2) = (1 - -4) + 0 = 5
...
I=5 => 1 - (-1*2*4) + (0*4*0) = (1 - -8 ) + 0 = 9
I=6 => 1 - (-1*2*5) + (-1*4*1) = (1 - -10) + -4 = 7 (!!!)
...
I=8 => 1 - (-1*2*7) + (-1*4*3) = (1 - -14) + -12 = 3
I=9 => 1 - (-1*2*8 ) + (-1*4*4) = (1 - -16) + -16 = 1

I added the extra parenthesis at the end to show how addition and subtraction takes place, that (1 - -16) + -16 = 1 but 1 - (-16 + -16) = 33. I suppose you have seen double negation before?

MikeS
July 11th, 2008, 09:15 AM

for i=1 to 9: x=i+(i>5)*(i-5)*2: ? mid\$("bbbb#########",x,x+4):next

or, using Carlsson's way:

for i=-4 to 4: x=i+5+(i>0)*i*2: ? mid\$("bbbb#########",x,x+4):next

(b=space)

m

carlsson
July 11th, 2008, 12:52 PM
Wow! That's a clever way to do it. Just remember not all Basics implement strings with LEFT\$ MID\$ RIGHT\$.

CP/M User
July 12th, 2008, 01:04 AM
It seems to have quite a complicated maths problem behind it. I dare say this is why there's Cross Referencing programs out there for these kinds of things, cause it makes me wonder how people come up with this stuff! :-o

Another BASIC book I've opened up today seems to have a whole heap of Text based the most impressive I've seen in that is an Electronic Field Art. The book then moves onto some colour graphics which looks to be using BASIC which comes with the Compucolor (specifically the Compucolor 8051), so the PLOT command built into that version of BASIC is somewhat different from the PLOT I know, it looks the book has been good about this and explains what the statements mean.

CP/M User
July 12th, 2008, 02:44 AM
Quite fascinating to see what BASIC books were like back in 1979 - quite remarkable I managed to find a book full of all kinds of mathematical problems and text graphs as well as some full coloured programs and problems relating to adopting some of those programs into full colour graphics! If anyone comes across "BASIC and the Personal Computer" by Thomas Dwyer & Margot Critchfield it's worth a look I reckon. I endeavour to post a program or two hopefully though (fingers crossed! :-o).