# Array creation and query - Searching for the most approximate value inside array

I'm running into trouble trying to create an array and find the most approximate value, when compared to a given number.

The array must be composed by the result of the division of the number 50, with the divisor being an integer ranging from 1 till 50.

The result will be a floating number, like 50 (for divisor 1), 25 (for divisor 2) , 16.666 (for divisor 3) , 12.5 (for divisor 4), 10 (for divisor 5) , 8.333 (for divisor 6) , 7.142 (for divisor 7) ... and so on, till 1 (for divisor 50).

Then, the idea is to give any integer or fractionary number between 1 and 50, like 7.5, for example, and look for the most approximate value on the array, printing it on screen, together with the respective divisor. In this case, it would be 7.142 (floating numbers must be preserved) and 7.

I imagine this must be a simple operation, but I'm a total beginner on the array subject.

Can anyone help my finishing this project?

regards and have a great weekend guys

I'd advise using the divisor as the index into the array. This means that we don't use element 0. So you need an array of 51 floats:

``````float divided[51];
``````

To initialise the array - easy. Just remember to use floating point numbers:

``````for(int i = 1; i<=50; i++) divided[i] = 50.0 / i;
``````

Actually: no You don't want the divisors, you want to chop up the line into those numbers where that index is the closest approximation.

So we will use element zero after all.

``````for(int i = 0; i<=50; i++) divided[i] = 50.0 / (i+.5);
``````

If a division is somewhere between

`[nobbc]divided[0][/nobbc]` and `[nobbc]divided[1][/nobbc]`, then that means that 1 is the closest approximation. If it's between `[nobbc]divided[34][/nobbc]` and `[nobbc]divided[35][/nobbc]`, then 35 is the closest approximation. If it's right on the edge, well, either will do.

To find a divisor, use a binary chop. Set an index equal to 0 (`[nobbc]divided[0][/nobbc]` should always be greater than any number you use) and 50 (`[nobbc]divided[50][/nobbc]` should always be less than any number you choose).

Then pick a point halfway between (round down or up). if that number is greater than your pick, move the lower bound up to there. If it's less, move the upper bound. Do this untill your bounds are next to each other. The higher bound is the one you want.

Can't you just divide 50 by your number and round off to the nearest integer?

``````int divisor = (50.0/value)+0.5;
``````