Jump to content
IGNORED

How to iterate through an array, subtracting in BASIC


KayBee

Recommended Posts

Hi Atarians,

 

I am self teaching BASIC and don't know where to go from here. If I have an array of 100 random integers, and I want to subtract each subscripted value from the one preceding it.

 

 

10 DIM X(100)

20 FOR A=1 TO 100

30 X(A)=INT(10*RND(1))+1

?

50 PRINT?

60 NEXT A

 

goal:

 

X(100)-X(99) print result

X(99)-x(98) print result

etc.

 

Any guidance would be appreciated. Thank you for your time.

KB

 

 

Link to comment
Share on other sites

100 FOR A=100 TO 2 STEP -1

110 ? X(A)-X(A-1)

120 NEXT A

 

You could just do the calculation inside the main loop but the first value wouldn't be calculatable since you need 2 elements defined before you can subtract.

Note arrays have element (0) so you could just DIM to 99 and use it.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...