# Table of X & Y Values.

• 10-14-2008
danlee58
Table of X & Y Values.
I have several tables of X and Y values to store, and lookup based on the indices x and y.

I need to store the tables in such a way thet I can retrieve the x, y intersection and interpolate between values of x and y.

Code:

``` table1 [8] [10] = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},                                                                            {0.92, 1.14, 0.80, 0.46, 0.34, 0.80, 0.80, 0.23, 0.46, 0.92},                                                                     {1.14, 1.37, 1.03, 0.92, 0.92, 1.14, 1.14, 0.80, 0.92, 1.14},                                                                     {1.49, 1.49, 1.37, 1.26, 1.14, 1.26, 1.26, 1.03, 1.26, 1.49},                                                                     {2.06, 1.94, 1.94, 1.72, 1.72, 1.72, 1.72, 1.14, 1.72, 2.06},                                                                     {2.52, 2.52, 2.17, 1.72, 1.72, 1.72, 1.72, 1.37, 1.72, 2.52},                                                                     {2.63, 3.32, 2.40, 1.72, 1.72, 1.72, 1.72, 1.49, 1.72, 2.63},                                                                     {3.09, 3.77, 2.75, 1.72, 1.72, 1.72, 1.72, 1.26, 1.72, 3.09}};```
x and y are floating point values and are not linear.

I want to make the lookup algorithm a fuction, so I can use it on multiple tables.
• 10-14-2008
ninboy
You need a function that look for an specific number (floating point) and returns the x and y values where it is found?
• 10-14-2008
danlee58
Quote:

Originally Posted by ninboy
You need a function that look for an specific number (floating point) and returns the x and y values where it is found?

No, I need a function that will take two floating point numbers (x, and y) and return the closest approximation from an 8 x 10 array.

I could do it with a separate function for x, and another for y, and a third for the x. y intersection.

Code:

``` int xindex [8] = {-55.5, -30, -20, 0, 10, 22.5, 41.0, 52]; int yindex [10] =[ 3000, 2100, 1500, 900, 600, 400, 300, 250, 220, 210}; if (x <= -55.5) xindex = 1; else if ((x >-55.5) && (x<=  -30)) xindex =2;     else.............); if (y >=3000) yindex = 1; else.........; functxy = table1 [xindex] [yindex]; return functxy;```

I also have to interpolate between the values of x, and y in functxy, but didn't try to include it here.
• 10-14-2008
danlee58
There's a couple of errors in the above code, but it's only included to demonstrate my problem.
• 10-14-2008
tabstop
So the if-elseif-elseif-etc is a perfectly good way of figuring out where in the table you need to be. But I would probably use auxiliary tables, a la
Code:

```float xindex[] = {-55.5, -30, -20, 0, 10, 22.5, 41.0, 52 }; . . . int i = 0; while ((i < 8) && (xindex[i] < target_x)) i++;```
which would find the table value one too high (and of course subtracting one for the one too low). You can do something similar for y.

After that, you'll have to decide which of the 16345 ways to interpolate you intend to do.
• 10-15-2008
danlee58
Quote:

Originally Posted by tabstop
So the if-elseif-elseif-etc is a perfectly good way of figuring out where in the table you need to be. But I would probably use auxiliary tables, a la
Code:

```float xindex[] = {-55.5, -30, -20, 0, 10, 22.5, 41.0, 52 }; . . . int i = 0; while ((i < 8) && (xindex[i] < target_x)) i++;```
which would find the table value one too high (and of course subtracting one for the one too low). You can do something similar for y.

After that, you'll have to decide which of the 16345 ways to interpolate you intend to do.

That looks like a good way to do what I want. I will also use an integer variable 'sizex' that can be passed in a function call for different size arrays. The same function can also be used to get the y index.

Code:

```xindex[] = {-55.5, -30, -20, 0, 10, 22.5, 41.0, 52 }; . . . int sizex = 8; int i = 0; while ((i < sizex) && (xindex[i] < target_x)) i++; return i; x = i; ' ' ' y = i; functxy = table1 [x] [y]; (interpolate here); return functxy;```