[comp.lang.pascal] Can I declare an array using a var in the index?

limoges@ac.dal.ca (09/22/90)

I want to write a program which generates 2D arrays of size n x n after n has
been input from the keyboard by the user running the program. My debugger
tells me I must use a constant in the range used for the index type. Is there
a way to declare an array using the n variable? Or do I have to declare a huge
array (what a waste of memory) and then do sub-range processing based on user
input? BTW I'm using Sun Pascal.

Thanks for any help, Bertrand Limoges

nkraft@crash.cts.com (Norman Kraft) (09/23/90)

In article <1994@ac.dal.ca> limoges@ac.dal.ca writes:
>I want to write a program which generates 2D arrays of size n x n after n has
>been input from the keyboard by the user running the program. 

I have no experience with the Sun Pascal compiler specifically, but the way 
Pascal compilers are generally built precludes arrays which are of 
indeterminate length at compile-time. The usual approach to this problem is
to declare pointers to the array element type and allocate memory from the
heap at run-time. 

Have you tried this approach? Several pieces of code have been posted to
alt.sources which support this concept, though those are specific to 
Turbo Pascal. You might even be able to implement a variant of the singly
linked list to accomplish the same function. Of course, this means that
you must also write all your own routines to get/place elements from/to
the dynamically allocated "array". Sort of takes the fun out of it, 
doesn't it? :)

Norm.

--------------------------------------------------------------------------
Norman R. Kraft	                                "Things should be as
Director, Software Development                   simple as possible,
Postal Buddy Corporation, San Diego, CA          but not simpler."
INET nkraft@crash.cts.com                          - Albert Einstein
UUCP {hplabs!hp-sdd ucsd nosc}!crash!nkraft
--------------------------------------------------------------------------
                                     

elmo@uhura.cc.rochester.edu (Eric Cabot) (09/23/90)

In article <1994@ac.dal.ca> limoges@ac.dal.ca writes:
>I want to write a program which generates 2D arrays of size n x n after n has
>been input from the keyboard by the user running the program. My debugger

If you can stand to have one of the dimensions (either row or col) pre-
defined then why not declare a type of that array size and an 
array of pointers to that type. There isn't really very much overhead
in having that array of pointers.      Addressing the elements
in your matrix isn't particularly messy or difficult. Here are some
program fragments to show you what I mean:

TYPE
    col_arr:record of col: array[1..200] of integer; end;
VAR
    row:array[1..200] of ^col_arr;
    
...

 readln(nrows);
 for i:=1 to nrows do new(row[i]^);
...
  row[5]^.col[5]:=25;  (*sets item at row 5, col 5 to the value 25*)
...

I've used this basic strategy quite a bit for programs that don't
know ahead of time how much memory a given user has on his computer.

You can also further complicate matters by making the col a linked
list, or a linked list of small arrays.

I hope this helps out.


-- 
=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
Eric Cabot                             |  elmo@{uhura | db1}.cc.rochester.edu
      "insert your face here"          |  elmo@uordbv.bitnet
=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=

mailman@telfon.enet.dec.com (Steven M. Mailman) (09/24/90)

>>I want to write a program which generates 2D arrays of size n x n after n has
>>been input from the keyboard by the user running the program. My debugger
>>

In general, Classic Pascal has required that all bounds be specified 
at compile-time.

The new Extended Pascal Standard does allow bounds to be specified
at run-time.  You can say

    TYPE
	matrix( size : Integer ) = ARRAY [1..size,1..size] OF REAL;

    VAR
	x : maxtrix( run-time-expression );


The only compiler that I know of that has implemented this feature
from Extended Pascal is VAX Pascal version 4.0.