Qbasicnews.com

Full Version: DIM structure help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
i am making a program to asign 16 random number 1-16 to 16 inputed names. i cant figuer out how to set up the varriables. i know you need to use DIM names(16,16) or some thing like that.
DIM names(1 TO 16) AS STRING
so that will set up 16 variables with the title of name that will hold the stings. now how would i asign a number to each name?
What do you mean?

like this?
Code:
names(1) = "Billybob"
names(2) = "Bobbybill"
names(3) = "Bubba"

etc....

names(16) = "Bubba-Ray Jr"


Code:
PRINT names(3)
Will display Bubba. You just treat the array and subscript eg. names(#) as a normal variable (in this case, a string).
i need a hash. like this.
bob 1
bill 2
joe 3
sue 4

the naems are printed out with their number.
Code:
DIM names(1 TO 16) AS STRING

FOR i = 1 TO 16
   READ names(i)
NEXT

GOSUB printNames  
END

printNames:
FOR i = 1 TO 16
   PRINT names(i); i
NEXT
RETURN

DATA Adam, Bertha, Charles, Danielle, Eddie, Frances, Gary, Helen, Ian, Jackie, Karl, Lucy, Marcus, Nelly, Oscar, Paula
please give us an example of when you would use something as useless as this.
.........next...............
I'm not sure what you want, but here are two possible ways (untested so you may need to make corrections)
Code:
' This randomizes a list of names, so the index becomes a
' random number assigned to the name.

DIM Names(1 to16) AS STRING

Restore NameList   ' This isn't necessary unless you have multiple data lists
FOR j = 1 TO 16
  READ  Names(j)
NEXT

FOR j= 1 TO 16
  Temp$ = Names(j)
  r = INT(RND * 16 +1)
  Names(j) = Names(r)
  Names(r) = Temp$
NEXT

' your code

END

NameList:
DATA Alice,Bob,Cindy,Dave,and so on

Code:
' This randomizes a list of numbers
DEFINT A-Z
DIM Names(1 to16) AS STRING
DIM RndmNmbr(1 to 16)

Restore NameList   ' This isn't necessary unless you have multiple data lists
FOR j = 1 TO 16
  READ  Names(j)
  RndmNmbr(j) = j
NEXT

FOR j= 1 TO 16
  Temp = RndmNmbr(j)
  r = INT(RND * 16 +1)
  RndmNmbr(j) = RndmNmbr(r)
  RndmNmbr(r) = Temp
NEXT

' your code

END

NameList:
DATA Alice,Bob,Cindy,Dave,and so on
********WAIT*************
********WAIT*************
********WAIT*************

FIRST, a HASH is a MATHEMATICAL EQUATION that can be used to find the POSITION of an index... I think... er..... someone give me a good example.....

Now, for your program, here is how you do it:

Code:
DIM names.string$(1 TO 16) AS STRING
DIM names.index%(1 TO 16) AS INTEGER

'put in your names by assigning to an array manually or through a file...
'.........
'

FOR i% = 1 TO 16
SWAP names.index%(INT(RND*16)+1), names.index%(i%)
NEXT i%

FOR i% = 1 TO 16
PRINT names.string$(names.index%(i%)); names.index%(i%)
NEXT i%
hash mathmatical?? anyway thanks for the code. would show me how to input the names? like
Code:
for i = 1 to 16
input "student name: ",names.string$(i)
next
for a file would be some thing like this
Code:
OPEN "names" FOR INPUT AS #1
FOR i = 1 TO 16
WHILE NOT EOF(1)
INPUT #1 names.string$(i)
WEND
NEXT
Pages: 1 2