Is There a Formula to Populate a Table?

1. May 29, 2012

ckirmser

I couldn't think of a better subject line without, basically, asking my question there.

What I have is a 9x7 table and I need to know if there is an easy way to drop a number into any of the squares and then populate the remainder of the table by subtracting 1 from each square as you move away from the original orthagonally, repeating the procedure for each newly populated square to populate empty squares.

I don't really know if this should be a geometry or linear question, so I dropped it in here.

My end goal is to take the formula, code it into Java or VBasic and use it in a program I'm working on. I could simply create a case statement that populates the table manually for each possible starting point, but that is gruesome to think of and I figure there must be an easier way...

Thanx for any help!

2. May 29, 2012

Diffy

Can you show an example of a table and what you want accomplished?

3. May 29, 2012

ckirmser

Sure.

The table starts as;
+0 +0 +0 +0 +0 +0 +0 +0 +0
+0 +0 +0 +0 +0 +0 +0 +0 +0
+0 +0 +0 +0 +0 +0 +0 +0 +0
+0 +0 +0 +0 +0 +0 +0 +0 +0
+0 +0 +0 +0 +0 +0 +0 +0 +0
+0 +0 +0 +0 +0 +0 +0 +0 +0
+0 +0 +0 +0 +0 +0 +0 +0 +0

A number - say, 4 - is dropped in at 5,3;
+0 +0 +0 +0 +0 +0 +0 +0 +0
+0 +0 +0 +0 +0 +0 +0 +0 +0
+0 +0 +0 +0 +0 +0 +0 +0 +0
+0 +0 +0 +0 +0 +0 +0 +0 +0
+0 +0 +0 +0 +4 +0 +0 +0 +0
+0 +0 +0 +0 +0 +0 +0 +0 +0
+0 +0 +0 +0 +0 +0 +0 +0 +0

The end result would look like;
-4 -3 -2 -1 +0 -1 -2 -3 -4
-3 -2 -1 +0 +1 +0 -1 -2 -3
-2 -1 +0 +1 +2 +1 +0 -1 -2
-1 +0 +1 +2 +3 +2 +1 +0 -1
+0 +1 +2 +3 +4 +3 +2 +1 +0
-1 +0 +1 +2 +3 +2 +1 +0 -1
-2 -1 +0 +1 +2 +1 +0 -1 -2

I seem to - vaguely - recall something like this from back in linear algebra or maybe discrete math, but it's been over a decade and I'm not sure.

4. May 29, 2012

Diffy

Well now seeing the example I would guess it would be pretty simple depending on what language you use. However this is a programming question more so than a mathematical one. So I am not sure how much help we can be.

My programming is a little fuzzy. But I do recall from C++ that you could work with matrices and you could work with the individual elements of the matrix using notation like $a_{i,j}$ so that you could index where the element was.

Then I would suppose you would just have to develop a few loops to get through the entire table.

Edit
Stupid me.

There is a much easier way than looping. Say you drop a 4 into (5,3) as you say. Then given any point in your table (a, b), the value will be 4 - (|5-a| + |3 - b|)

5. May 29, 2012

Staff: Mentor

This would be a 7 x 9 matrix, not 9 x 7. The first number is the number of rows, and the second number is the number of columns.
The 4 is at location [3, 4], using 0-based indexes for the rows and columns. Many programming languages, especially C-based ones, use zero-based indexing.

The following C code declares an array variable, and sets the element row 3, column 4 to 4.

Code (Text):

int table[7][9];
table[3][4] = 4;

6. May 29, 2012

ckirmser

This might do it - all I'll have to do is figure how to code it to be dynamic, like making a function with the coords of the original target square and then figure the rest from there.

Probably won't get to test this out until the weekend, though, but thanx for the formula.

7. May 30, 2012

ckirmser

Well, got to it earlier - slow day at work - and this'll work perfectly!

Danke, Diffy!

Question, though - why did you choose 5 and 3? is it because they are on either side of 4 or are they derived by some function of the dimensions of the table?

8. May 30, 2012

ckirmser

@Mark44 -

I understand the indexing of the tables, but since I'll be coding the function using an array that I set up, I'll simply create the array as X(9,7) and control which cell is being addressed via code.

9. May 30, 2012

Staff: Mentor

i would do this recursively as follows (trying not to hand you the complete solution)

setvalue(cell,i,j,value) {

// check limits of i,j so as not to exceed the cell matrix size

if(cell(i,j)==0) cell(i,j)=value

// recurse thru neighboring cells
setvalue(i-1,j,value-1) // look to the left
// apply similarly for other directions

return
}

10. May 30, 2012

Diffy

To answer your question the 5 and the 3 come from the initial spot in the table where the 4 is dropped.

So in general if the starting value you put into the table is k.

and the position you want to calculate is (a, b)

and the position you placed the initial k value is (x,y)

then the general formula becomes

k - (|x-a| + |y - b|)

So step one would be to set the value k in position x,y

Then set each element of the table equal to k - (|x-a| + |y - b|) where the element is not in position (x,y) .. (As to avoid overwriting the initial position of k.)

Hope this helps. Let me know what language you end up using to code this then I might be able to help some more.

11. May 30, 2012

ckirmser

Uh, well, yeah - I mean, well of course it is.

Duh.

Oh, what, this cone-shaped hat? Bought it for myself.

Oh, it helps tremendously. I knew it would be something simple and that I just had a mental block.

The end result will be JavaScript, so the next stumbling block is how to dynamically reference an object so that I can transfer the contents of the final table to the cells I've placed on the form. I wish I could be doing it in VB6, since that has grouped controls with an index property, but c'est la vie, it ain't.

However, I figure that is a bit outside the scope of this forum...

12. Jun 1, 2012

Staff: Mentor

you should look at the recursive solution as well its how most senior programmers would do it.

13. Jun 1, 2012

ckirmser

That solution seemed - to me, anyway - overly complex.

I've determined that I don't really need a procedure to cycle through the table.

Basically, as I create each cell on the form, I have its source set to a function that determines the current value based upon the X,Y coords passed to it of the relevant cell. Since I have the initial coordinates selected in two other fields and the value to place there in another, I can have the function assign values to the a, b, k, x and y variable using the formula from Diffy and then just return the result.

No looping required and every cell updates as it should.

I'll have to examine the recursive solution, too, to see if that's cleaner in the end.