Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem with array ("Seine" by Ellsworth Kelly)

Status
Not open for further replies.

riesjuh

Technical User
Dec 13, 2001
5
NL
Hey, i have a kind of a problem, i want to program something in qbasic, for a part it's working already (small part), but partly there are some problems
I want to create a kind of an artpiece, originally by Ellsworth Kelly (the "Seine"). In this piece of art there is a kind of structure: there are vertical columns for different horizontal values (x-values), kind of like this:
-----------------
| | | | |
| | | | |
| | | | |
-----------------
1 2 3 4 x -->
each column got to have collored blocks, column 1 has 1 block at random y-height, column 2 has 2 blocks at random y-height, column 3 has 3 blocks etc..
This is the code i already made(it's not really much):

CLS
SCREEN 9
WINDOW (-2, -2)-(6, 6)
randomize timer
for x = 0 to 4
for q = 1 to (x + 1)
T = x + 1
y = INT(RND * 5)
LINE (x, y)-((x+1), (y+1)), T, BF
next q
next x
end

as you can see, this is quite simple (even for me), now here is my problem: with this code, basic sets the random blocks, but they can be printed over a block which it's y-value was already used, so what i need to do, is to put the used y-values in an array, so that those will not be picked again, but i don't know how to do that, i tried some things, but it didn't work out right...
Does somebody know how to solve this problem?
Thanx in advance...
 
The answer to ur problem is quite simple. If u want to check both the x and y values of the block then i suggest u use a double dimensioned array e.g: arr1(1 to 50, 1 to 50)

if u want to check only the y value then i suggest that u use a single dimensioned array e.g: arr2(1 to 50). Note the number of elements are RANGING from 1 to 50.

Incase of the double dimensioned array u can treat is as a
2x2 Matrix.

Now coming to storing of values. You can store values as follows. Just take a look at the following program.

10 CLS
20 DIM Y(1 TO 20) AS INTEGER 'Define an integer array
30 DIM I AS INTEGER
40 DIM CTR AS INTEGER
40 INPUT "Enter no. of inputs:",I
50 FOR CTR = 1 TO 20
60 INPUT "ENTER A VALUE : ", Y(CTR)
70 NEXT CTR
80 CLS
90 PRINT Press Any Key to print the all values"
100 SLEEP
110 FOR CTR = 1 TO 20
120 PRINT "VALUE OF ELEMENT #"+LTRIM$(STR$(Y(CTR))), Y(CTR)
130 NEXT CTR
140 SLEEP
150 CLS
160 END

This example is supposed to be used with qb1.1 and higher. The line numbers are unnecessary in qb4.5 and higher versions.

Using double dimensioned arrays is simple. And similar to single dimensioned arrays.

Hope this helps!
 
Thanx for you reply,
the only thing a don't understand yet is how to avoid that y-values are used more then 1 time.
so i thought this would be the answer (but doesn't work):

cls
screen 9
d = 4
dim y(0 to d) as integer
dim x as integer
dim q as integer
window (-2, -2)-(d+2, d+2)
randomize timer
for x = 0 to d
for q = 1 to (x+1)
t = x + 1
begin:
y(x) = int(rnd * (d +1 ))
if ltrim%(y(x)) = y(x) then goto begin else
line (x, y)-((x+1), (y+1)), t, bf
next q
next x
end

so i need to compare the stored y-value with the actual new y-value, those can't be the same, else it will print a block at an y-value which is already used.
do you know how i can avoid this ?
thanx in advance
 
Hi,

Without going into to much of your program, I would go about it this way

cls
screen 9
d = 4
dim a as integer
dim b as integer

dim y(0 to d) as integer

for a=0 to d:y(a)=0:next a 'to clear the array

dim x as integer
dim q as integer
.
.
.

Retry:
b = 'Your random function' 'Get random number & put in b
If y(b)<> 0 then Retry 'if b already used then retry
y(b)=b 'y(b) now is your Y value
.
.
Rest of program

You have to be careful with this as the more the y(b) array becomes full the slower it gets. If it becomes full then it will loop in the Retry forever. you would need a check for this. A simple counter would do. I.e. if the counter = the size of the array d then exit program
Any Help, Yes/No let me know

Regards

________________________________________
Is not a fool a wise man in his own eyes - Proverbs
 
The answer is simple. In order to avoid drawing over the blocks or avoiding repition just use the following logic.

Randomize Timer

dim Arr%(1 to 15)
For q% = 1 to NofBars%

Retry:
l% = (random number generating statement)
for n% = 1 to q%
if Arr%(n%) = l% then
Flag%=0
goto Retry
else
Flag% = 1
endif
next n%
if Flag%=1 AND q% = n% then Arr%(q%) = l%
next q%

There may be some flaw with the above program but its just an example. The idea is to check the values of all the previous elements and see if all the values dont match the current random values. e.g:

Say...
Arr%(1)=5
Arr%(2)=7
Arr%(3)=3
Arr%(4)=59
Arr%(5)=45

current random number = 7

check from 1 to 5
it matches element 2. Hence select another random number.

now
current random number = 55 ...(Say)
check from 1 to 5
it does not match any element. Hence store it as the 6th element.

Hope that this helps u.
Please reply if it satisfies ur query or else i will post another reply



 
Hey,

I'm sorry i couldn't reply any sooner, my computer crashed and with all the busyness around christmas and other things, it took a while.
Anyway, thanx for your replies, I really appreciate!
Although I still don't get it to work, i tried and tried, but it doesn't work, which is probably my fault...
But I'll keep on trying, if you have any tips, please let me know, thanx in advance!

(One of the most common problems i have is this error: &quot;subscript out of reach&quot;)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top