pasl1006.html

来自「This is programing tutorial for people w」· HTML 代码 · 共 156 行

HTML
156
字号
<HTML>

<head>
<LINK href="../style.css" rel="stylesheet" type="text/css">
<TITLE>Pascal Tutorial - Chapter 7</Title></head>
<body background="../tile01.jpg">
<H1><center>Mastering Array</center></h1><p>&nbsp;<br>
<p>Hello  ! Nice to meet you ! It seems you are cleverer day by day.  You  are
even  be able to write simple games ! I have a little problem on my  health
recently, so I have to get some good rest.
Topics that will be discussed this time are :<ol>
<li>What is an array.</li>
<li>Declarations.</li>
<li>Using arrays.</li>
<li>Two-dimensional array.</li>
<li>Multi-dimensional array.</li>
<li>Special case of array.</li></ol><p>Let's begin !
<p>What is an array exactly ? Well it is like a variable that is able to store
a  bunch of data. Think about drawers. Drawers have several latches,  don't
they ? Each latch could keep things safe. Eventhough they have so many latches
and store things, they are called drawers. Confused ? Let's look at the declaration :</p>
<pre>
var
  arrayname : array [x..y] of vartype;
</pre><p>Example :</p><pre>
var
  ourdata : array[1..100] of byte;
  myarray : array[5..25] of char;
</pre><p>
Yes, yes, name it whatever you want. Array declarations are located  inside
the var block. You can declare arrays as global variable or local variable,
just whenever you want it. How to use it ? Suppose, I have an array :</p>
<pre>
var
  x : array[1..30] of byte;
</pre><p>
It  means that x has 30 elements ranging from 1 to 30. Each element can  be
accessed  individually, not affecting others. Suppose I want to access  the
first element, it would be :</p>
<pre>
  x[1]:=10;  { Suppose I want to fill the first element with 10 }
</pre><p>
Then, I would like to access the second element :</p><pre>
  x[2]:=15;
</pre><p>
If  you check the first element, it is still 10. Even I have  assigned  the
second  element with 15, the first one still retain its originality.  Check
out this simple program to gain better understanding :</p><hr><pre>
var
  a : array[1..10] of byte;

begin
  a[1]:=10;
  a[2]:=15;
  a[3]:=a[1]+a[2];
  writeln(a[1]);
  writeln(a[2]);
  writeln(a[3]);
end.
</pre><hr><p>
As you can see that the variable a could store multiple values. Each element
could be accessed without interfering others.</p><p>
In what case the array contribute to the applications in real-life ?
Suppose  you  have a list of names. You would probably prefer  to  have  an
array of names instead of declaring each variable names. Suppose :
It would be nicer to store 10 names with an array like this :</p><pre>
var
  names : array[1..10] of string;
</pre><p>rather than having each variables like this :</p><pre>
var
  name1, name2, name3, name4, name5,
  name6, name7, name8, name9, name10 : string;
</pre><p>
Array  makes us easier in writing the contents. Writing all the names  with
an array names above, would be :</p><pre>

  for i:=1 to 10 do
    writeln(names[i]);
</pre><p>
You  would love doing that rather than writing writeln statements  with  10
variables intact. It is much easier and simpler.</p>
<p>What about if I have a table of x and y, could I access it with array too ?
Yes !  But, you will need a two-dimensional array. It is the same, but  the
declaration is like this :</p>
<pre>
var
  table : array[1..5, 1..3] of byte;
</pre>
<p>That table is 5x3 in size. How to access it ? Well, like this :</p>
<pre>
  table[5,3]:=5;
  table[1,2]:=4;
  table[4,1]:=table[1,2]*table[5,3];
</pre>
<p>Yes, it is pretty much the same. Well, array helps you make spreadsheets,
just like Lotus 1-2-3 or Excel.</p>
<p>How  about the three-dimensional table or more ? It also the same. Look at
the declaration :</p>
<pre>
var
  table3d : array [1..5, 1..4, 1..6] of byte;
</pre><p>Accessing it is all the same :</p>
<pre>
  table3d[3,4,5]:=6;

</pre><p>And so on. You may extend it to 4-D array, like :</p>
<pre>var
  table4d : array[1..2, 1..3, 1..4, 1..5] of byte;
</pre>
<p>Accessing it is analogue to the previous examples. 5-D array, or even 100-D
array is just the same in concept.</p>
<p>Here are the special case of array in Pascal : You can declare array like this :</p>
<pre>
  x   : array[3..40] of integer;
  idx : array['A'..'Z'] of string;
  a   : array['a'..'z'] of byte;
  n   : array[byte] of integer; { The same as array[0..255] of integer; }
</pre><p>Or even like this :</p><pre>
  schedule : array[Monday..Saturday] of string;
  carprice : array[Honda..Suzuki] of longint;
</pre><p>These types of array will be discussed in depth in chapter 10.</p>
<p>Accessing them is just the same :</p><pre>
  x[3]:=4;
  x[1]:=5; {Illegal}
  idx['D']:='Dave';
  idx[1]:='XX'; {Illegal}
  a['a']:=65;
  n[0]:=1;
  n[255]:=10;
  n[5]:=5;
  schedule[Monday]:='Go meet clients at 10am';
  carprice[Honda]:=15000; {US$}
</pre><p>
Well, well ! Got to practise a lot. Accessing array idx and a with loop  is
similar. Suppose c is a char variable :</p>
<pre>
  for c:='A' to 'Z' do
    idx[c]:='';
  for c:='a' to 'z' do
    a[c]:=random(100);
</pre>
<p>This  kind  of array, for me, is seldom used. The more usual ones  are  the
array of schedule and carprice above. They use enumeration types. This will
be discussed in chapter 10.</p>
<p>OK, that's all for this time !</p>
<hr><p><B><H3>Where to go ?</H3></B><p>
<A HREF="../news.html">Back to main page</A><BR>
<A HREF="pasles01.html">Back to Pascal Tutorial Lesson 1 contents</A><BR>
<A HREF="pasq1006.html">To the quiz</A><BR>
<A HREF="pasl1005.html">Back to Chapter 6</A> about procedures and functions<BR>
<A HREF="pasl1007.html">To Chapter 8</A> about processing strings<BR>
<A HREF="../mylink.html">My page of programming link</A><BR>
<A HREF="../faq.html">Contact me here</A>
<hr><P class="cpy">By : Roby Joehanes, &copy; 1996, 2000</P>
</BODY></HTML>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?