Thread: Finding the closest number to another number

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    12

    Post 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[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.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,656
    Try abs(tempRow-firEle), and keep track of how near you're getting to the answer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    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. #4
    Registered User
    Join Date
    Mar 2014
    Posts
    12
    Quote Originally Posted by DRK View Post
    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. #5
    Registered User
    Join Date
    Mar 2014
    Posts
    12
    Quote Originally Posted by Salem View Post
    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. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,508
    The lowest value is the winner right?
    What would that [lowest] value indicate?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Closest prime number in an array?
    By turke92 in forum C Programming
    Replies: 5
    Last Post: 11-18-2011, 11:59 AM
  2. Replies: 2
    Last Post: 10-31-2009, 06:49 PM
  3. Finding The Hcf Of A Number
    By Saimadhav in forum C++ Programming
    Replies: 5
    Last Post: 10-31-2008, 06:50 AM
  4. Finding a number within a number
    By jeev2005 in forum C Programming
    Replies: 2
    Last Post: 01-10-2006, 08:57 PM
  5. Replies: 9
    Last Post: 11-20-2003, 08:55 PM