Shrinking an array when an element is removed

by Solitaire (Login Solitaire1)
S

This demo uses a sample array of random numbers. User selects the index to delete. All elements above that index are moved down, placing the value of 0 in the last index.


DIM x AS INTEGER, ndx AS INTEGER
DIM myarray(25) AS INTEGER
RANDOMIZE TIMER
CLS
PRINT "Original array of 25 indexes:"
FOR x = 1 TO 25
    myarray(x) = INT(RND * 100) + 1
    PRINT myarray(x); ", ";
NEXT x
PRINT : PRINT
DO
    INPUT "Which index to delete (1-25)?  ", ndx
LOOP WHILE ndx > 25 OR ndx < 1
PRINT
PRINT "Array index"; ndx; "value"; myarray(ndx); "will be deleted"
FOR x = ndx TO 24
    myarray(x) = myarray(x + 1)
NEXT x
myarray(25) = 0
PRINT
PRINT "New array"
FOR x = 1 TO 24
    PRINT myarray(x); ", ";
NEXT x
END

Posted on May 19, 2010, 9:44 AM

Respond to this message   

Return to Index