Delphi array Definition

Source: Internet
Author: User
Tags array definition

In C Language

Yes: A [5] = {1, 2, 3, 4, 5 };

In Delphi, how should I write the value assignment of this array ??
Question added: I want to assign an initial value when declaring an array. You do not need to know the content of the initial value, all I want to know is the format of the value assignment during the declaration, instead of implementing it through a process.

VaR
A: = array [1 .. 5] of integer = (1, 2, 3, 4, 5)

Only the global variables can be assigned an initial value, and all five values must be filled.

S: array [1 .. 264] of integer;
S: = (1), (2), (2), (1), (1), (2), (2), (1), (2 ), (1), (1), (1), (1), (2), (2), (1), (1), (2), (1 ), (1), (2), (2), (1), (1), (2), (1), (1), (2), (2 ), (2), (1), (2), (1), (2), (1), (2), (2), (2), (2 ), (1), (2), (2), (1), (2), (1), (1), (2), (1), (2 ), (1), (1), (2), (1), (1), (1 ));

S: = (122112221211111111111111222221212121212121212 );

Errors are displayed. The C language is acceptable.

 

Static array definition method
// 1. Standard method:
VaR
Myarr: array [0 .. 10] of integer; // defines a static Array
--------------------------------------------------------------------------------

// 2. Non-0 subscript can be used:
VaR
Myarr: array [9 .. 10] of integer; // cannot be promoted, so it is not easy to communicate with system functions.
--------------------------------------------------------------------------------

// 3. Declare the Array Based on the predefined type:
Type
Tmyarr = array [0 .. 10] of integer; // define an array type first
VaR
Myarr: tmyarr; // redefines static arrays.
--------------------------------------------------------------------------------

// 4. You can assign values directly in the non-process zone:
VaR
Myarr: array [0 .. 2] of integer = (11,22, 33 );
--------------------------------------------------------------------------------

// 5. Multi-dimensional array:
VaR
Myarr: array [0 .. 2, 0 .. 2] of integer;
Begin
// Use
Myarr [1, 2]: = 100;
End;
--------------------------------------------------------------------------------

// 6. Define an Array Based on the subinterface:
Type
Trange = 0 .. 10;
VaR
Myarr: array [trange] of integer;
--------------------------------------------------------------------------------

// 7. Define an Array Based on enumeration:
Type
Tenums = (enum1, enum2, enum3 );
VaR
Myarr: array [tenums] of string;
Begin
Myarr [enum1]: = 'case ';
Showmessage (myarr [enum1]); // In case
End;
--------------------------------------------------------------------------------

// 8. Define an array based on other types:
VaR
Myarr: array [byte] of char;
Begin
Myarr [255]: = #65;
Showmessage (myarr [1, 255]); //
End;

// Try not to use the built-in type. You can create a new type:
Type
Tnewbyte = byte;
VaR
Myarr: array [tnewbyte] of char;
Begin
Myarr [255]: = #65;
Showmessage (myarr [1, 255]); //
End;

// You can also use the type alias:
Type
Tchar = type char;
VaR
Myarr: array [tchar] of byte;
Begin
Myarr ['C']: = 255;
Showmessage (inttostr (myarr ['C']); // 255
End;

// Define the type simultaneously:
Type
Myrec = record
S: string;
R: real;
B: byte;
End;
VaR
Arr1: array [0 .. 100] of myrec;
Arr2: array [0 .. 100] of record S: string; R: real; B: byte; end; // This can be defined directly.
Arr3: packed array [0 .. 100] of myrec; // compressed array definition, as if there is no difference?

 

1. One-dimensional array

A: array of integer;

Setlength (A, 5)

2. Two-dimensional array

Define an integer two-dimensional array
Aimyarray: array of Integer
Size setting
Setlength (aimyarray, 5, 5)

3. Multi-dimensional array Definition
When the element type of a one-dimensional array is also a one-dimensional array, a two-dimensional array is formed. The general format defined by two-dimensional arrays:
Array [subscript type 1] of array [subscript type 2] of element type;
However, we generally define two-dimensional arrays as follows:
Array [subscript Type 1, subscript type 2] of element type;
Note: The two subscript types are the same as the definition of one-dimensional arrays. They can be regarded as "lower bound 1... upper bound 1" and "lower bound 2... Upper Bound 2 ",
The following table lists the ranges in which each element (double subscript variable) in a two-dimensional array can use the following values. Of is the base type.
Generally, the n-dimensional array format is:
Array [subscript Type 1, subscript type 2 ,..., Subscript type N] of element type;
The number of subscript types is the dimension of the array, and the type and value range of each subscript are described.

** Multi-dimensional array element reference
The array element reference of a multi-dimensional array is similar to that of a one-dimensional array element reference. The difference is that the multi-dimensional array element reference must provide multiple subscripts.
The reference format is:
<Array name> [subscript 1, subscript 2 ,..., Subscript N]
Note: Obviously, the type of each subscript expression should be the same as that of the corresponding subscript type, and the value cannot exceed the range specified by the subscript type.
For example, there are instructions:

Reference content: type Matrix = array [1 .. 5, 1 .. 4] of integer;
VaR A: matrix;

It indicates that A is a two-dimensional array with 5*4 = 20 elements. They are:

Reference content
A [] a [] a [] a []
A [2, 1] a [2, 2] a [2, 3] a [2, 4]
A [3, 1] a [3, 2] a [3, 3] a [3, 4]
A [4, 1] a [4, 2] a [4, 3] a [4, 4]
A [5, 1] a [5, 2] a [5, 3] a [5, 4]

Therefore, we can regard it as a matrix. A [4th] indicates the elements of 2nd rows and columns. Because the computer memory is one-dimensional,
To store the elements of a two-dimensional array in the memory, Pascal stores the elements in the order of rows (the first subscript), that is, by a [] a [1,
2] a [1, 3] a [1, 4] a [2, 1]…, The sequence of a [5, 4] is stored in a group of continuous storage units in the memory.
For element references of the entire two-dimensional array, the dual loop is mostly used. For example, assign a two-dimensional array a as described above
Value:

Reference content A [I, j] = I * j.
For I: = 1 to 5 do
For J: = 1 to 4 do
A [I, j]: = I * J;

The input and output of two-dimensional arrays can also be achieved through a dual loop:

Reference content: for I: = 1 to 5 do
Begin
For J: = 1 to 4 do read (A [I, j]);
End;
For I: = 1 to 5 do
Begin
For J: = 1 to 4 Do write (A [I, j]: 5 );
End;

Example of multi-dimensional array Application

Example 7: Set a program:

Reference content: Program ex5_3;
Const
N = 3;
Type
Matrix = array [1. N, 1. N] of integer;
VaR
A: matrix;
I, J: 1. N;
Begin
For I: = 1 to n do
Begin
For J: = 1 to n do
Read (A [I, j]);
Readln;
End;
For I: = 1 to n do
Begin
For J: = 1 to n do
Write (A [J, I]: 5 );
Writeln;
End;
End.

The input for running the program is:

Reference content 2 □1 □3 ←
3 □3 □1 then done
1 □2 □1 then done

The program output should be:

Reference content 2 □3 □1
1 □3 □2
3 □1 □1

Example 8: Enter the scores of four students in five courses: mathematics, physics, English, chemistry, and Pascal to obtain the average score of each student,
Print the table.
Analysis: the data is stored in two-dimensional array A. The first subscript represents the student's student ID, and the second subscript represents the student's score in a certain subject, as shown in
A [I, 1], a [I, 2], a [I, 3], a [I, 4], a [I, 5] The math, physics, English, chemistry, and Pascal of student I
Because the total score and average score of each student are required, the second subscript can be used to open two more columns, each of which contains five
Performance, total score, and average score. The source program is as follows:

Reference content: Program ex5_4;
VaR
A: array [1 .. 4, 1 .. 7] of real; I, J: integer;
Begin
Fillchar (A, sizeof (A), 0); {The fillchar function is used to set all elements in a to 0}
Writeln ('enter 4 students score ');
For I: = 1 to 4 do
Begin
For J: = 1 to 5 do {read each person's score of 5 subjects}
Begin
Read (A [I, j]); {count the total score of each subject at the same time}
A [I, 6]: = A [I, 6] + A [I, j];
End;
Readln;
A [I, 7]: = A [I, 6]/5; {average score}
End;
Writeln ('No. Mat. Phy. Eng. Che. Pas. Tot. ave. '); {output orders table}
For I: = 1 to 4 do
Begin
Write (I: 2 ,'');
For J: = 1 to 7 do
Write (A [I, j]: 9: 2 );
Writeln; {line feed}
End;
End.

Example 9: enter a string of no more than 100 characters and end. Determine whether they constitute a background.
Analysis: the so-called back-to-text refers to reading a string of characters from left to right and from right to left, such as 12321, abcba, and AA. Read first
Judge a string of characters (put in the array letter), remember the length of the string, and then compare the first and last characters, and keep moving closer to the middle,
Then you can determine whether it is a response.
The source program is as follows:

Reference content: Program ex5_5;
VaR
Letter: array [1 .. 100] of char;
I, J: 0.. 100;
Ch: Char;
Begin

{Read a string ending}
Write ('input a string :');
I: = 0; read (CH );
While ch <> '.' Do
Begin
I: = I + 1; letter [I]: = CH;
Read (CH)
End;

{Checking whether it is a reply}
J: = 1;
While (j <I) and (letter [J] = letter [I]) Do
Begin
I: = I-1; J: = J + 1;
End;
If j> = I then writeln ('Yes .')
Else writeln ('no .');
End.

Example 10: Odd-level magic Arrays
Magic arrays use natural numbers 1, 2, 3 ..., N2 fill in the positions of each element in the n-order square matrix, so that the sum of elements in each row of the square matrix
And the sum of the primary diagonal elements are equal. An Algorithm of the odd-order magic array is to sort the natural number series from the last position in the middle row of the phalanx.
From the beginning, each time it is always arranged in the lower right corner (that is, the next part of AIJ is Ai + 1, J + 1 ). However, in the following four cases, the row number method should be corrected.
(1) After the column is completed (j = n + 1), the first column is displayed. (2) after the row is completed (I = n + 1 ), the first line is displayed;
(3) The next to an, n is always an, n-1; (4) if AIJ has been arranged into a natural number, then Ai-1, J-2.
For example, the third-order square matrix can be arranged according to the above algorithm:

4 3 8
9 5 1
2 7 6

With the above algorithms, the main idea of solving problems can be described by pseudocode as follows:

1 I? N Div 2 + 1, y? N/* the initial position of the number of rows */
2 A [I, j]? 1;
3 For K: = 2 to N * n do
4. Calculate the number of the next row! (I, j );
5 If a [I, j] <> 0 then
6 I? I-1;
7 J? J-2;
6 A [I, j]? K;
7 endfor

For calculating the number position of the next row, the preceding four cases are used, but we should first handle the third case. The algorithm is described as follows:

1 if (I = N) and (j = N) then
2 J? J-1;/* the next position is (n, n-1 )*/;
3 else
4 I? I mod n + 1;
5 J? J mod n + 1;
6 endif;

The source program is as follows:

Reference content: Program ex5_7;
VaR
A: array [1 .. 99, 1 .. 99] of integer;
I, j, k, n: integer;
Begin
Fillchar (A, sizeof (A), 0 );
Write ('n' = '); readln (N );
I: = N Div 2 + 1; J: = N;
A [I, j]: = 1;
For K: = 2 to N * n do
Begin
If (I = N) and (j = N) then
J: = J-1
Else
Begin
I: = I mod n + 1;
J: = J mod n + 1;
End;
If a [I, j] <> 0 then
Begin
I: = I-1;
J: = J-2;
End;
A [I, j]: = K;
End;
For I: = 1 to n do
Begin
For J: = 1 to n do
Write (A [I, j]: 5 );
Writeln;
End;
End.

 

 

2. Two-dimensional array
Merge definition:
VaR array name: array [subscript Type 1, subscript type 2] of element type;
The number of subscript types is the dimension of the array, and the type and value range of each subscript are described.
For example, the definition is Var A: array [1 .. 4, 1 .. 3] of integer;
It indicates that A is a two-dimensional array with 4*3 = 12 elements. They are:
A [] a [] a []
A [2, 1] a [2, 2] a [2, 3]
A [3, 1] a [3, 2] a [3, 3]
A [4, 1] a [4, 2] a [4, 3]
Therefore, we can regard it as a matrix. A [4th] indicates the elements of 2nd rows and columns.
Although the two-dimensional array can be viewed as a table or a matrix logically, the storage units corresponding to all elements of the two-dimensional array are continuous within the computer, the storage method is essentially the same as that of one-dimensional arrays.

For element references of the entire two-dimensional array, the dual loop is mostly used. For example, assign a value to the two-dimensional array a defined in the preceding example:
For I: = 1 to 4 do
For J: = 1 to 3 do a [I, j]: = I * J;
The Dual Loop is also used to implement the input and output of two-dimensional arrays:
For I: = 1 to 4 do
For J: = 1 to 3 do read (A [I, j]);
For I: = 1 to 4 do
Begin
For J: = 1 to 3 do write (A [I, j]: 5 );
Writeln;
End;
Example 5: In the known array A, each element a (I, j) occupies three bytes during storage, so that I changes from 1 to 8, and J changes from 1 to 10, memory Allocation starts from the address SA and continues row-based storage allocation. Question: The starting address of a (5, 8) is (). {Noip2000 High School single answer 13 questions}
A) SA + 141 B) SA + 180 C) SA + 222 d) SA + 225
Example 6: Read the following sections carefully:
VaR A: array [1 .. 3, 1 .. 4] of integer; {noip2000 High School single choice 16 questions}
B: array [1 .. 4, 1 .. 3] of integer;
X, Y: integer;
Begin
For X: = 1 to 3 do
For Y: = 1 to 4 Do A [x, y]: = x-y;
For X: = 4 downto 1 do
For Y: = 1 to 3 do B [x, y]: = A [y, X];
Writeln (B [3, 2]);
End.
The correct output of the program segment listed above is ().
A)-1 B)-2 c)-3 D)-4
Analysis: array A: array B:
0-1-2-3 0 1 2
1 0-1-2-1 0 1
2 1 0-1-2-1 0
-3-2-1
Example 7: Enter the scores of four students in five courses (mathematics, physics, English, chemistry, and Pascal), calculate the average score of each student, and print out the table.
VaR A: array [1 .. 4, 1 .. 7] of real; {subscript 1 student ID, subscript 2 Student Score}
I, J: integer; {subscript 2 + two columns, each containing the total score and average score}
Begin
Fillchar (A, sizeof (A), 0); {use the fillchar function to set all elements in array a to 0}
Writeln ('input 4 students score ');
For I: = 1 to 4 do
Begin
For J: = 1 to 5 do
Begin
Read (A [I, j]); A [I, 6]: = A [I, 6] + A [I, j]; {count the total score for each score}
End;
A [I, 7]: = A [I, 6]/5; {average score}
End;
Writeln ('No. Math PHY Eng che pas total average ');
For I: = 1 to 4 do
Begin
Write (I ,'');
For J: = 1 to 7 do write (A [I, j]: 7: 1 );
Writeln;
End;
End.
Example 8: Odd cube arrays. The output is an integer ranging from 1 to N2 (N is Qi). A matrix of n × n is arranged, so that the sum of each row, column, and diagonal line in the matrix is the same.
Odd level magic array algorithm:
① Fill 1 in the middle of the first line, that is, [1, (n + 1)/2], and then fill in 2 to N2 in sequence;
② If the current position is [I, j], the next fill in [I-1, J-1]; if there is no space in the upper left, that is, when the J-1 is 0, fill in [N, J-1];
③ If the J-1 is 0, fill in the [I-1, N];
④ If the current position is [I, j], and a number exists in the upper-left corner, fill in the front and bottom, that is, [I + 1, J];
For example, the third-order square matrix can be arranged according to the above algorithm:
6 1 8
7 5 3
2 9 4
VaR A: array [1 .. 100, 1 .. 100] of integer;
I, j, k, n, H, L: integer;
Begin
Fillchar (A, sizeof (A), 0 );
Write ('input N: '); readln (N );
J: = (n + 1)/2;
I: = 1; A [I, j]: = 1;
For K: = 2 to N * n do
Begin
H: = I-1; if h = 0 Then H: = N;
L: = J-1; if l = 0 then l: = N;
If a [h, l] = 0 then begin a [h, l]: = K; I: = H; J: = l end;
Else begin a [I + 1, J]: = K; I: = I + 1 end;
End;
For I: = 1 to n do
Begin
For J: = 1 to n do write (A [I, j]: 5 );
Writeln
End
End.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.