newbie basics please - Programmers Heaven

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

#### Categories

Posts: 6Member
I have a random number and would like the result to determine which array to go to.
example:
if 01 to 20 then array A
21 to 30 array B
31 array C
32-37 array D
etc to 100.

thanks

• Posts: 6,349Member
: I have a random number and would like the result to determine which array to go to.
: example:
: if 01 to 20 then array A
: 21 to 30 array B
: 31 array C
: 32-37 array D
: etc to 100.
:
: thanks
:
:
:
:
First you need to define your array type. This is done using the "type" reserved word. Here is a simple example:
[code]
type
TMyArray = array[1..100] of integer;
[/code]
This code defines an integer array with 100 elements. Next you define the necessary array variables with this new type:
[code]
var
ArrayA, ArrayB, ArrayC, ArrayD: TMyArray;
[/code]
All this code is before any of the begins. I would suggest that you pace it just above the word "implementation" in the mainform's unit.
Now for the real code. Here is a small example, which uses a button-click event:
[code]
procedure TForm1.Button1Click(Sender: TObject);
var
UseArray: TMyArray; // This is the actual array to be used
begin
// Select the appropiate array
if SomeInt in [1..20] then
UseArray := ArrayA // Select ArrayA
else
if (SomeInt >= 21) and (SomeInt <= 30) then
UseArray := ArrayB // Select ArrayB
else
if SomeInt = 30 then
UseArray := ArrayC;
// etc.
// Now perform some actions on the UseArray variable
UseArray[1] := 1; // Example: make 1st element "1"
end;
[/code]
This is a very elegant and simple method, which works only if the arrays are of the same type. If this isn't so, then the code becomes slightly more complex:
[code]
procedure UseArrayC;
begin
ArrayC[1] := 1;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
if SomeInt in [1..20] then
begin
ArrayA[1] := 1;
end
else
if (SomeInt >= 21) and (SomeInt <= 30) then
begin
ArrayB[1] := 1;
end
else
if SomeInt = 30 then
UseArrayC;
end;
[/code]
In this case the indentation of the code becomes quite important for the readability. This is especially true if the code processing each array becomes more complex.
I've also demonstated several methods of comparing the value of SomeInt and the ranges to select the arrays. The help files contain more info on the words "in", and "and". In the "and" version, the brackets are especially important, because it uses the following preference:
[code]
1 Boolean reserved words: AND, OR, XOR
2 Comparisons: =, <, <=, etc.
[/code]
So if you leave out the brackets the statement will become this:
[code]
if SomeInt >= (21 and SomeInt) <= 30 then
[/code]
which the compiler cannot understand.
• Posts: 331Member
: : I have a random number and would like the result to determine which array to go to.
: : example:
: : if 01 to 20 then array A
: : 21 to 30 array B
: : 31 array C
: : 32-37 array D
: : etc to 100.
: :
: : thanks
: :
: :
: :
: :
: First you need to define your array type. This is done using the "type" reserved word. Here is a simple example:
: [code]
: type
: TMyArray = array[1..100] of integer;
: [/code]
: This code defines an integer array with 100 elements. Next you define the necessary array variables with this new type:
: [code]
: var
: ArrayA, ArrayB, ArrayC, ArrayD: TMyArray;
: [/code]
: All this code is before any of the begins. I would suggest that you pace it just above the word "implementation" in the mainform's unit.
: Now for the real code. Here is a small example, which uses a button-click event:
: [code]
: procedure TForm1.Button1Click(Sender: TObject);
: var
: UseArray: TMyArray; // This is the actual array to be used
: begin
: // Select the appropiate array
: if SomeInt in [1..20] then
: UseArray := ArrayA // Select ArrayA
: else
: if (SomeInt >= 21) and (SomeInt <= 30) then
: UseArray := ArrayB // Select ArrayB
: else
: if SomeInt = 3[red]1[/red] then
: UseArray := ArrayC;
: // etc.
: // Now perform some actions on the UseArray variable
: UseArray[1] := 1; // Example: make 1st element "1"
: end;
: [/code]
: This is a very elegant and simple method, which works only if the arrays are of the same type. If this isn't so, then the code becomes slightly more complex:
: [code]
: procedure UseArrayC;
: begin
: ArrayC[1] := 1;
: end;
:
: procedure TForm1.Button1Click(Sender: TObject);
: begin
: if SomeInt in [1..20] then
: begin
: ArrayA[1] := 1;
: end
: else
: if (SomeInt >= 21) and (SomeInt <= 30) then
: begin
: ArrayB[1] := 1;
: end
: else
: if SomeInt = 3[red]1[/red] then
: [red]ArrayD[1] := 1[/red];
: end;
: [/code]
: In this case the indentation of the code becomes quite important for the readability. This is especially true if the code processing each array becomes more complex.
: I've also demonstated several methods of comparing the value of SomeInt and the ranges to select the arrays. The help files contain more info on the words "in", and "and". In the "and" version, the brackets are especially important, because it uses the following preference:
: [code]
: 1 Boolean reserved words: AND, OR, XOR
: 2 Comparisons: =, <, <=, etc.
: [/code]
: So if you leave out the brackets the statement will become this:
: [code]
: if SomeInt >= (21 and SomeInt) <= 30 then
: [/code]
: which the compiler cannot understand.
:
just reminding there is a case statement which is more elegant:
[code]
case SomeInt of
1..20: UseArray := ArrayA;
21..30: UseArray := ArrayB;
31: UseArray := ArrayC;
32..37: UseArray := ArrayD;
{...}
end;
[/code]
also, some [red]corrections[/red] in your code ;-)
[hr][red][italic][b]N[/b][/red][blue]et[/blue][red][b]G[/b][/red][blue]ert[/italic][/blue][hr]