Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

help

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

Comments

  • zibadianzibadian 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
    write('Give X size: '); readln(UserArraySize[1]);
    until UserArraySize[1] in [1..100]; { Check the size. }
    repeat
    write('Give Y size: '); readln(UserArraySize[2]);
    until UserArraySize[2] in [1..100]; { Check the size. }
    repeat
    write('Give Z size: '); readln(UserArraySize[3]);
    until UserArraySize[3] in [1..100]; { Check the size. }
    writeln('Select an element to Enter:');
    repeat
    write('X: '); read(X);
    until X in [1..UserArraySize[1]];
    repeat
    write('Y: '); read(Y);
    until Y in [1..UserArraySize[2]];
    repeat
    write('Z: '); read(Z);
    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.
Sign In or Register to comment.