I am a mainly Java and VB programmer. However, I am writing a program that generates computer code in various languages. One of these is Pascal.

What I am asking is simple.

To detemine the size of an array in Java and VB I would enter:

anArray.length

What is the Pascal equivalent?? I have looked arround the web and found no info. Is there no such equivalent??

Thanks for your kind help

Andrew.

Recommended Answers

All 13 Replies

I've not used Delphi for a while but is it something like

Length(anArray)

I am a mainly Java and VB programmer. However, I am writing a program that generates computer code in various languages. One of these is Pascal.

What I am asking is simple.

To detemine the size of an array in Java and VB I would enter:

anArray.length

What is the Pascal equivalent?? I have looked arround the web and found no info. Is there no such equivalent??

Thanks for your kind help

Andrew.

Use Low() and High() to find the lowest element or highest element index, respectively, of an array.

code

in pascal, you have to predefine all your variable types, including array lengths, so you should already know the length.

in pascal, you have to predefine all your variable types, including array lengths, so you should already know the length.

NOT TRUE. Free Pascal permits dynamic arrays defined like this:

Type
TByteArray : Array of Byte;
Var
A : TByteArray;

SetLength(A,1000);


After a call to SetLength, valid array indexes are 0 to 999: the array index is always zero-based.

Borland Object Pascal (Delphi) permits dynamic arrays defined like this:

var
byteArray : Array of Byte;

SetLength(byteArray, 5);

Ok, but in dev-pascal, you can't change the length.

it is declared at the beginning in the form:
"array name" : array["lower limit".."upper limit"] of string;
(The quotation marks are where you put in your own thing but do not put the quotation marks in the actual program.)
Eg.
Favourite_car : array[1..10] of string;

array lenght = size of the element type * index

You can find the size of the array in the memory as I show it above. Delphi (object pascal) has dinamic array, that we can set the length by the SetLength(array_name, element_number) procedure, so the elements starting from 0 to element_number-1. And as said, High(array_name) the upper indenx of the array, and the Low(array_name) the lowest.

in pascal, you have to predefine all your variable types, including array lengths, so you should already know the length.

For most programs we need to write, this may be true.
There are however many cases where we need to find the length of an array.

An array can be defined as open.

VAR
  Example:ARRAY OF STRING;
  Example2:ARRAY OF ARRAY OF INTEGER;

These arrays are resizeable in runtime :-)

SetLength(Example,10);
SetLength(Example2,10,10);

A multidimensional array is nice to use this way. Each sub array can be defined separately.

SetLength(Example2,5);
SetLength(Example2[0],10);
SetLength(Example2[1],6);
SetLength(Example2[2],90);
SetLength(Example2[3],12);
SetLength(Example2[4],19);

Instead of creating an array that is holding all the possible records our program can possibly need, we can design the program to only use as much memory as is actually needed on demand.

In these cases with SetLength, it makes sense to use the command HIGH(Example2[4]) in order to read out its set length.
Since SetLength always start an array at [0], the command LOW will always return Zero.

PROCEDURE SomeParameters(CONST Params:ARRAY OF INTEGER);
  VAR
    x:BYTE;
  BEGIN
//    HIGH(Params) will return the number of integers passed in the Params array.
    FOR x:=0 TO HIGH(Params) DO BEGIN
      // Code for whatever you would like to do with the parameters.
    END;
  END;

And again... When an array is passed as parameters, to my knowledge it always start with [0], so LOW(Params) will always return Zero.

IF you however declare an array from scratch in VAR section of program.

VAR
  Example : ARRAY[7..14];
BEGIN
  LOW(Example)// Should return 7, but we know this already as it is already defined.
END.

:icon_cheesygrin:
Best regards.

Program sba2(INPUT,OUTPUT);
var counter, count, totalfee, entry:Integer;
last:string;
last:array [1…999] of string;
fee:array[1…999] of integer;
money:array [1…999] of integer;
Begin
counter:=0;
count:=0;
totalfee:=0;
entry:=0;
Write('Enter number of money spent $: ');
readln (spent);
while spent <> 0
Begin
counter=counter+1
Write('Enter Tourist last name: ');
Readln( last[counter]);
Gst:= spent * 0.10;
Environment: = spent * 0.02;
Departure:= 37.50;
fee(counter) = gst + environment + departure;
money(counter) = spent;
Write( 'Enter number of money spent $: ');
readln (spent);
end;
Write('Tourist Last Name',' ', 'Money Spent', ' ', 'Tax paid');
for count=1 to counter
write('last(count),' ', money(count), ' ', fee(count));
totalfee=totalfee+fee(count)
totalspent = totalspent + money(count)
Begin
Write('The total tax collected by all tourist is: $', totalfee);
Write('The total money spent by all tourist is: $', totalspent);
Write('Lets search the list');
Write('Enter surname of tourist');
readln(surname$);
if surname$ = last(counter) then
write('Surname:', last(counter));
write('Money spent: $', money(counter));
write('Tax paid: $', fee(counter));
readln;
end if
End.

Start a new thread for this, instead of hijacking an old one, and please use code tags. Do not forget to post your error too.

Threads should be automatically locked after a few months so idiots like luisin22 can't resurrect them in order to find an answer to a homework question that he's too lazy to do properly.

Program sba2(INPUT,OUTPUT);
var counter, count, totalfee, entry:Integer;
last:string;
last:array [1…999] of string;
fee:array[1…999] of integer;
money:array [1…999] of integer;
Begin
counter:=0;
count:=0;
totalfee:=0;
entry:=0;
Write('Enter number of money spent $: ');
readln (spent);
while spent <> 0
Begin
counter=counter+1
Write('Enter Tourist last name: ');
Readln( last[counter]);
Gst:= spent * 0.10;
Environment: = spent * 0.02;
Departure:= 37.50;
fee(counter) = gst + environment + departure;
money(counter) = spent;
Write( 'Enter number of money spent $: ');
readln (spent);
end;
Write('Tourist Last Name',' ', 'Money Spent', ' ', 'Tax paid');
for count=1 to counter
write('last(count),' ', money(count), ' ', fee(count));
totalfee=totalfee+fee(count)
totalspent = totalspent + money(count)
Begin
Write('The total tax collected by all tourist is: $', totalfee);
Write('The total money spent by all tourist is: $', totalspent);
Write('Lets search the list');
Write('Enter surname of tourist');
readln(surname$);
if surname$ = last(counter) then
write('Surname:', last(counter));
write('Money spent: $', money(counter));
write('Tax paid: $', fee(counter));
readln;
end if
End.

Why do you define last twice?
For counter := 1 to counter ???? Need to fix that
you have an odd combination of C and Pascal assignment statements
Fix the stuff I've just mentioned and I'll take another look for more yuck

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.