Qbasicnews.com
February 28, 2020, 03:44:27 PM
 Pages: [1]
 Author Topic: How do you make a cyclical array?  (Read 2407 times)
jupiter
New Member

Posts: 3

 « on: March 05, 2005, 06:09:06 PM »

What more can I say?

Thanks
 Logged
Rattrapmax6
__/--\__

Posts: 2577

 « Reply #1 on: March 05, 2005, 07:40:10 PM »

A What?? Do be more clear, what do they do? ::
 Logged

Kevin (x.t.r.GRAPHICS)

Sterling Christensen
Na_th_an

Posts: 1328

 « Reply #2 on: March 05, 2005, 08:03:23 PM »

If I understand you corrently, a really cheap way (probably not what you had in mind, but still) is to MOD the index by the number of elements.

Something like this:
Code:
FUNCTION getArrayElement (array(), whichElement)

' Calculate the number of elements in the array:
numElements = UBOUND(array) - LBOUND(array) + 1

' De-cyclic-itize the index (whichElement):
IF whichElement > UBOUND(array) THEN
whichElement = ((whichElement - LBOUND(array)) MOD numElements) + LBOUND(array)
ELSE
DO WHILE whichElement < LBOUND(array)
whichElement = whichElement + numElements
LOOP
END IF

getArrayElement = array(whichElement)

END FUNCTION
 Logged
jupiter
New Member

Posts: 3

 « Reply #3 on: March 06, 2005, 05:53:55 AM »

I mean an array which, theoretically, is in the shape of a circle. To put it in more qbasic-y terms, if you reach the end it will start again from the beginning.
For example, if the array was five fields long, and there was a formula (in loose english!) 'x+3 = array field' to access a part of the array, then if a user enters 4 for x the program should access the second field.
Sterling Christensen, thanks for the code but it's a little over my head - I haven't been programming very long at all!
 Logged
Anonymous
Guest
 « Reply #4 on: March 06, 2005, 07:57:58 AM »

okay um

seems like what you want can be done with MOD

like lets say your array has 5 slots like youre saying

so, theres 5 'slots' at the 6th, we start back at the first, etc..

so lets use a variable x%. x% = number of slots. x% = 5

in order to determine which slot to use based on this system, you only need to do

tehindicewewant% = theinputfromuser% MOD x%

so, lets make a sample prog:

Code:

dim thisisafiveslotarray%(1 to 5)

'fill them with junk

thisisafiveslotarray%(1) = 12
thisisafiveslotarray%(2) = 23
thisisafiveslotarray%(3) = 34
thisisafiveslotarray%(4) = 45
thisisafiveslotarray%(5) = 56

x% = UBOUND(thisisafiveslotarray%)

'This puts 5 into x%.

input "Put in a number: ", userinput%

tehindicewewant% = userinput% MOD x%

? "The array ended up on slot number"; tehindicewewant%
? "The value stored in this indice is"; thisisafiveslotarray%(tehindicewewant%)

hope this makes sense and wasnt overly complicated <.<

edit: realized it would be clearer to not work with a zero indexed array

well i guess ill explain MOD in case anyone doesn't know what it does. Basically MOD takes a number, divides it by another number, and only takes the remainder of the division

so 12 / 8 would be 1,, with a remainder of 4

x% = 12 MOD 8.   x% is 4

an easier way to think of it is like a slicer, you have a line of numbers and MOD is the size of the slice

lets say we have 7 as our userinput from the above program

would be that in a strip

now if we were to use 7 MOD 5, we would be slicing the strip into 5's

so 7 MOD 5 would return 2

(yes im really bored thats why im explaining it so detailed)

but hey maybe it will help someone ;p

editted like 87162487 times
 Logged
jupiter
New Member

Posts: 3

 « Reply #5 on: March 06, 2005, 09:42:42 AM »

Brilliant.
Thanks a lot

(see, all the editing was worthwhile!)
 Logged
Anonymous
Guest
 « Reply #6 on: March 06, 2005, 11:22:07 AM »