Printing items in an array.

new BookmarkLockedFalling
daveylibra
New Member
*

daveylibra Avatar

Posts: 5

Post by daveylibra on Jan 25, 2015 18:39:08 GMT -5


Hi, I am just trying to write a very simple program in RunBasic, but as I am a beginner I am struggling.

I am trying to create an array of numbers, depending on another number I input. So far I have


DIM P(100)
P(1)=1
P(2)=2
P(3)=2

FOR X=4 TO 100
INPUT N

I then want to do this:
If N is one of a certain set of numbers, lets say 1 for simplicity, the next item in the array - P(X)- keeps the value of the previous one.
If N is one of another set of numbers, lets say 0, I want to 'cross off' (or make 0) the previous value in the list, and also the first value in the list that is not already crossed off. I then make P(X) equal to the next value in the list, plus 1.
If all values are crossed off, END.

But there must be something wrong with my syntax, as whatever I do, if I PRINT P(X) I get

Error: #asIfnumber

any advice on how to write the progression would be much appreciated.


StefanPendl
Global Moderator
*****

StefanPendl Avatar

Run for BASIC ...
Posts: 945

[b]Stefan[/b] - [a href=http://stefanpendl.runbasichosting.com/]Homepage[/a][br][br][b]Please give credit if you use code I post, no need to ask for permission.[/b][br][br]Run BASIC 1.01, Fire-/Waterfox (IE11, Edge), Windows 10 Professional x64, Intel Core i7-4710MQ 2.5GHz, 16GB RAM
daveylibra
New Member
*

daveylibra Avatar

Posts: 5

Post by daveylibra on Jan 26, 2015 6:22:03 GMT -5

Thanks! But I need to update the array one by one, as N is input

I have

DIM P(100)
P(1)=1
P(2)=2
P(3)=2

FOR X=4 TO 100
INPUT N
SELECT CASE N
CASE N=1 REM NEXT VALUE STAYS THE SAME
P(X)=P(X-1)

CASE N=0 REM CALC NEXT VALUE
P(X-1)=0 REM CROSS OFF PREVIOUS VALUE
FOR Y=1 TO X
IF P(Y)<>0 THEN P(Y)=0: P(X)=P(Y+1)+1: END FOR
REM CROSS OFF 1ST VALUE IN LIST THAT HAS NOT BEEN CROSSED OF ALREADY, AND MAKE OUR NEXT
REM VALUE THE NEXT VALUE IN OUR LIST +1
NEXT Y
END SELECT

FOR Y=1 TO X
IF P(Y)=0 THEN CHECK=0 ELSE CHECK=1: REM CHECK IF ALL VALUES ARE 0
NEXT Y
IF CHECK=0 THEN PRINT "STOP": END

PRINT "PROGRESSION IS NOW "
FOR Y=1 TO X
PRINT P(Y);" ";
NEXT Y

NEXT X

....but not sure if this syntax is correct.
StefanPendl
Global Moderator
*****

StefanPendl Avatar

Run for BASIC ...
Posts: 945

Post by StefanPendl on Jan 27, 2015 14:09:21 GMT -5

If you use the variable at the SELECT CASE line, you can not use a complete condition at the CASE line.
Instead you only use the expected value.

In addition there is no END FOR, just an EXIT FOR.

I added indent and moved the remarks so it is more readable.
To preserve the indent in messages, use the CODE tags.


DIM P(100)
P(1)=1
P(2)=2
P(3)=2

FOR X=4 TO 100
INPUT N

SELECT CASE N
CASE 1
REM NEXT VALUE STAYS THE SAME
P(X)=P(X-1)

CASE 0
REM CALC NEXT VALUE
REM CROSS OFF PREVIOUS VALUE
P(X-1)=0

FOR Y=1 TO X
IF P(Y)<>0 THEN P(Y)=0: P(X)=P(Y+1)+1: EXIT FOR
REM CROSS OFF 1ST VALUE IN LIST THAT HAS NOT BEEN CROSSED OF ALREADY, AND MAKE OUR NEXT
REM VALUE THE NEXT VALUE IN OUR LIST +1
NEXT Y
END SELECT

FOR Y=1 TO X
REM CHECK IF ALL VALUES ARE 0
IF P(Y)=0 THEN CHECK=0 ELSE CHECK=1
NEXT Y

IF CHECK=0 THEN PRINT "STOP": END

PRINT "PROGRESSION IS NOW "

FOR Y=1 TO X
PRINT P(Y);" ";
NEXT Y
NEXT X
Last Edit: Jan 27, 2015 14:11:03 GMT -5 by StefanPendl: corrected formatting and typo
[b]Stefan[/b] - [a href=http://stefanpendl.runbasichosting.com/]Homepage[/a][br][br][b]Please give credit if you use code I post, no need to ask for permission.[/b][br][br]Run BASIC 1.01, Fire-/Waterfox (IE11, Edge), Windows 10 Professional x64, Intel Core i7-4710MQ 2.5GHz, 16GB RAM
daveylibra
New Member
*

daveylibra Avatar

Posts: 5