## Pascal

Moderators: None (Apply to moderate this forum)
Number of posts: 14016

This Forum Only

help Posted by slowdeath on 14 Mar 2005 at 7:58 AM
can somebody help me in writing a 3d array in which the user defines the size and inputs the data(numbers)???
Re: help Posted by zibadian on 14 Mar 2005 at 8:58 AM
: can somebody help me in writing a 3d array in which the user defines the size and inputs the data(numbers)???
:
Pascal doesn't allow dynamic arrays, so there will always be a maximum number of elements. Here is an example, which allows the user to specify up to 100x100x100 integers:
```var
UserArray: array[1..100, 1..100, 1..100] of integer;
UserArraySize: array[1..3];
X, Y, Z, i, j: integer;
begin
{ Set Dimensions }
repeat
until UserArraySize[1] in [1..100]; { Check the size. }
repeat
until UserArraySize[2] in [1..100]; { Check the size. }
repeat
until UserArraySize[3] in [1..100]; { Check the size. }
writeln('Select an element to Enter:');
repeat
until X in [1..UserArraySize[1]];
repeat
until Y in [1..UserArraySize[2]];
repeat
until Z in [1..UserArraySize[3]];
write('Value for ',X,',',Y,',',Z,':'); readln(UserArray[X, Y, Z]);

{ Code to show 2D-slice of the array}
for i := 1 to UserArraySize[2] do
begin
for j := 1 to UserArraySize[1] do
write(UserArray[j, i, z]), ' ');
writeln;
end;
end.
```

There are various other ways to structure such an array. One commonly used method utilizes a 1D-array and calculates the 3D-coordinates to a 1D-coordinate. The main advantage is that the dimension-maximums aren't set, only the total number of elements.
Other methods use linked-lists to build the array. The advantage of this is that the array becomes dynamic.
These methods are increasingly harder to code and debug.

## Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic