Arrays? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Arrays?

Mirror7Mirror7 Posts: 6Member
Okay. Call me ignorant, but I can not possibly concieve any use for an array in the Basic language, that is, a use that can't already be fulfilled by easier means. Could someone please tell me a use for an array?

Comments

  • GaashiusGaashius Posts: 349Member
    : Okay. Call me ignorant, but I can not possibly concieve any use for an array in the Basic language, that is, a use that can't already be fulfilled by easier means. Could someone please tell me a use for an array?
    :
    Well, let's say we have 100,000 integers in our program. Now, we wont call each integer like int1, int2 etc., right? We should put them into an array, what is much more flexible. We can index them as Int[xy], where xy is the position of the array what we want data from. Arrays can be multidimensional, like Int[x][y], this way we can make our pretty 2D/3D games. Due to my english, I can't go further, but ask freely :-)
    ****************
    Any questions? Just ask!

    :-) [b][blue]GAASHIUS[/blue][/b] :-)


  • MargaloMargalo Posts: 75Member
    : : Okay. Call me ignorant, but I can not possibly concieve any use for an array in the Basic language, that is, a use that can't already be fulfilled by easier means. Could someone please tell me a use for an array?
    : :
    : Well, let's say we have 100,000 integers in our program. Now, we wont call each integer like int1, int2 etc., right? We should put them into an array, what is much more flexible. We can index them as Int[xy], where xy is the position of the array what we want data from. Arrays can be multidimensional, like Int[x][y], this way we can make our pretty 2D/3D games. Due to my english, I can't go further, but ask freely :-)
    : ****************
    : Any questions? Just ask!
    :
    : :-) [b][blue]GAASHIUS[/blue][/b] :-)
    :
    :
    :
    Well thats the thing if you call them all int[xy] then you can get them confussed and if you want to make the pretty games why not make a grid of the graphics you need, also arrays make it near immpossible to read the sorce code, still why do we need them?

    -Margalo ;-)
  • IDKIDK Posts: 1,784Member
    : Well thats the thing if you call them all int[xy] then you can get them confussed and if you want to make the pretty games why not make a grid of the graphics you need, also arrays make it near immpossible to read the sorce code, still why do we need them?
    :
    : -Margalo ;-)
    :
    The grid thing you speak of is an array. This is a rather advanced example where you'll need to use arrays. What if you got 100 particles, and each particle should attract with eachother. You wont make 100*100=10000 lines of code when you only need a few. This program is a small 2D universe. The laws of gravity is there. If you want to add more start simply change the max variable. My computer can only handle that...

    If you'll try to make this program without arrays it is impossible (almost anyways).

    [code]
    SCREEN 13
    RANDOMIZE 1
    FOR b = 0 TO 63
    OUT &H3C9, b / 1.3
    OUT &H3C9, b / 1.2
    OUT &H3C9, b
    NEXT
    TYPE star
    x AS SINGLE
    y AS SINGLE
    dx AS SINGLE
    dy AS SINGLE
    mas AS SINGLE
    li AS SINGLE
    END TYPE
    max = 10
    DIM star(max) AS star
    DEF SEG = &HA000

    FOR a = 0 TO max
    star(a).x = RND * 120 + 100
    star(a).y = RND * 120 + 40
    star(a).dx = 0
    star(a).dy = 0
    star(a).mas = RND
    star(a).li = (star(a).mas * .5 + .5) * 63
    NEXT

    DO
    LOCATE 1, 1
    FOR a = 0 TO max
    POKE star(a).x + INT(star(a).y) * 320, 0
    IF star(a).x > 320 OR star(a).x < 1 OR star(a).y > 200 OR star(a).y < 0 THEN
    star(a).x = RND * 120 + 100
    star(a).y = RND * 120 + 40
    star(a).dx = 0
    star(a).dy = 0
    star(a).mas = RND
    star(a).li = (star(a).mas * .5 + .5) * 63
    END IF
    star(a).x = star(a).x + star(a).dx
    star(a).y = star(a).y + star(a).dy
    FOR b = 0 TO max
    IF NOT (b = a) THEN
    r = (star(a).x - star(b).x) ^ 2 + (star(a).y - star(b).y) ^ 2
    PRINT SQR(r)
    star(b).dx = star(b).dx + (star(a).x - star(b).x) / r * (star(a).mas / star(b).mas) / 100
    star(b).dy = star(b).dy + (star(a).y - star(b).y) / r * (star(a).mas / star(b).mas) / 100
    END IF 'star(a).mas * star(b).mas / r
    NEXT
    POKE star(a).x + INT(star(a).y) * 320, star(a).li
    NEXT
    'FOR a = 0 TO 10000: NEXT
    LOOP UNTIL INKEY$ = CHR$(27)
    [/code]


    [b]Niklas Ulvinge[/b] [white]aka [b]IDK[/b][/white]

  • GaashiusGaashius Posts: 349Member
    : : Well thats the thing if you call them all int[xy] then you can get them confussed and if you want to make the pretty games why not make a grid of the graphics you need, also arrays make it near immpossible to read the sorce code, still why do we need them?
    : :
    : : -Margalo ;-)
    : :
    : The grid thing you speak of is an array. This is a rather advanced example where you'll need to use arrays. What if you got 100 particles, and each particle should attract with eachother. You wont make 100*100=10000 lines of code when you only need a few. This program is a small 2D universe. The laws of gravity is there. If you want to add more start simply change the max variable. My computer can only handle that...
    :
    : If you'll try to make this program without arrays it is impossible (almost anyways).
    :
    : [code]
    : SCREEN 13
    : RANDOMIZE 1
    : FOR b = 0 TO 63
    : OUT &H3C9, b / 1.3
    : OUT &H3C9, b / 1.2
    : OUT &H3C9, b
    : NEXT
    : TYPE star
    : x AS SINGLE
    : y AS SINGLE
    : dx AS SINGLE
    : dy AS SINGLE
    : mas AS SINGLE
    : li AS SINGLE
    : END TYPE
    : max = 10
    : DIM star(max) AS star
    : DEF SEG = &HA000
    :
    : FOR a = 0 TO max
    : star(a).x = RND * 120 + 100
    : star(a).y = RND * 120 + 40
    : star(a).dx = 0
    : star(a).dy = 0
    : star(a).mas = RND
    : star(a).li = (star(a).mas * .5 + .5) * 63
    : NEXT
    :
    : DO
    : LOCATE 1, 1
    : FOR a = 0 TO max
    : POKE star(a).x + INT(star(a).y) * 320, 0
    : IF star(a).x > 320 OR star(a).x < 1 OR star(a).y > 200 OR star(a).y < 0 THEN
    : star(a).x = RND * 120 + 100
    : star(a).y = RND * 120 + 40
    : star(a).dx = 0
    : star(a).dy = 0
    : star(a).mas = RND
    : star(a).li = (star(a).mas * .5 + .5) * 63
    : END IF
    : star(a).x = star(a).x + star(a).dx
    : star(a).y = star(a).y + star(a).dy
    : FOR b = 0 TO max
    : IF NOT (b = a) THEN
    : r = (star(a).x - star(b).x) ^ 2 + (star(a).y - star(b).y) ^ 2
    : PRINT SQR(r)
    : star(b).dx = star(b).dx + (star(a).x - star(b).x) / r * (star(a).mas / star(b).mas) / 100
    : star(b).dy = star(b).dy + (star(a).y - star(b).y) / r * (star(a).mas / star(b).mas) / 100
    : END IF 'star(a).mas * star(b).mas / r
    : NEXT
    : POKE star(a).x + INT(star(a).y) * 320, star(a).li
    : NEXT
    : 'FOR a = 0 TO 10000: NEXT
    : LOOP UNTIL INKEY$ = CHR$(27)
    : [/code]
    :
    :
    : [b]Niklas Ulvinge[/b] [white]aka [b]IDK[/b][/white]
    :
    :
    IDK is totally right. As for the code is readable or not, only the creator is responsible.

    ****************
    Any questions? Just ask!

    :-) [b][blue]GAASHIUS[/blue][/b] :-)


  • PrzemekG_PrzemekG_ Posts: 595Member
    : : : Well thats the thing if you call them all int[xy] then you can get them confussed and if you want to make the pretty games why not make a grid of the graphics you need, also arrays make it near immpossible to read the sorce code, still why do we need them?
    : : :
    : : : -Margalo ;-)
    : : :
    : : The grid thing you speak of is an array. This is a rather advanced example where you'll need to use arrays. What if you got 100 particles, and each particle should attract with eachother. You wont make 100*100=10000 lines of code when you only need a few. This program is a small 2D universe. The laws of gravity is there. If you want to add more start simply change the max variable. My computer can only handle that...
    : :
    : : If you'll try to make this program without arrays it is impossible (almost anyways).
    : :
    : : [code]
    : : SCREEN 13
    : : RANDOMIZE 1
    : : FOR b = 0 TO 63
    : : OUT &H3C9, b / 1.3
    : : OUT &H3C9, b / 1.2
    : : OUT &H3C9, b
    : : NEXT
    : : TYPE star
    : : x AS SINGLE
    : : y AS SINGLE
    : : dx AS SINGLE
    : : dy AS SINGLE
    : : mas AS SINGLE
    : : li AS SINGLE
    : : END TYPE
    : : max = 10
    : : DIM star(max) AS star
    : : DEF SEG = &HA000
    : :
    : : FOR a = 0 TO max
    : : star(a).x = RND * 120 + 100
    : : star(a).y = RND * 120 + 40
    : : star(a).dx = 0
    : : star(a).dy = 0
    : : star(a).mas = RND
    : : star(a).li = (star(a).mas * .5 + .5) * 63
    : : NEXT
    : :
    : : DO
    : : LOCATE 1, 1
    : : FOR a = 0 TO max
    : : POKE star(a).x + INT(star(a).y) * 320, 0
    : : IF star(a).x > 320 OR star(a).x < 1 OR star(a).y > 200 OR star(a).y < 0 THEN
    : : star(a).x = RND * 120 + 100
    : : star(a).y = RND * 120 + 40
    : : star(a).dx = 0
    : : star(a).dy = 0
    : : star(a).mas = RND
    : : star(a).li = (star(a).mas * .5 + .5) * 63
    : : END IF
    : : star(a).x = star(a).x + star(a).dx
    : : star(a).y = star(a).y + star(a).dy
    : : FOR b = 0 TO max
    : : IF NOT (b = a) THEN
    : : r = (star(a).x - star(b).x) ^ 2 + (star(a).y - star(b).y) ^ 2
    : : PRINT SQR(r)
    : : star(b).dx = star(b).dx + (star(a).x - star(b).x) / r * (star(a).mas / star(b).mas) / 100
    : : star(b).dy = star(b).dy + (star(a).y - star(b).y) / r * (star(a).mas / star(b).mas) / 100
    : : END IF 'star(a).mas * star(b).mas / r
    : : NEXT
    : : POKE star(a).x + INT(star(a).y) * 320, star(a).li
    : : NEXT
    : : 'FOR a = 0 TO 10000: NEXT
    : : LOOP UNTIL INKEY$ = CHR$(27)
    : : [/code]
    : :
    : :
    : : [b]Niklas Ulvinge[/b] [white]aka [b]IDK[/b][/white]
    : :
    : :
    : IDK is totally right. As for the code is readable or not, only the creator is responsible.
    :
    : ****************
    : Any questions? Just ask!
    :
    : :-) [b][blue]GAASHIUS[/blue][/b] :-)
    :
    :
    :
    That's true, imagin that code repeated 100 times, will it be easyer to read ? Also try to imagin a cad program which needs to allocate dynamicly the number of points, without arrays it would be impossible. Also look at all those paint programs written in QB (there a a few of them), how do you imagin dynamicly ploting pixels when you have variables like "pixel1x1, pixel1x2, pixel2x1,...", imagin it for images 640x480.

    The problem is I can't say arrays are the best way of doing things, sometimes you need buffers, which actualy work much like arrays (sometimes like strings in basic, but those are too slow), but in lower level languages gives much more power.
  • IDKIDK Posts: 1,784Member
    Is there any language that doesn't support arrays?

    Assembler, could do if you call the segment, x
    and the offset y. The you could make a 16* something array.

    [b]Niklas Ulvinge[/b] [white]aka [b]IDK[/b][/white]

Sign In or Register to comment.