Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Allocating items to a table

Posted on 2011-04-22
Medium Priority
Last Modified: 2012-05-11
I need to allocate a fixed number of items to be placed on a variable number of tables. The number of tables is to be determined by the number of items, with these caveats:

1) There will always be at least 3 items. If there are fewer than 3 items, we cannot proceed

2) There can be no more than 6 items on any one table.

3) We wish to balance the number of items on a table. For example:

7 Items = 4 on one table, 3 on another
8 items = 4 on each table
9 items = 5 on one table, 4 on another
10 items = 5 on each
11 items = 6 on one table, 5 on another
12 items 6 on each table

And the issue would start back over at 13 only this time with 3 tables. We'd want something like 4 + 4 + 5.

Any suggestions on an algorithm, or coding for this? I can hard code it, and just build a matrix that supplies those values, but wondered if anyone had ideas on doing this a little differently.


Expert Comment

ID: 35450972
# of items = item1

item1 mod 12 should give you the remainder

int (item1 / 12) should give you the quotient

whatever is the quotient, distribute it equally in tables of 6

for the remainder, follow your logic with if/else or case statement.

LVL 16

Expert Comment

by:Imran Javed Zia
ID: 35451069

Hope it will work for you

If items < 3
else If items < 13 Then
      Tables = 2
Else If Items/2=0 Then
      Tables = Items/6       // Just quotient
      Tables = (Items/6) + 1
End If


Initialize ArrayOfTable(Tables)

For i = 1 to items
      index = i Mod Tables
      Add Item to ArrayOfTable(index)      

Print ArrayOfTable

LVL 37

Accepted Solution

TommySzalapski earned 2000 total points
ID: 35451410
All those solutions are still more complicated than you need. You don't need to hard code anything (except maybe the three).

const MAX_ITEMS As Integer = 6 'Or however you do this
const MIN_ITEMS As Integer = 3

Dim NumTables As Integer

if items < MIN_ITEMS, you give up and quit. Otherwise,

if items mod MAX_ITEMS = 0 then
  NumTables = items/MAX_ITEMS
  NumTables = items/6 + 1

Then put int(items/NumTables) on each table and add one item to (items mod NumTables) of them.
LVL 85
ID: 35463850
Thanks for your responses, and sorry for the delay in responding - Easter weekend took more time than anticipated, so I'm a bit behind schedule.

I'll review, and post back here with my findings.
LVL 85
ID: 35501377
Thanks. This provided me with a very good basis for my algorithm.

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Simple Linear Regression
Screencast - Getting to Know the Pipeline

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question