Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine rows/columns needed given a number

I've got a number of controls (charts in this case) that's determined at runtime. I'd like to put these in a grid with the proper number of rows and columns. For example,

  • 4 items = 2 x 2
  • 8 items = 4 x 2
  • 9 items = 3 x 3
  • 20 items = 5 x 4
  • 11 items = 4 x 3 (I don't care about an empty cell)

Sorry, I don't really have any code to show my attempts. I started playing with determining if the square root is an integer, if the number is evenly divisible by 2 etc. and realized I'm not sure how to attack this problem. But this is what I'm thinking:

  • If the square root is an integer, use the square root for the number of rows and columns (no problems there)
  • If not, make sure the number is even (add one if you have to - no problems there)
  • Find the highest two integers that produce the number. e.g. If I have 20 controls, the grid should be 5 x 4 and not 10 x 2 (not really sure the best way for this)

I'd appreciate it if someone could point me in the right direction - or suggest a different algorithm if I'm way off base.

like image 459
Mike Hildner Avatar asked Nov 05 '10 14:11

Mike Hildner


People also ask

How do you calculate rows and columns?

Just click the column header. The status bar, in the lower-right corner of your Excel window, will tell you the row count. Do the same thing to count columns, but this time click the row selector at the left end of the row. If you select an entire row or column, Excel counts just the cells that contain data.

How do you find the number of rows and columns in a matrix?

If for example your matrix is A, you can use : size(A,1) for number of rows. size(A,2) for number of columns. Also there are some other ways like : length ( A(:,1) ) for number of rows. Sign in to answer this question.

How do I find the number of rows and columns in a python list?

Count the number of rows and columns of Dataframe using len() function. The len() function returns the length rows of the Dataframe, we can filter a number of columns using the df. columns to get the count of columns.


2 Answers

Idea: If square root is not integer, floor it, then divide whole number by this, ceil it.

int columns = (int)sqrt(number);
int lines = (int)ceil(number / (float)columns);

Example: 21 => columns = 4, lines = 6.

UPDATE: bonus, it also works when sqrt(number) is integer. No rounding occurs anywhere, and values are correct.

like image 103
jv42 Avatar answered Sep 30 '22 19:09

jv42


The "usual" way of handling this problem is by saying that there will always be N columns (less often, always N rows). The problem then becomes a matter of taking the number of items, dividing by N, and that's the number of rows you have (plus one if there's a remainder).

Changing the size of the grid makes for a confusing user interface. Users won't understand why the size of the grid keeps changing. They won't really wonder about it, but they'll be confused by the seemingly random changes.

If you still want to do what you're saying, I think you'll need to define your problem a little better. Is there a maximum number of items that can fit on the grid? Is there a maximum number of columns that you'll allow? For example, if you allow 50 items, should they be in 25 rows of 2 items? 5 rows of 10 items? 10 rows of 5 items?

At some point you'll have to either scroll horizontally or say, "maximum number of columns is X". And if you're going to impose that maximum number of columns, then you're better off just saying "There will always be X columns."

Unless there's a compelling reason to do the variable-dimension grid that you ask for, you're way better off just fixing the number of columns. It makes for trivially simple code rather than some complicated hack, and it presents a much more consistent interface to your users.

like image 31
Jim Mischel Avatar answered Sep 30 '22 17:09

Jim Mischel