Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,563 questions

56,781 answers

573 users

How to initialize array elements in Pascal

9 Answers

0 votes
{
  Best Practices for Array Initialization

  1. Use declaration initialization when values are known beforehand.

  2. Use loops for computed or repeated values.

  3. Use SetLength before accessing dynamic arrays.

  4. Use Low() and High() instead of hardcoded bounds.

  5. Use FillChar only for zeroing memory or when you fully
     understand the underlying memory representation.

  6. Prefer readable code over clever initialization tricks.

  7. Use typed constants for fixed lookup tables.

  8. Use dynamic array constructors ([...]) when appropriate
     for concise initialization.
}



program ArrayInitialization_Declaration;

// Initialize During Declaration (Static Arrays)

{
  Title:
  Initializing a Static Array During Declaration

  Description:
  The array elements are assigned values directly
  when the variable is declared.
}

{$mode objfpc}{$H+}

uses
  SysUtils;

var
  Numbers: array[0..4] of Integer = (10, 20, 30, 40, 50);
  I: Integer;

begin
  Writeln('Array contents:');

  for I := Low(Numbers) to High(Numbers) do
    Writeln('Numbers[', I, '] = ', Numbers[I]);
end.



{
run:

Array contents:
Numbers[0] = 10
Numbers[1] = 20
Numbers[2] = 30
Numbers[3] = 40
Numbers[4] = 50

}

 



answered Oct 9 by avibootz
edited Oct 9 by avibootz
0 votes
program ArrayInitialization_IndividualElements;

// Initialize Elements One by One

{
  Title:
  Initializing Array Elements Individually

  Description:
  Each element is assigned separately.
}

{$mode objfpc}{$H+}

uses
  SysUtils;

var
  Scores: array[1..5] of Integer;
  I: Integer;

begin
  Scores[1] := 90;
  Scores[2] := 85;
  Scores[3] := 95;
  Scores[4] := 88;
  Scores[5] := 92;

  Writeln('Scores:');

  for I := Low(Scores) to High(Scores) do
    Writeln('Scores[', I, '] = ', Scores[I]);
end.


{
run:

Scores:
Scores[1] = 90
Scores[2] = 85
Scores[3] = 95
Scores[4] = 88
Scores[5] = 92

}

 



answered Oct 9 by avibootz
0 votes
program ArrayInitialization_Loop;

// Initialize Using a Loop

{
  Title:
  Initializing an Array with a Loop

  Description:
  Values are generated algorithmically.
}

{$mode objfpc}{$H+}

uses
  SysUtils;

var
  Squares: array[1..10] of Integer;
  I: Integer;

begin
  {
    Fill the array with square numbers.
  }
  for I := Low(Squares) to High(Squares) do
    Squares[I] := I * I;

  Writeln('Square numbers:');

  for I := Low(Squares) to High(Squares) do
    Writeln('Squares[', I, '] = ', Squares[I]);
end.


{
run:

Square numbers:
Squares[1] = 1
Squares[2] = 4
Squares[3] = 9
Squares[4] = 16
Squares[5] = 25
Squares[6] = 36
Squares[7] = 49
Squares[8] = 64
Squares[9] = 81
Squares[10] = 100

}

 



answered Oct 9 by avibootz
0 votes
program ArrayInitialization_FillChar;

// Fill Entire Array with Zero Using FillChar

{
  Title:
  Initializing an Array Using FillChar

  Description:
  FillChar sets every byte in memory to a specified value.
  It is commonly used to zero-initialize arrays.
}

{$mode objfpc}{$H+}

uses
  SysUtils;

var
  Values: array[1..10] of Integer;
  I: Integer;

begin
  {
    Set every byte to zero.
    Safe because Integer value 0 consists entirely of zero bytes.
  }
  FillChar(Values, SizeOf(Values), 0);

  for I := Low(Values) to High(Values) do
    Writeln('Values[', I, '] = ', Values[I]);
end.


{
run:

Values[1] = 0
Values[2] = 0
Values[3] = 0
Values[4] = 0
Values[5] = 0
Values[6] = 0
Values[7] = 0
Values[8] = 0
Values[9] = 0
Values[10] = 0

}

 



answered Oct 9 by avibootz
0 votes
program ArrayInitialization_SingleValue;

// Fill Entire Array with One Value Using a Loop

{
  Title:
  Filling an Array with the Same Value

  Description:
  Every element receives the same value.
}

{$mode objfpc}{$H+}

uses
  SysUtils;

var
  Data: array[0..9] of Integer;
  I: Integer;

begin
  for I := Low(Data) to High(Data) do
    Data[I] := 100;

  for I := Low(Data) to High(Data) do
    Writeln('Data[', I, '] = ', Data[I]);
end.


{
run:

Data[0] = 100
Data[1] = 100
Data[2] = 100
Data[3] = 100
Data[4] = 100
Data[5] = 100
Data[6] = 100
Data[7] = 100
Data[8] = 100
Data[9] = 100

}

 



answered Oct 9 by avibootz
0 votes
program ArrayInitialization_DynamicArray;

// Dynamic Array Initialization

{
  Title:
  Initializing a Dynamic Array

  Description:
  Dynamic arrays can grow and shrink at runtime.
}

{$mode objfpc}{$H+}

uses
  SysUtils;

var
  Numbers: array of Integer;
  I: Integer;

begin
  SetLength(Numbers, 5);

  for I := Low(Numbers) to High(Numbers) do
    Numbers[I] := (I + 1) * 10;

  Writeln('Dynamic array contents:');

  for I := Low(Numbers) to High(Numbers) do
    Writeln('Numbers[', I, '] = ', Numbers[I]);
end.


{
run:

Dynamic array contents:
Numbers[0] = 10
Numbers[1] = 20
Numbers[2] = 30
Numbers[3] = 40
Numbers[4] = 50

}

 



answered Oct 9 by avibootz
0 votes
program ArrayInitialization_DynamicConstructor;

// Initialize Dynamic Array with a Constant Expression

{
  Title:
  Dynamic Array Constructor

  Description:
  Create and initialize a dynamic array
  in a single statement.
}

{$mode objfpc}{$H+}

uses
  SysUtils;

var
  Numbers: array of Integer;
  I: Integer;

begin
  Numbers := [5, 10, 15, 20, 25];

  for I := Low(Numbers) to High(Numbers) do
    Writeln('Numbers[', I, '] = ', Numbers[I]);
end.


{
run:

Numbers[0] = 5
Numbers[1] = 10
Numbers[2] = 15
Numbers[3] = 20
Numbers[4] = 25

}

 



answered Oct 9 by avibootz
0 votes
program ArrayInitialization_TwoDimensional;

// Multidimensional Array Initialization

{
  Title:
  Initializing a Two-Dimensional Array

  Description:
  Demonstrates matrix initialization.
}

{$mode objfpc}{$H+}

uses
  SysUtils;

var
  Matrix: array[1..3, 1..3] of Integer;
  RowIndex, ColIndex: Integer;

begin
  for RowIndex := Low(Matrix) to High(Matrix) do
    for ColIndex := 1 to 3 do
      Matrix[RowIndex, ColIndex] := RowIndex * ColIndex;

  Writeln('Matrix contents:');

  for RowIndex := 1 to 3 do
  begin
    for ColIndex := 1 to 3 do
      Write(Matrix[RowIndex, ColIndex]:4);

    Writeln;
  end;
end.


{
run:

Matrix contents:
   1   2   3
   2   4   6
   3   6   9

}

 



answered Oct 9 by avibootz
0 votes
program ArrayInitialization_TypedConstant;

// Typed Constant as Initial Data

{
  Title:
  Using a Typed Constant

  Description:
  Store predefined array data in a constant.
}

{$mode objfpc}{$H+}

uses
  SysUtils;

const
  WeekDays: array[1..7] of string =
  (
    'Monday',
    'Tuesday',
    'Wednesday',
    'Thursday',
    'Friday',
    'Saturday',
    'Sunday'
  );

var
  I: Integer;

begin
  for I := Low(WeekDays) to High(WeekDays) do
    Writeln(WeekDays[I]);
end.


{
run:

Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday

}

 



answered Oct 9 by avibootz
...