Board index » delphi » Declaring constants

Declaring constants

I would be grateful for some help; I'm a beginner having trouble working out
how to do the following (crippled by the fact that my help file is in
German).

I am trying to declare an array of numbers whose index will be a longint,
but cannot fathom the usage.  I need a to be a longint constant, and p to be
an array 1..a of longints.  As far as I see from examples the following
ought to work, but always returns error 133 at the end of the array here:

const a: longint = 100000;
var p: array[1..a] of longint;

Many thanks, Richard.

 

Re:Declaring constants


You do not say what compiler you are using (I'm guessing Borland, whose details
I admit I do not know).  My answers will assume Standard Pascal.

1)  In Standard Pascal, constants do not have types.  You would simply say

  CONST
    a = 100000;

2)  You are trying to create an array of 100,000 long integers.  Some compilers
have restrictions on the amount of memory you can allocate all at once, with
that limit being "related to" 65535 (I'm unsure if the limit is number of bytes,
number of elements, etc).  In any case, 100000 may simply be too big for your
system.

Are you really planning to generate 100,000 numbers?

Bob Schor
Pascal Enthusiast

Quote
Hepworth wrote:
> I would be grateful for some help; I'm a beginner having trouble working out
> how to do the following (crippled by the fact that my help file is in
> German).

> I am trying to declare an array of numbers whose index will be a longint,
> but cannot fathom the usage.  I need a to be a longint constant, and p to be
> an array 1..a of longints.  As far as I see from examples the following
> ought to work, but always returns error 133 at the end of the array here:

> const a: longint = 100000;
> var p: array[1..a] of longint;

> Many thanks, Richard.

Re:Declaring constants


Thanks (I'm using TP7 for dos).  I have to admit that I'm not quite sure
where I'm going with this.  My task is to identify all primes less than a
certain (big) number, and for large enough numbers there will be loads of
them, so yes I need a large (though not necessarily that large) array.  I
was trying to see if I was making a glaring program mistake or whether I
want to go back to the maths and start again.

Quote
Bob Schor wrote in message <38F5BD9E.E37B...@pitt.edu>...
>You do not say what compiler you are using (I'm guessing Borland, whose
details
>I admit I do not know).  My answers will assume Standard Pascal.

>1)  In Standard Pascal, constants do not have types.  You would simply say

>  CONST
>    a = 100000;

>2)  You are trying to create an array of 100,000 long integers.  Some
compilers
>have restrictions on the amount of memory you can allocate all at once,
with
>that limit being "related to" 65535 (I'm unsure if the limit is number of
bytes,
>number of elements, etc).  In any case, 100000 may simply be too big for
your
>system.

>Are you really planning to generate 100,000 numbers?

>Bob Schor
>Pascal Enthusiast

>Hepworth wrote:

>> I would be grateful for some help; I'm a beginner having trouble working
out
>> how to do the following (crippled by the fact that my help file is in
>> German).

>> I am trying to declare an array of numbers whose index will be a longint,
>> but cannot fathom the usage.  I need a to be a longint constant, and p to
be
>> an array 1..a of longints.  As far as I see from examples the following
>> ought to work, but always returns error 133 at the end of the array here:

>> const a: longint = 100000;
>> var p: array[1..a] of longint;

>> Many thanks, Richard.

Re:Declaring constants


problem 1: turbo pascal accepts that. :)
problem 2: free pascal accepts that. :)

However, the problem that causes
error 133 is another thing.

Error 133: Cannot evaluate this expression.
(Cursor stays on identifier 'a'.)
ie. it cannot evaluate 'a' at compile time.

3)

Quote
>> const a: longint = 100000;

When you declare a typed constant in this way (see Bob's first explanation),
Turbo pascal actually declares a _Variable_ (not a constant) and add the
following line to the very beginning to your program (invisible unless under
a de{*word*81})

begin
  a:=100000; {compiler adds this}
 {your code goes here}
end.

You can even try this to prove it is a variable:

const a:longint=0;
begin
  readln(a);
end;

Quote
>> var p: array[1..a] of longint;

However, the size of p has to be determinded at compile time. Since Turbo
Pascal does not treat [a] as constant, [a]'s value is only determinded at
run time. So it cannot determine the exact size of the structure, and emits
a compile error.

To solve these problems,
1) use Const A=10000;
  if you really want to use [A] as variable, use this
  Const
    ConA=10000;
    A:longint=ConA;
  var p:array [1..ConA] of longint;

2) try to use a smaller size. under turbo pascal, this means (64
kilobyte)/sizeof(longint)=16384. since other variables also need to use
memory, the number has to be further reduced. (usually 15000 is still ok for
a simple program)

Bob Schor ???g??31 <38F5BD9E.E37B...@pitt.edu>...

Quote
>You do not say what compiler you are using (I'm guessing Borland, whose
details
>I admit I do not know).  My answers will assume Standard Pascal.

>1)  In Standard Pascal, constants do not have types.  You would simply say

>  CONST
>    a = 100000;

>2)  You are trying to create an array of 100,000 long integers.  Some
compilers
>have restrictions on the amount of memory you can allocate all at once,
with
>that limit being "related to" 65535 (I'm unsure if the limit is number of
bytes,
>number of elements, etc).  In any case, 100000 may simply be too big for
your
>system.

>Are you really planning to generate 100,000 numbers?

>Bob Schor
>Pascal Enthusiast

>Hepworth wrote:

>> I would be grateful for some help; I'm a beginner having trouble working
out
>> how to do the following (crippled by the fact that my help file is in
>> German).

>> I am trying to declare an array of numbers whose index will be a longint,
>> but cannot fathom the usage.  I need a to be a longint constant, and p to
be
>> an array 1..a of longints.  As far as I see from examples the following
>> ought to work, but always returns error 133 at the end of the array here:

>> const a: longint = 100000;
>> var p: array[1..a] of longint;

>> Many thanks, Richard.

Re:Declaring constants


JRS:  In article <8d2ps0$s5...@news6.svr.pol.co.uk> of Wed, 12 Apr 2000
22:24:25 seen in news:comp.lang.pascal.misc, Hepworth <p...@hepworth77.fr

Quote
eeserve.co.uk> wrote:
>I would be grateful for some help; I'm a beginner having trouble working out
>how to do the following (crippled by the fact that my help file is in
>German).

>I am trying to declare an array of numbers whose index will be a longint,
>but cannot fathom the usage.  I need a to be a longint constant, and p to be
>an array 1..a of longints.  As far as I see from examples the following
>ought to work, but always returns error 133 at the end of the array here:

>const a: longint = 100000;
>var p: array[1..a] of longint;

Your question is unanswerable in this newsgroup, which is c.l.p.misc,
since this is for all Pascal not covered by other c.l.p.* newsgroups and
so we do not know what Pascal you are using.

It looks rather like Borland's Pascal, however (for which use newsgroup
c.l.p.borland); if your implementation closely resembles that, you have
two problems.

(1) Array bounds must be true constants, and "const a : ... = ... ;" is
in effect an initialised variable - use "const a - ... ;".  This is what
the Borland compiler would complain about first.

(2) You are trying to declare something, "p", of size 100000*4 bytes; the
largest that Borland Pascal compilers can handle is about 65520 bytes.
You will need to think about that, and my WWW pas-extn.htm#BigData should
help, as will TSFAQP #14 (see sig - you should get a copy of TSFAQP).

Your code looks equally like Borland Delphi, or TMP Pascal, in which case
only (1) applies (but they may have different error numbers).

If it is a Borland Pascal that you are using, be aware that you can
obtain TP5.5 free from the Borland WWW site, with help in American; and
probably TP7.01, with help in French - see my pas-bptp.htm#museum.
Fetching of the eatlier TP is described in the c.l.p.b mini-FAQ which you
should also obtain.

--
? John Stockton, Surrey, UK.  j...@merlyn.demon.co.uk   Turnpike v4.00   MIME. ?
 <URL: http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
 <URL: ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ;
 <URL: http://www.merlyn.demon.co.uk/clpb-faq.txt> Pedt Scragg: c.l.p.b. mFAQ.

Re:Declaring constants


JRS:  In article <8d4oq0$oi...@newsg2.svr.pol.co.uk> of Thu, 13 Apr 2000
14:39:06 seen in news:comp.lang.pascal.misc, Hepworth <p...@hepworth77.f

Quote
reeserve.co.uk> wrote:
>Thanks (I'm using TP7 for dos).  I have to admit that I'm not quite sure
>where I'm going with this.  My task is to identify all primes less than a
>certain (big) number, and for large enough numbers there will be loads of
>them, so yes I need a large (though not necessarily that large) array.  I
>was trying to see if I was making a glaring program mistake or whether I
>want to go back to the maths and start again.

>Bob Schor wrote in message <38F5BD9E.E37B...@pitt.edu>...
>>You do not say what compiler you are using (I'm guessing Borland, whose
>details
>>...

Responses should follow snipped quotes.

If you can give a sufficiently convincing explanation of what this is
needed for, and if it is not student course work, then prefabricated
solutions can be made known.

If the big number is indeed certain, what is it?  Some consider big to
start at, say, 1000; others at 2^63; and in choosing a solution the
difference is significant.

--
? John Stockton, Surrey, UK.  j...@merlyn.demon.co.uk   Turnpike v4.00   MIME. ?
 <URL: http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
 <URL: ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ;
 <URL: http://www.merlyn.demon.co.uk/clpb-faq.txt> Pedt Scragg: c.l.p.b. mFAQ.

Re:Declaring constants


Quote
Hepworth wrote:
> Thanks (I'm using TP7 for dos).  I have to admit that I'm not quite sure
> where I'm going with this.  My task is to identify all primes less than a
> certain (big) number, and for large enough numbers there will be loads of
> them, so yes I need a large (though not necessarily that large) array.  I
> was trying to see if I was making a glaring program mistake or whether I
> want to go back to the maths and start again.

     I can see why you might want a big array.  But what will you put in the
array?
Do you want to store all of the primes?  All of the "trial divisors" (the primes
up to
the square root of the largest number you will be testing)?  In which case, to
test primes from 1 to 1,000,000,000 (a pretty big number, certainly requiring
more than
a 16-bit integer) would only need (at most, assuming EVERY number is a prime)
30,000 "test" divisors.  You could probably get away with (I'm guessing) 3000,
assuming 1 out of 10 integers is a prime (there are about 9600 primes <
100,000).

Bob Schor
Pascal Enthusiast

Re:Declaring constants


Thanks,
I've got myself sorted now: the plan I had been using was to find and store
all primes up to the relevant number (more than just count them).  For
1000000 that's about 75000 of them, so would have been impossible (for me)
on my compiler as it stands.  As it turns out, I was barking up the wrong
tree entirely as regards my task, anyhow.

Quote
>assuming 1 out of 10 integers is a prime (there are about 9600 primes <
>100,000).

(There are approx n / log n primes less than n).

Many thanks to you and all other repliers for your help.
Richard.

Other Threads