Is There a Formula to Populate a Table?

In summary, the conversation discusses a method for populating a 7x9 table by dropping a number into any square and then subtracting 1 from each square as you move away from the original orthagonally, repeating the procedure for each newly populated square. The end goal is to code this formula into a programming language and use it in a program. An example table and code snippet are provided.
  • #1
ckirmser
105
3
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!
 
Mathematics news on Phys.org
  • #2
ckirmser said:
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!


Can you show an example of a table and what you want accomplished?
 
  • #3
Diffy said:
Can you show an example of a table and what you want accomplished?

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
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 [itex]a_{i,j}[/itex] 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
ckirmser said:
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
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.
ckirmser said:
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 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:
int table[7][9];
table[3][4] = 4;
ckirmser said:
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.
 
  • #6
Diffy said:
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|)

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 thanks for the formula.
 
  • #7
Diffy said:
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|)

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
@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
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
ckirmser said:
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?

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
Diffy said:
To answer your question the 5 and the 3 come from the initial spot in the table where the 4 is dropped.

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

Duh.

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

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

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
you should look at the recursive solution as well its how most senior programmers would do it.
 
  • #13
jedishrfu said:
you should look at the recursive solution as well its how most senior programmers would do it.

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.
 

1. What is the purpose of populating a table?

The purpose of populating a table is to store, organize, and present data in a structured and easily accessible manner. This allows for efficient data retrieval and analysis.

2. Is there a standard formula for populating a table?

No, there is no standard formula for populating a table. The structure and content of a table will vary depending on the nature of the data being stored and the specific needs of the user.

3. What are the key components of populating a table?

The key components of populating a table include identifying the data to be stored, determining the appropriate data types and formats, creating column headings and labels, and inputting the data into the table.

4. How do you ensure data integrity when populating a table?

Data integrity can be ensured by setting constraints and validation rules on the table, such as data type restrictions and required fields. Regular data backups and periodic data audits can also help maintain data integrity.

5. What are some common mistakes to avoid when populating a table?

Common mistakes to avoid include entering incorrect data, using inconsistent formatting, and not properly defining data types. It is also important to regularly review and update the table to ensure accuracy and relevance of the data.

Similar threads

Replies
7
Views
2K
Replies
13
Views
7K
  • General Math
Replies
22
Views
2K
  • General Math
Replies
2
Views
1K
Replies
2
Views
1K
Replies
6
Views
6K
  • Calculus and Beyond Homework Help
Replies
3
Views
994
  • Set Theory, Logic, Probability, Statistics
Replies
7
Views
2K
  • Calculus and Beyond Homework Help
Replies
3
Views
523
  • Set Theory, Logic, Probability, Statistics
Replies
17
Views
1K
Back
Top