Pascal

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

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
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)???
Report
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
    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.

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
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.