Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need Help with an array
#1
I recently received a school assignment that I'm having a tough time with, mostly with the array and DIM statement. See below for data supplied:

10 tanks of gas.

Tank Gallons Miles

1 17.2 638
2 19.5 661
3 16.8 610
4 17.7 573
5 18.1 639
6 10.2 379
7 16.5 635
8 19.1 550
9 18.6 714
10 17.4 555

Would I need to have two arrays, one for gallons and miles.
Reply
#2
Yes

Or one using UDTs (User Defined Types)
UDT example:
Code:
Type MyType
x as long
y as double
End Type
Dim Myarray(-32768 To 32767) As MyType

MyArray(3242).x = 50
MyArray(3242).y = 66.55443322
Errors in code, it will never work, just an example.
Reply
#3
okay, you want to use the DIM statement to let the program know what kind of variables are in the array, and what kinda data it holds:

DIM arrayname (firstindex TO lastindex) AS arraytype

the lowercase stuff you need to fill in:

arrayname: name your array something
firstindex/lastindex: this determines the array size. if you wanted an array of just two numbers you could do (1 TO 2)
arraytype: this tells the program what kind of information the array holds. it looks like your program wants miles and gallons. miles looks to be whole numbers, so use INTEGER arraytype. gallons has a decimal point, so use SINGLE arraytype.

example: DIM testscores(1 to 500) AS INTEGER

this would make an array that could hold 500 students test scores, each one a whole number integer.

then, you can reference individual test scores by using the index number. for example, the tenth student in the class gets a 56% on their test:

testscores(10) = 56

what did the 85th person get on their test? let's find out:

PRINT testscores(85)

get it?

lemme know if you've any more questions, and if you figure the problem out, post your code for us :)

*peace*

Meg.
Reply
#4
Now I didn't read Meg's post but wouldn't Rcarpent98
just need to do something like this.

Tank Gallons Miles
Code:
DIM tank(10)       DIM gallon(10)          DIM mile(10)
tank(1) = 1        gallon(1) = 17.2        miles(1) = 638
tank(2) = 2        gallon(2) = 91.5        miles(2) = 661
tank(3) = 3        gallon(3) = 16.8        miles(3) = 610
tank(4) = 4        gallon(4) = 17.7        miles(4) = 573
tank(5) = 5        gallon(5) = 18.1        miles(5) = 639
tank(6) = 6        gallon(6) = 10.2        miles(6) = 379
tank(7) = 7        gallon(7) = 16.5        miles(7) = 635
tank(8) = 8        gallon(8) = 19.1        miles(8) = 550
tank(9) = 9        gallon(9) = 18.6        miles(9) = 714
tank(10) = 10      gallon(10) = 17.4       miles(10) = 555

Depending on what he uses $, ! ect....
Reply
#5
Gin,

I like your straight to the point example.

Meg's text book chapter on arrays is very good, but a picture is worth a thousand words.
*****
Reply
#6
Hey thanx alot Moneo, I'm glad you enjoyed my simple example. Sorry if it's taken me this long to post back, I'm always busy I don't have time to come here offten.
Reply
#7
rcarpent98,

Not to confuse you, but here is yet another way of accomplishing your goal.

(This is just a rough version of it, so feel free to make whatever changes you want. :wink: )


' ***** TANK MILE GALLON PROJECT *****
CLS
DIM TANK(10): DIM G(10): DIM M(10): G = 0: M = 0
FOR T = 1 TO 10: G = G + 1: M = M + 1
READ TANK(T), G(T), M(T)
PRINT TANK(T); G(T); M(T), : NEXT

' (Of course you wouldn't need the:"PRINT TANK(T);G(T);M(T)...etc.
' for the actual program.
' I just added that so you can see the see the results.)


' ***** TRY IT OUT! *****

INPUT "WHICH FILE DO YOU WISH TO CHECK "; F
PRINT TANK(F); G(F); M(F)
END


'Tank Gallons Miles

DATA 1, 17.2, 638
DATA 2, 19.5, 661
DATA 3, 16.8, 610
DATA 4, 17.7, 573
DATA 5, 18.1, 639
DATA 6, 10.2, 379
DATA 7, 16.5, 635
DATA 8, 19.1, 550
DATA 9, 18.6, 714
DATA 10, 17.4, 555

I don't know if you are allowed to use DATA statments or not on your assignment, so this is just another option for you if you are.
adsherm
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)