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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Two dimensional Arrays using Read data

Status
Not open for further replies.

Daggos

Technical User
Nov 13, 2012
1
0
0
US

Hi all, new to the forums. I was ill last Thursday and missed alot in class, they went straight past Arrays to two-dimensional Arrays.

I'm required to make a program that will use Arrays, reading data, and displaying the data alongside averages for all.

The data looks somewhat like

DATA 7,9
DATA John Doe, 55, 90, 100, 100, 90, 45, 56, 43, 90
DATA John Doe, 55, 90, 100, 100, 90, 45, 56, 43, 90
DATA John Doe, 55, 90, 100, 100, 90, 45, 56, 43, 90
DATA John Doe, 55, 90, 100, 100, 90, 45, 56, 43, 90
DATA John Doe, 55, 90, 100, 100, 90, 45, 56, 43, 90
DATA John Doe, 55, 90, 100, 100, 90, 45, 56, 43, 90
DATA John Doe, 55, 90, 100, 100, 90, 45, 56, 43, 90


I'm lost, and looking for a reference to learn how I would go about this. Thank you.
 
Google is your friend... You will find LOADS of tutorials regarding reading data. Searching for "qbasic read data into array" returns what you are looking for.

Here's the basic idea.

To read DATA, you use the READ command.

Code:
READ name$
READ age$
READ sex$

PRINT name$, age$, sex$ 

DATA "Dan", 19, "Male"
'prints Dan19Male

The thing you need to realize is that the DATA element pointer moves to the next element (DATA elements are separated by a comma) when the READ command is used, therefore, DATA can be put anywhere in the program.

Code:
DATA Dan, 19, Male

READ name$
READ age$
READ sex$

DATA Mary

READ name$
READ age$
READ sex$

DATA 34, Female

PRINT name$, age$, sex$
'prints Mary34Female

I hope this makes sense.

Notice, the first 2 elements in the DATA (7 and 9) corresponds with the dimensions of the remaining DATA, and thus, the dimensions of the array.

Code:
READ x%
READ y%

DIM myArray(x%, y%) AS STRING

Then read the rest of the DATA into the array.

Code:
FOR j = 0 TO y%
   FOR i = 0 TO x%
      READ myArray(i, j)
   NEXT i
NEXT j

Now, you know that the first element in myArray is a string, and the rest are integers. Add the elements together and divide by number of elements.

Code:
FOR n = 0 TO 7
   FOR i = 1 TO 9
      sum% = sum% + VAL(myArray(n, i))
   NEXT i
   print "The average is: ", (sum% / 9)
NEXT n

NOTE: It's been A LONG time since I've played with qbasic. This is ad-hoc code and has not been tested. Additionally, I cannot guarentee the syntax is correct.

-Geates

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top