1,105,534 Community Members

Determining an Array's Length in Pascal

Member Avatar
Scottie_uk
Newbie Poster
1 post since Sep 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
pty
Posting Pro
529 posts since Oct 2005
Reputation Points: 54 [?]
Q&As Helped to Solve: 44 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Length(anArray)
Member Avatar
codewritinfool
Newbie Poster
5 posts since Oct 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
needs_help
Junior Poster in Training
59 posts since Dec 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
codewritinfool
Newbie Poster
5 posts since Oct 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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);

Member Avatar
needs_help
Junior Poster in Training
59 posts since Dec 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
codewritinfool
Newbie Poster
5 posts since Oct 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

dev-pascal is an IDE, not a compiler. It commonly uses Free Pascal or GNU Pascal.

I've already shown how to do it in Free Pascal.

In GNU Pascal, you can use Schemata:

http://www.mirrorservice.org/sites/www.gnu-pascal.de/demos/schemademo.pas

Member Avatar
fhgfjhgfhgfh
Newbie Poster
1 post since Oct 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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;

Member Avatar
FlamingClaw
Posting Pro
559 posts since Feb 2009
Reputation Points: 98 [?]
Q&As Helped to Solve: 138 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
Morten Brendefu
Light Poster
44 posts since Mar 2011
Reputation Points: 28 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
luisin22
Newbie Poster
1 post since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
pritaeas
mod_pritaeas
11,315 posts since Jul 2006
Reputation Points: 1,420 [?]
Q&As Helped to Solve: 1,835 [?]
Skill Endorsements: 155 [?]
Moderator
Featured
Sponsor
 
0
 

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.

Member Avatar
pty
Posting Pro
529 posts since Oct 2005
Reputation Points: 54 [?]
Q&As Helped to Solve: 44 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
Smileydog
Newbie Poster
6 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article