Arran
Arran

Reputation: 43

How do I concisely assign array values based on the index?

Xaxis:array[1..10] of integer;
begin 
Xaxis[1] :=10;
Xaxis[2] :=20;
Xaxis[3] :=30;
Xaxis[4] :=40;
Xaxis[5] :=50;
Xaxis[6] :=60;
Xaxis[7] :=70;
Xaxis[8] :=80;
Xaxis[9] :=90;
Xaxis[10] :=100;

is there a simpler and quicker way of declaring values for an array that this in pascal ?

Upvotes: 0

Views: 45

Answers (3)

No'am Newman
No'am Newman

Reputation: 6477

You could also pre-initialise the array by writing

const
 Xaxis: array[1..10] of integer = (10, 20, 30, 40, 50, 60, 70, 80, 90, 100);

This approach is better when there's no simple arithmetical function to initialise the array. Had you written xaxis[1]:= 5, xaxis[2]:= 14, xaxis[3]:= 29, etc, then loops would not have been suitable.

Upvotes: 0

SuperDisk
SuperDisk

Reputation: 2840

Use a for loop:

for num := 1 to High(Xaxis) do 
begin
  Xaxis[num] := num * 10
end;

But first be sure to declare 'num' as an Integer.

Upvotes: 1

durron597
durron597

Reputation: 32323

Use a loop. I forget pascal's syntax but something like Xaxis[index] := index * 10; inside a loop would work.

Upvotes: 0

Related Questions