# How to iterate through an array, subtracting in BASIC

BASIC

3 replies to this topic

### #1 KayBeeOFFLINE

KayBee

• 18 posts
• Calculating...
• Location:Porter Ranch, CA

Posted Fri Jan 11, 2019 7:22 AM

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

### #2 RybagsONLINE

Rybags

Gridrunner

• 15,948 posts
• Location:Australia

Posted Fri Jan 11, 2019 7:44 AM

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.

### #3 E474ONLINE

E474

Chopper Commander

• 123 posts

Posted Fri Jan 11, 2019 1:01 PM

90 X(100)=INT(10*RND(1))+1
100 FOR A=99 TO 1 STEP -1
110 X(A)=INT(10*RND(1))+1
120 ? X(A+1)-X(A)
130 NEXT A

### #4 KayBeeOFFLINE

KayBee

• Topic Starter
• 18 posts
• Calculating...
• Location:Porter Ranch, CA

Posted Sun Jan 13, 2019 10:08 AM

Yup, got it now. Thank you gentlemen. And of course it seems obvious once you know the answer. Ahhh learning...

Cheers,

KB

### Also tagged with one or more of these keywords: BASIC

#### 0 user(s) are browsing this forum

0 members, 0 guests, 0 anonymous users