Nested FOR LOOPS and populating arrays.

Discuss all aspects of programming here. From 8-bit through to modern architectures.
Post Reply
mysteriouswiggler
Posts: 24
Joined: Tue Nov 27, 2018 8:59 am
Contact:

Nested FOR LOOPS and populating arrays.

Post by mysteriouswiggler » Fri Dec 07, 2018 11:36 am

Sorry for being a potato, but I can't seem to get my head round this nestled For loop.

What I am trying to do is rotate elements in a multi-dimensional array.

I have the first line 1,2,3,5,6
what I want to do is populate the rest of the array to look like
0 1 2 3 4:
----------------
0: 1,2,3,5,6
1: 6,1,2,3,5
2: 5,6,1,2,3
3: 3,5,6,1,2
4: 2,3,5,6,1

I figured the best way to approach this was to DIM a 5x5 array and use a nestled FOR loop. What I am getting is a subscript problem even when I try and modulate the result. What I want to be able to do is get a value like A%(1,5) where 5 is modulated to 0. I tried this in the code below but I can't seem to crack it.

Code: Select all

5 REM I declare my array (5x5) where 0 is the starting location.
10 DIM A%(4,4)
15 REM I populate the first line of the array with the tones I want to shift
20 A%(0,0)=1:A%(0,1)=2:A%(0,2)=3:A%(0,3)=5:A%(0,4)=6
25 REM I already have the 0 row populated, thus I start on number 1. This is the outer FOR loop.
30 FOR I=1 TO 4
35 This is the inner FOR loop. Where we populate the columns of the array.
40 FOR J=0 TO 4
45 REM what I am trying to do here is take the top row of the array, then pop the J+1 column (Mod 4) While poping that value into the A%(I,J) array location. 
50 A%(I,J)=A%(I-1,((J+1)MOD4)))
52 REM Printing the result of the array to the screen.
55 PRINT STR$(A%(I,J));" ";
60 NEXT : PRINT : NEXT
Perhaps I should be using the LEN() instead and some logic to perform the element re-arranging instead of MOD?

User avatar
lurkio
Posts: 1817
Joined: Tue Apr 09, 2013 11:30 pm
Location: Doomawangara
Contact:

Re: Nested FOR LOOPS and populating arrays.

Post by lurkio » Fri Dec 07, 2018 12:29 pm

Try this:

Code: Select all

5 REM I declare my array (5x5) where 0 is the starting location.
10 DIM A%(4,4)
15 REM I populate the first line of the array with the tones I want to shift
20 A%(0,0)=1:A%(0,1)=2:A%(0,2)=3:A%(0,3)=5:A%(0,4)=6
22 PRINT;"0: ";:FORI=0TO4:PRINT;A%(0,I);" ";:NEXT:PRINT
25 REM I already have the 0 row populated, thus I start on number 1. This is the outer FOR loop.
30 FOR I=1 TO 4:PRINT;I;": ";
35 REM This is the inner FOR loop. Where we populate the columns of the array.
40 FOR J=0 TO 4
45 REM what I am trying to do here is take the top row of the array, then pop the J+1 column (Mod 4) While poping that value into the A%(I,J) array location. 
50 A%(I,J)=A%(I-1,((J+4)MOD5))
52 REM Printing the result of the array to the screen.
55 PRINT;A%(I,J);" ";
60 NEXT : PRINT : NEXT
:idea:
Last edited by lurkio on Fri Dec 07, 2018 12:33 pm, edited 1 time in total.

mysteriouswiggler
Posts: 24
Joined: Tue Nov 27, 2018 8:59 am
Contact:

Re: Nested FOR LOOPS and populating arrays.

Post by mysteriouswiggler » Fri Dec 07, 2018 1:11 pm

Thank you; that worked like a charm 8)

Post Reply