find closest num in array

My overall goal is trying to get the closest number in an array. But first trying to get the smallest number from an array, in which i would use later in accordance with abs().

Regarding finding the smallest:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

int min(int a[]){
    int smallest = a[0];
    for ( int i=1;  i < sizeof(a)/sizeof(a[0]);  ++i )
        if ( a[i] < smallest )
            smallest = a[i] ;
    return smallest;
}

int main ()
{
    int array []= {4,2,3,1,5};

    cout << min(array) << endl;
    /*
    int smallest = array[0];
    for ( int i=1;  i < sizeof(array)/sizeof(array[0]);  ++i )
        if ( array[i] < smallest )
             smallest = array[i] ;
    cout << smallest;
    */
}

I am not sure why when within main() it grabs 1, when its in its own func, it is returning 2?

And regarding to finding the closest number in an array: Is there a better way in c++? My idea was for something like:
min(array, key=lamda x: abs(x-num))
Last edited on
closed account (LN7oGNh0)
Sorry, im not sure what yo are actually asking here. This might be a possible error: for ( int i=1; i < sizeof(a)/sizeof(a[0]); ++i ) - change the int i = 1 to int i = 0. Sorry If Ive gotten this wrong, but I dont exactly know what yo are trying to do.
Last edited on
change the int i = 1 to int i = 0

it shouldnt be. As it is just skipping the first element check, as it was already assigned to smallest for intital check and thus checking against itself if it was 0.

I am not sure why, but you cannot get the size of the array in the function, so i instead just pasded the size to it. Although if there was a way to find the size in a function, i would prefer that method.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int min(int a[], int ind){
    int smallest = a[0];
    for ( int i=1;  i < ind;  ++i )
        //cout << smallest << endl;
        if ( a[i] < smallest )
            smallest = a[i] ;
    return smallest;
}

int main ()
{
    int array []= {44, 54, -3, 3};
    int ind = sizeof(array)/sizeof(array[0]);
    cout << min(array, ind) << " is the smallest" << endl;
}




Sorry If Ive gotten this wrong, but I dont exactly know what yo are trying to do.

I am trying to input a number that when checked against an array of random numbers, returns the number of the nearest to that number of the array. Example:
if an array had the elements 1 and 10, and i gave it the number 8, it would return 10, if i gave it the number 2, it would return the number 1



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <cmath>

using namespace std;

int min(int a[], int ind){
    int smallest = a[0];
    for ( int i=1;  i < ind;  ++i )
        //cout << smallest << endl;
        if ( a[i] < smallest )
            smallest = a[i] ;
    return smallest;
}


int main ()
{
    int num = 10;
    int array []= {44, 54, 3};
    int ind = sizeof(array)/sizeof(array[0]);
    int temp[ind];
    //cout << min(array, ind) << " is the smallest" << endl;
    
    for (int i=0; i<ind; i++){
        for (int j=0; j<ind; j++){
            cout << abs(array[i] - num) << endl;
            temp[j] = abs(array[i] - num);
        }
    }
    
    cout << min(temp, ind) << endl;
}

something like this, except it returns the lowest number 7, which is right, except i need the number associated with it, 3, as the number i am checking it with is 10. I am not sure if i am making it more complicated than it has to be or not? Would the easiest method be to map the element with the associated number? It seems like an awful lot for such a simple task.
Last edited on
Although if there was a way to find the size in a function, i would prefer that method.

There is not. The function prototype int min(int a[]) is entirely equivalent to int min(int* a).

if an array had the elements 1 and 10, and i gave it the number 8, it would return 10, if i gave it the number 2, it would return the number 1

What if it had the elements 9 and 10 and you gave it the number 8?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <cstdlib>
#include <ctime>

int dist(int a, int b)
{
    return std::abs(b-a) ;
}

unsigned getClosest( int* nums, unsigned nNums, int target )
{
    unsigned indexOfClosest = 0 ;

    unsigned index = 1 ;
    while ( index < nNums )
    {
        if ( dist(nums[index], target) < dist(nums[indexOfClosest], target) )
            indexOfClosest = index ;

        ++index ;
    }

    return indexOfClosest ;
}

void displayClosest(int* nums, unsigned nNums, int target)
{
    std::cout << "{ " ;
    for ( unsigned i=0; i<nNums; ++i )
        std::cout << nums[i] << ' ' ;
    std::cout << "}\n" ;

    unsigned index = getClosest(nums, nNums, target) ;
    std::cout << "The closest number to " << target << " is " ;
    std::cout << nums[index] << " at index " << index << ".\n" ;
}

int main()
{
    std::srand(std::time(0)) ;

    const unsigned array_size = 3 ;
    int array[array_size] = { std::rand() % 20, std::rand() % 20, std::rand() % 20 } ;

    for ( unsigned i=1; i<21; i+=2 )
        displayClosest(array, array_size, i) ;
}


http://ideone.com/ZgN2LU
Topic archived. No new replies allowed.