1

Here is my code so far and I am stuck. The closest value to 7 in terms of absolute value is 5. How can I check each element of the array to see if it is the closest element and then return that value. I know this is probably easier than I think but I am a beginner at programming.

#include <iostream>
#include <cmath>
using namespace std;

const int MAX = 25;

int searchArray(int [], int); \\prototype

int main(){

    int numArray[MAX] ={1,4,5,10,11,12,13,14,15,16,17,18,19,20,35,26,43,15,48,69,32,45,57,98,100};

searchArray(numArray, 7);
system("pause");
return 0;
}


int searchArray(int a[], int b){

int distance = 0;

for(int i=0;i<MAX;i++){
    if(i==b)
        return i;
    else if(i<b || i > b)
        abs(distance) == i-b;
    return distance;


}

}
4
  • 1
    umm what is that abs(distance) == i-b supposed to do? Commented Jan 8, 2013 at 20:32
  • 1
    and why are you comparing i to be and not a[i] , looks like you need to read up on your homework. Commented Jan 8, 2013 at 20:33
  • Ah you are right all of them should be a[i]. Like i said I am a beginner. I used abs(distance) == a[i] - b to try and find the absolute value of how far away an element is from the key 7. Commented Jan 8, 2013 at 20:35
  • Nick, please refrain from "thank you" comments. That's what voting is for. Only comment if you actually have something constructive to say. Commented Jan 8, 2013 at 20:44

4 Answers 4

6

You can use std::min_element as:

#include <iostream>
#include <algorithm>
#include <cstdlib>

int main()
{
   int numArray[] ={1,4,5,10,11,12,13,14,15,16,17,18,19,20,35,26,43,15,48,69,32,45,57,98,100};

   //find nearest element to key            
   int key = 7;
   auto cmp = [&](int a, int b) { return std::abs(key-a) < std::abs(key-b); };
   int nearest_value = *std::min_element(std::begin(numArray),std::end(numArray),cmp);

   std::cout << nearest_value << std::endl;
}

Output (demo):

5
Sign up to request clarification or add additional context in comments.

Comments

3

Write your search function to the following:

int searchArray(int a[], int b){

    int min_dist = 0; // keep track of mininum distance seen so far
    int min_index = 0; // keep track of the index of the element 
                       // that has the min index

    for( int i = 0; i < a.Length; i++ ){ // a.Length is the size of the array
        if( a[i] == b ) { // if we find the exact one, stop
            return i;
        } else { // otherwise, keep looking for the closest
            if ( abs(a[i] - b) < min_dist ) { // if this one is closer, update
                min_dist = abs(a[i] - b);
                min_index = i;
            }
        }
    }
    return min_index; // when we finish return the index of the closest
}

1 Comment

@Nick - see the edited version =) fixing a few other minor mistakes
3

You can use standard algorithms to do that for you:

struct closer_to
{
    int target_;

    explicit closer_to( int target ) : target_( target ){}

    bool operator ()( int left, int right ) const
    {
        return std::abs( target_ - left ) < std::abs( target_ - right );
    }
};

int* iter =
    std::min_element(
        numArray + 0, numArray + MAX
      , closer_to( 7 )
    );

min_element will return an iterator (pointer in this case) to the first element for which there is no other element in the Container that is closer_to( 7 ).

With C++11 and lambdas, it would look like this:

int* iter = 
    std::min_element(
        numArray + 0, numArray + MAX
      , []( int left, int right ) { return std::abs( target_ - left ) < std::abs( target_ - right ); }
    );

Comments

1

You have to compare your number to every number on your array and keep track of the smallest distance so far.

int searchArray(int a[], int b){
    int minDistance = -1;

    for(int i=0;i<MAX;i++){
        if(minDistance == -1 || abs(minDistance - b) > abs(a[i] - b))
            minDistance = a[i];
    }
    return minDistance;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.