I would really appreciate if someone could take a look and give some advice or h
ID: 664239 • Letter: I
Question
I would really appreciate if someone could take a look and give some advice or help.
I got a 2D array and based on it I can figure out a value based only on those coordinates. What I am trying to do though is to use a y=mx+b equation to find the values in between the points. I am having lots of issues writing a code that would say, figure out the value of the two closest points (that surround the value).
For example if I have-
a graph associated with the array that looks like this:
Then in code this is:
array[4][5] = { {1, 2, 3, 4, 5} }
{ {3, 5, 8, 10, 14} }
{ {5, 7, 12, 16, 19} }
{ {8, 9, 13, 18, 23} }
// I can plug in indirectly for points using-
if (x=1)
{a=0}
if (x=2)
{a=1}
if (x=4)
{a=2}
if (x =6)
{a=3}
b = y-3
cout << array[a][b];
But if I plug in, for example, x=4.7 and y = 6, how should I write a code that would figure out that 4.7 is between x=4 and x=6 or between a =2 and a=3 and then take the coordinates at x=4 or 6 or a=2 or 3 and plug it into an y=mx+b equation?
Explanation / Answer
As the values stored in aray are integers.... so rather selecting by single value...select by range
so change your if conditions set like this
if (x>=1.0 && x<1.5)
{a=0}
if (x>=1.5 && x<=2)
{a=1}
if (x>=2 && x<4.5)
{a=2}
if (x>=4.5 && x=6)
{a=3}
so that any numbers given we can get exact value between boundary given
Before this we have to cast int to float
x=(float)x;