# Thread: 1D array to "most square" 2D array...

1. ## 1D array to "most square" 2D array...

Hello,
Ive been wondering about the following problem...

Given a 1D array of n values,

I want to find a 2D array to store it.. But the array has to be as "square as possible",

ie:
x_dimension - y_dimension = m
|m| has to be as small as possible.

and without any padding, ie: dummy values filling the array..

Any help?
ut.

2. ## Re: 1D array to "most square" 2D array...

does no seem so hard :

x = ceil(sqrt(n));
y = ceil(n*1.0/x);

3. ## Re: 1D array to "most square" 2D array...

Here is what i ended up using, if anyone has anything bit better, please let me know, as i find this solution a bit nasty...

Code :
```//given array_size (size of the 1D array)
//work out 2D array dimensions.

float dimX = sqrt(array_size);

while(helper)
{
if(array_size%dimX==0) helper=0;
dimX++;
}

dimY = array_size/dimX;```

4. ## Re: 1D array to "most square" 2D array...

However with your approach, the 2D array will mostimes be bigger then N. I dont want this..

ut.

5. ## Re: 1D array to "most square" 2D array...

Ah ok I reversed your priorities. Your strongest need is that there is "not padding at all", I first though is was "as square as possible".

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•