nearest number

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
omar shaaban
Posts: 616
Joined: Wed Nov 01, 2006 6:26 pm
Location: Cairo,Egypt
Contact:

nearest number

Post by omar shaaban »

i have a set numbers and i want to see which number is nearest to "a"
example:
set of numbers:1,3,8,5,78
a=6
so the nearest number is 6!
i remember that there was a function like this but i cant remeber any help! :roll:
Perceval
Posts: 158
Joined: Tue May 30, 2006 2:42 pm

Post by Perceval »

Looks like a student exercise... :wink:
i have a set numbers and i want to see which number is nearest to "a"
example:
set of numbers:1,3,8,5,78
a=6
so the nearest number is 6!
i remember that there was a function like this but i cant remeber any help! Rolling Eyes
6 :shock: I think it's 5 :lol:

Do you really need a set ? Because it would be a lot faster to use a sorted array. May be something like this (not tested) :

Code: Select all

int i=0;
int j=arraysize;
int m=(i+j)/2;
int n=tab[m];
while(i!=j)
{
	if(tab[m]==a) return a;
	if(abs(tab[m]-a) < abs(n-a))
	{
		n=tab[m];
	}
	if(tab[m]<a)
	{
		i=m;
	}
	else
	{
		j=m;
	}
	m=(i+j)/2;
}
return n;
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

You shouldn't need a function from a library to do that, it's a rather trivial thing to carry out as Perceval has just shown!
Image Image Image
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

Just because you are able to write something yourself isn't reason enough to expect that it isn't already written. Not only that, but the code provided expects that the collection is sorted, but the given data indicates that it is not sorted. So it won't even work.

There are some questions remaining. You say you want to get the number that is nearest to a. What if the collection includes the numbers 1,9,3,4? Should it return 1 or 3 in this case? Do you want to know the value of nearest number, or do you need to know where it is in the collection? If you want the position of the nearest element, and the collection is 1, 1, 1, 1, 1, and a is 2, which element would you expect it to return?

I'm assuming that when you say you have a set of numbers, you mean that you have a collection of numbers, in an array, list or vector.

If the collection is sorted [as assumed by others, and that is probably a good thing to do], the easy way would be to use the lower_bound algorithm. It assumes the sequence is ordered, and will return to you an iterator to the first element equal to or larger than the value you provide.

Travis
DarkDepths
Posts: 126
Joined: Sun Apr 02, 2006 1:21 am
Location: Canada

Post by DarkDepths »

My rather primitive method would be to take the numbers from the set and subtract "a" from each, and take the absolute value of each. Then I would compare all of them and see which is the lowest number, since "a"-"a" would result in 0, and "a" - "a-1" would result in 1.
Post Reply