BasicsTutorial/Multidimensional Arrays

Last-modified: 2008-03-10 (月) 12:08:08

Multidimensional Arrays

多次元配列

Remember our grid?
我々の格子を覚えている?
Well what if we extend the grid along another dimension so that we have a 2D grid.
我々が二次元格子を持つように我々がもう一つの次元に沿って格子を広げるとしたら。

LapTimes#(x,y)

AttemptsPerson1Person2
1
2
3
4
Average

Now we have a multidimensional array with 5*2 slots, that’s 10 values that it can store (actually one more on each axis but we aren’t using the 0th slots).
現在、我々はそれが保存することができる(実は、各々の軸の上のもう1でなく、しかし、我々は、第0のスロットを使っている)10個の値である5*2スロットで、多次元配列を持つ。
The grid now looks like a typical table that you may store values in.
格子は、現在、あなたが値を保存するかもしれない典型的テーブルのように見える。
I have labelled this up for recording times in a 100m sprint just so that it is easier to understand.
ちょうどそれが理解するのがより簡単であるように、私は100mの短距離競走において時間を記録するためにこれに上へラベルをつけた。

Now how would be DIM (I’ll now tell you it stands for dimension) a multidimensional array?
方法がある今、多次元配列をDIM(私は、現在、それが次元を表すとあなたに話す)て?
Like this…
this…のように

Dim LapTimes#(2,5)

We have to specify two values, one for each dimension.
我々は、2値(各々の次元のための1個)を指定しなければならない。
In this case we have 2 along the x axis and 5 along the y axis.
この場合、我々はy軸に沿って5と、x軸に沿って2を持つ。
We separate the values by a comma, otherwise we get a 25 slot one-dimensional array.
我々は値をコンマで区切る、さもなければ、我々は25のスロット一次元の配列を得る。

Notice how I have put a # after the array name, but before the brackets.
括弧の前に以外、私がどのように配列名の後で#を置いたかについて気がつきなさい。
That is so that the array can store float values, it’s the same as regular variables, and if you want a string array use a $ sign.
それはそう、配列がフロート値(実数)を保存することができるということである、それは規則的な変数と同じである、そして、あなたが文字を望むならば、多数は$のサインを使う。

Let’s fill the array with random values between around 10 and 20 seconds.
およそ10と20秒の間で、配列をランダムな値で満たそう。

`Loop through runners
For x=1 to 2
  `Generate a base time so that all the runners times can be about the same
  baseTime = rnd(10)+10
  `Loop through times
  For y=1 to 4
     `Offset each time by a slight bit
     LapTimes#(x,y) = baseTime+(rnd(50)-25)/10.0
     `Add to the average time
     Inc LapTimes#(x,5), LapTimes#(x,y)
  Next y
  `Complete average calculations
  LapTimes#(x,5) = LapTimes#(x,5) / 5.0
Next x

Look carefully at this code to see what I have done.
私が何をしたかについて見るために、慎重にこのコードを見なさい。
What is happening is I have an outside FOR NEXT loop, looping through the runners, then an inside loop, looping through the runner’s times and randomly generating them.
起こっていることは私が次のループのために外側を持つということである。そして、ランナー、そして内部ループによって輪になって、ランナーの時間を通して輪になって、ランダムにそれらを生み出す。
We are also adding to the average lap time We access the multidimensional array values just the same way that we did before ,with the numbers of the slot we want.
我々は、平均的ラップタイムまで、我々が,withの前に我々が欲しいスロットの数をしたまさに同じ方法で、我々が多次元配列値にアクセスするとも付け加えている。

Let’s now print a table to show the values.
すぐに、値を示すために、テーブルをプリントしよう。

`Draw the results up in a table
For x=1 to 2
  `Write runner numbers
  Set cursor x*150,0
  Print "Runner ",x
  `Write lap times and average time
  For y=1 to 5
     Set cursor x*150,y*20
     Print LapTimes#(x,y)
  Next y
Next x
`Write lap time names
For x=1 to 4
  Set cursor 50,x*20
  Print "Lap Time ",x
Next x
`Write "Average"
Set cursor 50,100
Print "Average"
Wait key

I have again used a loop to read from the array, just as I did with a one-dimensional array.
私が一次元の配列でしたちょうどその時、私は配列から読むためにループを再び使用した。
Notice how I also positioned the cursor where I wanted, by multiplying it by the variable in the FOR_NEXT loop.
FOR_NEXTループにおいて変数によってそれを増やすことによって、私もどのように私が必要としたカーソルを置いたかについて気がつきなさい。
This gradually moves the cursor down or across the page as the variable increases.
変数が増加して、これはページの下に、または、全体で徐々にカーソルを動かす。

You can also have more than 2 dimensions, you could have 3 which is like having a 3D grid (imagine a cube divided into smaller cubes).
あなたには2つ以上の次元があることもできる、あなたは3D格子(立方体がより小さな立方体に分かれたと想像する)を持つようである3を持つことができた。
You can have a maximum of 9 like this.
あなたは、このように最高9を持つことができる。

Dim veryLargeArray(4,2,6,5,8,7,3,2,9)

But be careful, using multidimensional arrays can use up memory very fast, and possibly crash the computer, so only use as many slots as you need.
しかし、慎重にしなさい、使っている多次元配列は非常に速く記憶を使い果たすことができて、おそらくコンピュータをクラッシュさせることができて、それで、あなたが必要とするのと同じくらい多くのスロットを使うことができるだけである。

End of Section Tasks

セクション終わりの作業

l Make a system where the user can enter in a username and password and secret message for a database, then the computer stores it so that by entering the user name and password, they can find out their message.
ユーザーがデータベースのためにユーザー名とパスワードと秘密のメッセージにおいて入ることができるシステムを行いなさい、そして、コンピュータはユーザー名とパスワードを入力することによって、それがそれのメッセージがわかることができることをそう保存する。
The program should have a main screen to be able to choose from “New User” and “Enter User Name and Password” by pressing either the return key or space bar.
プログラムは、「新しいユーザー」から選んで、リターンキーかスペース・バーを押すことによって「ユーザー名とパスワードを入力する」ことができる主なスクリーンを持っていなければならない。