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[0][0] - y[0] );
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.