# Initializing dynamic arrays

4 messages
Open this post in threaded view
|

## Initializing dynamic arrays

 I am writing a unit for dealing with matrix algebra with arbitrary sized matrices (as opposed to the FPC unit that deals with a few fixed size ones). I declare the matrix type type         Matrix = array of array of Real; By using Length or High, I can loop through the array as I please, and I can make operator overloading and functions for most operations I need. There is just one thing that I can't do: I can't initialize a matrix in any elegant way! I would like to do like this:         m1: Matrix = ((1, 2, 3), (2, 3, 4)); and get a properly sized dynamic array matching the matrix given. This is obviously not supported, but what can I do? I can build an infinite number of functions, taking fixed-sized arrays as input and returning a dynamic array, but then I need one function for each combination of height and width! That is OK for sizes up to 3 or 4 but then it becomes ridiculous. Is there a better way to solve this problem? /Ingemar _______________________________________________ fpc-pascal maillist  -  [hidden email] http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Open this post in threaded view
|

## Re: Initializing dynamic arrays

 ingemar wrote on Mon, 04 Jun 2012: > There is just one thing that I can't do: I can't initialize a matrix in > any elegant way! I would like to do like this: > > m1: Matrix = ((1, 2, 3), (2, 3, 4)); > > and get a properly sized dynamic array matching the matrix given. > > This is obviously not supported, but what can I do? I can build an > infinite number of functions, taking fixed-sized arrays as input and > returning a dynamic array, but then I need one function for each > combination of height and width! That is OK for sizes up to 3 or 4 but > then it becomes ridiculous. > > Is there a better way to solve this problem? I think this is a case where a pointer combined with a width and   height parameter is probably the best solution. The more Pascal-like   and cleaner approach using open array parameters is only generic if   you limit yourself to 1-dimensional arrays. Jonas _______________________________________________ fpc-pascal maillist  -  [hidden email] http://lists.freepascal.org/mailman/listinfo/fpc-pascal