Board index » delphi » Pascal Array prob...please help

Pascal Array prob...please help

OK... i have a type and delcared in that type is Parcel_price:array[0..29]of
real;

i have a procedure and the local variable loop:array[0..29] of real;

now what i am trying to do is a for loop

for loop:= 0 to 29 do
One_Parcel_price.Parcel_Price[loop]:=One_parcel_price.Parcel_Price[loop]*(1+
per/100);

and i keep gettin the error message Invalid FOR control variable blinking at
the colon which is positioned after 'for loop:='

whats wrong with it? please can someone email me wuck my coursework has to be
in by friday!!! aaarrrrgggghhh

thank u so much for taking time to help me
Raj;)

 

Re:Pascal Array prob...please help


Quote
> whats wrong with it?
> ... the local variable loop:array[0..29] of real;

Try using an integer variable "loop", no array.
var loop:byte;
for loop:=0 to 29 do (whatever it is supposed to)

Leo Nesemann

Re:Pascal Array prob...please help


On 23 May 2001 21:05:36 GMT, Busta RF sez:

Quote
>i have a procedure and the local variable loop:array[0..29] of real;
>now what i am trying to do is a for loop
>for loop:= 0 to 29 do
>One_Parcel_price.Parcel_Price[loop]:=One_parcel_price.Parcel_Price[loop]*(1+
>per/100);

Leo Nesemann already provided a fine answer as to HOW to fix it, but
the pedant in me can't let you get away without knowing WHY that's
necessary.

You correctly declared your array of Parcel_Price as an array[0..29]
of real numbers.  How do you refer to any one of those array elements?
By the index number in brackets, which you said can be anything from
zero to 29.

When you want to access the fifth price in your list, you would
ask the program for Parcel_Price[ 6 ] (since the first element would
be [0]).  But you can't hard-code the exact item number into your
program, so you have to refer to the [index number] by some
variable name, which you chose to call "loop".

Fine so far... but think about what kind of numbers can go inside
those brackets.  Can you refer to Parcel_Price[ 1, 2, 3, 4, 5,
6...29]?  No, you can only refer to a single item at a time.  Can you
refer to Parcel_Price[ 3.14159 ]?  Not unless you're selling pie (get
it?  Pi?  Oh, skip it...)

The type of number that goes in those brackets is what type
of number your index counter "loop" can be... and that's an
integer.  If you say you want Parcel_Price[ loop ], you mean
that [ loop ] might be zero, or it might be 12, or it might be 29,
but never a whole bunch of numbers at once (an array) and
never a real/floating point number.

THAT'S why you have to use an integer for your array offset
variable type:
var
  loop: integer;

If you're cool with that, I'll make it a little more interesting... as
long as you're absolutely sure that Parcel_Price will never
contain more than 255 elements, "loop" could also be declared
as a single byte:
var
  loop: byte;

Either one will work, and these days we have tons of memory
to throw around so nobody cares about optimizing variables
to use the least amount of memory necessary... so you can
use integer or byte as you wish.  Crusty curmudgeons like
me will say you should use a byte because you never know
when saving even a single byte will help; smarter people
will say you should use an integer so you don't have to
worry about the array going beyond 255 elements.

Good luck!  And let that be a lesson to folks who come in
here saying "Here's my homework assignment, write the
program for me".  This guy did his own work and only
asked for help with a specific part that gave him trouble,
and as a result he has people falling over themselves
offering him plenty of answers.

Good luck, Raj!

Peter B. Steiger
Cheyenne, WY
----
If you reply by email, send it to pbs at com dot
canada (or vice-versa).  All adverti{*word*224}ts will be
returned to your postmaster, eh!

Re:Pascal Array prob...please help


"Peter B. Steiger" <see....@for.email.address> wrote in message
news:3b0c4cce.2764094@news.cheyne1.wy.home.com...

Quote
> When you want to access the fifth price in your list, you would
> ask the program for Parcel_Price[ 6 ] (since the first element would
> be [0]).

Looks like a typo...  "fifth price" would be "[4]" not "[6]".
    first = 0
    second = 1
    third = 2
    fourth = 3
    fifth = 4

Nice explaination.

    - Bill

Re:Pascal Array prob...please help


Quote
> OK... i have a type and delcared in that type is

Parcel_price:array[0..29]of
Quote
> real;
> i have a procedure and the local variable loop:array[0..29] of real;

For loops can only have ordinal values for the "for variable", so you can
use variables such as characters or integers for the variable.  However, you
cannot use real values for a "for loop control" variable, nor can you use
arrays of any type for the loop control variable (I don't care "professors"
if I didn't use the "correct" terms, so don't reply to my answer saying that
I need to learn proper names for it... my reply was proper enough, for it
answered his question just well enough).
Quote
> for loop:= 0 to 29 do

One_Parcel_price.Parcel_Price[loop]:=One_parcel_price.Parcel_Price[loop]*(1+
Quote
> per/100);

Re:Pascal Array prob...please help


On Thu, 24 May 2001 00:21:01 GMT, Bill_Leary sez:

Quote
>Looks like a typo...  "fifth price" would be "[4]" not "[6]".
>    first = 0
>    second = 1
>    third = 2
>    fourth = 3
>    fifth = 4

>Nice explaination.

{groan} No, I just have trouble counting to 5 sometimes.
How embarrassing!

Peter B. Steiger
Cheyenne, WY
----
If you reply by email, send it to pbs at com dot
canada (or vice-versa).  All adverti{*word*224}ts will be
returned to your postmaster, eh!

Re:Pascal Array prob...please help


In article <3b0c4cce.2764...@news.cheyne1.wy.home.com>,
Peter B. Steiger <see....@for.email.address> wrote:

Quote

>If you're cool with that, I'll make it a little more interesting... as
>long as you're absolutely sure that Parcel_Price will never
>contain more than 255 elements, "loop" could also be declared
>as a single byte:
>var
>  loop: byte;

>Either one will work, and these days we have tons of memory
>to throw around so nobody cares about optimizing variables
>to use the least amount of memory necessary... so you can
>use integer or byte as you wish.  Crusty curmudgeons like
>me will say you should use a byte because you never know
>when saving even a single byte will help; smarter people
>will say you should use an integer so you don't have to
>worry about the array going beyond 255 elements.

You also should use integer as the byte would be converted into one
anyway when it is used and this always adds one instruction or two bytes.
So using integers is faster and saves memory.

Osmo

Other Threads