help - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

help

Posts: 1Member
can somebody help me in writing a 3d array in which the user defines the size and inputs the data(numbers)???

• Posts: 6,349Member
: 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:
[code]
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.
[/code]
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.