Allocating items to a table

Posted on 2011-04-22
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.

    LVL 9

    Expert Comment

    # 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

    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

    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 84
    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 84

    Author Closing Comment

    by:Scott McDaniel (Microsoft Access MVP - EE MVE )
    Thanks. This provided me with a very good basis for my algorithm.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Iteration: Iteration is repetition of a process. A student who goes to school repeats the process of going to school everyday until graduation. We go to grocery store at least once or twice a month to buy products. We repeat this process every mont…
    A short article about a problem I had getting the GPS LocationListener working.
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

    754 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now