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

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

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

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,652 questions

51,529 answers

573 users

How to pad an array to a specified length with a given value in Pascal

1 Answer

0 votes
program ArrayPad;
 
type
    TIntArray = array of Integer;
 
function ArrayPad(arr: TIntArray; originalSize, size, value: Integer): TIntArray;
var
    i: Integer;
begin
    if (size < originalSize) then
        ArrayPad := arr;
    
    SetLength(ArrayPad, size); // dynamic array
 
    for i := 0 to originalSize - 1 do
        ArrayPad[i] := arr[i];
    for i := originalSize to size - 1 do
        ArrayPad[i] := value;
end;
 
var
    arr: TIntArray;
    paddedArray: TIntArray;
    i: Integer;
begin
    arr := TIntArray.Create(1, 2, 3);
    paddedArray := ArrayPad(arr, Length(arr), 5, 0);
 
    for i := 0 to Length(paddedArray) - 1 do
        Write(paddedArray[i], ' ');
end.
 
       
        
(*
run:
     
1 2 3 0 0 
    
*)

 



answered Feb 4, 2025 by avibootz
edited Feb 4, 2025 by avibootz
...