Image Map Image Map
Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: BASIC problem! :-o

  1. #1
    Join Date
    May 2003
    Location
    Back of Burke (Guday!), Australia
    Posts
    2,905

    Default BASIC problem! :-o

    According to this book I've got (Programming is fun) the following can be produced using Nested Loops and presumibly an IF - THEN statement:

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

  2. #2

    Default

    you could use the LOCATE statement. i never use TAB. or just append spaces to the beginning of what you're PRINTing...
    My emulators!
    Fake86 8086/V20 PC emulator: http://sourceforge.net/p/fake86
    MoarNES Nintendo emulator: http://sourceforge.net/p/moarnes


  3. #3
    Join Date
    Jul 2003
    Location
    Västerås, Sweden
    Posts
    6,277

    Default

    Code:
    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.
    Anders Carlsson

  4. #4
    Join Date
    May 2003
    Location
    Back of Burke (Guday!), Australia
    Posts
    2,905

    Default

    "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

  5. #5
    Join Date
    May 2003
    Location
    Back of Burke (Guday!), Australia
    Posts
    2,905

    Default

    carlsson wrote:

    Code:
    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.

  6. #6
    Join Date
    Jul 2003
    Location
    Västerås, Sweden
    Posts
    6,277

    Default

    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.
    Anders Carlsson

  7. #7
    Join Date
    May 2003
    Location
    Back of Burke (Guday!), Australia
    Posts
    2,905

    Default

    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:

    Code:
     
    10 PRINT"    #"
    20 PRINT"   ###"
    30 PRINT"  #####"
    40 PRINT" #######"
    50 PRINT"#########"
    60 PRINT" #######"
    70 PRINT"  #####"
    80 PRINT"   ###"
    90 PRINT"    #"
    100 END
    Which anyone can do! :-(

  8. #8
    Join Date
    Jul 2003
    Location
    Västerås, Sweden
    Posts
    6,277

    Default

    Code:
    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.
    Anders Carlsson

  9. #9
    Join Date
    May 2003
    Location
    Back of Burke (Guday!), Australia
    Posts
    2,905

    Default

    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

  10. #10
    Join Date
    Jul 2003
    Location
    Västerås, Sweden
    Posts
    6,277

    Default

    Yes, without the IF statement you would get an extra space at the beginning of each line.

    Code:
    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
    Anders Carlsson

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •