INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Two dimensional Arrays using Read data

Two dimensional Arrays using Read data

(OP)

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.

RE: Two dimensional Arrays using Read data

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

http://raqxtr.sstar.com/caspdoc/html/chapter_5__de...

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close