# Thread: Finding the closest number to another number

1. ## Finding the closest number to another number

Try not to make too much fun of me for my logic, but I'm having trouble with this. I am trying to make it so the program takes a 1 dimensional array and a 2 dimensional array, and checks to see what row in the 2 dimensional array is the closest to the 1D array.

To compute the value of the 1D array you take the first row first element in the 2D array, and the first element in the 1D array, subtract and the absolute value.

Example: | 4 - 3 | = 1;
1Darray = 1.

Full 1 Dimensional Array:
3 1 6 9

Full 2 Dimensional Array:
4 9 1 5
6 1 7 3
0 8 2 6

To compute the value of the first row in the 2D array,
| 4 - 3 | + | 9 - 1 | + | 1 - 6 | + | 5 - 9 | = 18.
| 6 - 3 | + | 1 - 1 | + | 7 - 6 | + | 3 - 9 | = 10.
| 0 - 3 | + | 8 - 1 | + | 2 - 6 | + | 6 - 9 | = 17.

Thus row 1 being the closest row to the 1 dimensional array.

The code I've written so far seems to be going down the right path. Have a look below...
Code:
```     int i,j,tempRow,tempTotal = 0,firEle,cloRow;

firEle = abs( x - y );

for( i = 0; i < size; i++ ) {
tempRow = 0;
for( j = 0; j < size; j++ ) {
tempRow += abs( x[i][j] - y[j] );
}
tempTotal = tempRow;
if( tempRow == firEle ) {
cloRow = i;
return cloRow;
}
else if( tempTotal < tempRow ) {

}
}```
The whole temp part is kind of confusing myself. What I'm thinking is that I can add all row values up using
Code:
`tempRow += abs( x[i][j] - y[j] );`
, then I need to compare that value to see if it is close to the "firEle" which is value I need to get closest to.

Hopefully someone understands where I'm going here. I thank everyone for their help. 2. Try abs(tempRow-firEle), and keep track of how near you're getting to the answer. 3. The explanation of your logic is not quite clear. How is row #1 in the 2D array the closest to 1D array when it's difference of 18 is the highest? Shouldn't be row #2 the closest with the lowest difference of 10? 4. Originally Posted by DRK The explanation of your logic is not quite clear. How is row #1 in the 2D array the closest to 1D array when it's difference of 18 is the highest? Shouldn't be row #2 the closest with the lowest difference of 10?
Sorry DRK for not being more specific. When I'm talking about row 1 or 2, I am referring to it as it's index. So it would start at 0.

The logic I am having trouble figuring out, is checking which row value is closest to the value of the 1D array. And since the 1D array is "1", that would make index 1 in the 2D array the closest. I am having trouble thinking of a way to compare all of the rows in the 2D array to the value of the 1D array (1). 5. Originally Posted by Salem Try abs(tempRow-firEle), and keep track of how near you're getting to the answer.
Your logic just popped into my head. I think using your code "abs( tempRow - firEle )" should work. The lowest value is the winner right? 6. The lowest value is the winner right?
What would that [lowest] value indicate? Popular pages Recent additions 