469,898 Members | 1,801 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,898 developers. It's quick & easy.

find closest number from array

I have ListArray with number in Eg:
1, 1.456, 2.43, 4, 6.78
next i have a decimal variable containing one number EG:
1.786

Could someone please tell me how i find the "closest match" number below the
decimal variable from the arraylist.

Thanks ever so much in advance
Nov 16 '05 #1
13 11886
Mike,

It seems like your array is sorted. If this is the case, then you can
call the static BinarySearch method on the Array class to find out.

You would pass your array, along with your number. If the result is a
non negative number, then that is the index in the array that it is found
in. If the number is negative, then that is equal to:

<result> = (<index of next highest value> + 1) * -1

So, in your case, it will return -3.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"mike" <sp******@hotmail.com> wrote in message
news:Dl******************@doctor.cableinet.net...
I have ListArray with number in Eg:
1, 1.456, 2.43, 4, 6.78
next i have a decimal variable containing one number EG:
1.786

Could someone please tell me how i find the "closest match" number below the decimal variable from the arraylist.

Thanks ever so much in advance

Nov 16 '05 #2
Mike,

It seems like your array is sorted. If this is the case, then you can
call the static BinarySearch method on the Array class to find out.

You would pass your array, along with your number. If the result is a
non negative number, then that is the index in the array that it is found
in. If the number is negative, then that is equal to:

<result> = (<index of next highest value> + 1) * -1

So, in your case, it will return -3.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"mike" <sp******@hotmail.com> wrote in message
news:Dl******************@doctor.cableinet.net...
I have ListArray with number in Eg:
1, 1.456, 2.43, 4, 6.78
next i have a decimal variable containing one number EG:
1.786

Could someone please tell me how i find the "closest match" number below the decimal variable from the arraylist.

Thanks ever so much in advance

Nov 16 '05 #3
Fantastic,

Sorry to be a pain but could you explain how i find the index of the closest
match when binarysearch produces a negative number i'm really confused?

Thanks
Mike
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:un**************@TK2MSFTNGP10.phx.gbl...
Mike,

It seems like your array is sorted. If this is the case, then you can
call the static BinarySearch method on the Array class to find out.

You would pass your array, along with your number. If the result is a
non negative number, then that is the index in the array that it is found
in. If the number is negative, then that is equal to:

<result> = (<index of next highest value> + 1) * -1

So, in your case, it will return -3.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"mike" <sp******@hotmail.com> wrote in message
news:Dl******************@doctor.cableinet.net...
I have ListArray with number in Eg:
1, 1.456, 2.43, 4, 6.78
next i have a decimal variable containing one number EG:
1.786

Could someone please tell me how i find the "closest match" number below

the
decimal variable from the arraylist.

Thanks ever so much in advance


Nov 16 '05 #4
Fantastic,

Sorry to be a pain but could you explain how i find the index of the closest
match when binarysearch produces a negative number i'm really confused?

Thanks
Mike
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:un**************@TK2MSFTNGP10.phx.gbl...
Mike,

It seems like your array is sorted. If this is the case, then you can
call the static BinarySearch method on the Array class to find out.

You would pass your array, along with your number. If the result is a
non negative number, then that is the index in the array that it is found
in. If the number is negative, then that is equal to:

<result> = (<index of next highest value> + 1) * -1

So, in your case, it will return -3.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"mike" <sp******@hotmail.com> wrote in message
news:Dl******************@doctor.cableinet.net...
I have ListArray with number in Eg:
1, 1.456, 2.43, 4, 6.78
next i have a decimal variable containing one number EG:
1.786

Could someone please tell me how i find the "closest match" number below

the
decimal variable from the arraylist.

Thanks ever so much in advance


Nov 16 '05 #5
Mike,

It produces a negative number to let you know that there was not an
exact match found in the array. If it gave you a positive number, then you
would think that it actually found the element you are looking for.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"mike" <sp******@hotmail.com> wrote in message
news:ld******************@doctor.cableinet.net...
Fantastic,

Sorry to be a pain but could you explain how i find the index of the closest match when binarysearch produces a negative number i'm really confused?

Thanks
Mike
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:un**************@TK2MSFTNGP10.phx.gbl...
Mike,

It seems like your array is sorted. If this is the case, then you can call the static BinarySearch method on the Array class to find out.

You would pass your array, along with your number. If the result is a non negative number, then that is the index in the array that it is found in. If the number is negative, then that is equal to:

<result> = (<index of next highest value> + 1) * -1

So, in your case, it will return -3.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"mike" <sp******@hotmail.com> wrote in message
news:Dl******************@doctor.cableinet.net...
I have ListArray with number in Eg:
1, 1.456, 2.43, 4, 6.78
next i have a decimal variable containing one number EG:
1.786

Could someone please tell me how i find the "closest match" number
below the
decimal variable from the arraylist.

Thanks ever so much in advance



Nov 16 '05 #6
Mike,

It produces a negative number to let you know that there was not an
exact match found in the array. If it gave you a positive number, then you
would think that it actually found the element you are looking for.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"mike" <sp******@hotmail.com> wrote in message
news:ld******************@doctor.cableinet.net...
Fantastic,

Sorry to be a pain but could you explain how i find the index of the closest match when binarysearch produces a negative number i'm really confused?

Thanks
Mike
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:un**************@TK2MSFTNGP10.phx.gbl...
Mike,

It seems like your array is sorted. If this is the case, then you can call the static BinarySearch method on the Array class to find out.

You would pass your array, along with your number. If the result is a non negative number, then that is the index in the array that it is found in. If the number is negative, then that is equal to:

<result> = (<index of next highest value> + 1) * -1

So, in your case, it will return -3.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"mike" <sp******@hotmail.com> wrote in message
news:Dl******************@doctor.cableinet.net...
I have ListArray with number in Eg:
1, 1.456, 2.43, 4, 6.78
next i have a decimal variable containing one number EG:
1.786

Could someone please tell me how i find the "closest match" number
below the
decimal variable from the arraylist.

Thanks ever so much in advance



Nov 16 '05 #7
Thanks Nicholas for your help i know you must be getting fed up of me,
but....

when i have a negative number how can i get the "next" element in the array?
in other words if i have a negative how to i make in into the index of the
next number along?

thanks again !!

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:ut**************@TK2MSFTNGP10.phx.gbl...
Mike,

It produces a negative number to let you know that there was not an
exact match found in the array. If it gave you a positive number, then you would think that it actually found the element you are looking for.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"mike" <sp******@hotmail.com> wrote in message
news:ld******************@doctor.cableinet.net...
Fantastic,

Sorry to be a pain but could you explain how i find the index of the closest
match when binarysearch produces a negative number i'm really confused?

Thanks
Mike
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote

in
message news:un**************@TK2MSFTNGP10.phx.gbl...
Mike,

It seems like your array is sorted. If this is the case, then you can call the static BinarySearch method on the Array class to find out.

You would pass your array, along with your number. If the result
is a non negative number, then that is the index in the array that it is found in. If the number is negative, then that is equal to:

<result> = (<index of next highest value> + 1) * -1

So, in your case, it will return -3.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"mike" <sp******@hotmail.com> wrote in message
news:Dl******************@doctor.cableinet.net...
> I have ListArray with number in Eg:
> 1, 1.456, 2.43, 4, 6.78
> next i have a decimal variable containing one number EG:
> 1.786
>
> Could someone please tell me how i find the "closest match" number below the
> decimal variable from the arraylist.
>
> Thanks ever so much in advance
>
>



Nov 16 '05 #8
Thanks Nicholas for your help i know you must be getting fed up of me,
but....

when i have a negative number how can i get the "next" element in the array?
in other words if i have a negative how to i make in into the index of the
next number along?

thanks again !!

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:ut**************@TK2MSFTNGP10.phx.gbl...
Mike,

It produces a negative number to let you know that there was not an
exact match found in the array. If it gave you a positive number, then you would think that it actually found the element you are looking for.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"mike" <sp******@hotmail.com> wrote in message
news:ld******************@doctor.cableinet.net...
Fantastic,

Sorry to be a pain but could you explain how i find the index of the closest
match when binarysearch produces a negative number i'm really confused?

Thanks
Mike
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote

in
message news:un**************@TK2MSFTNGP10.phx.gbl...
Mike,

It seems like your array is sorted. If this is the case, then you can call the static BinarySearch method on the Array class to find out.

You would pass your array, along with your number. If the result
is a non negative number, then that is the index in the array that it is found in. If the number is negative, then that is equal to:

<result> = (<index of next highest value> + 1) * -1

So, in your case, it will return -3.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"mike" <sp******@hotmail.com> wrote in message
news:Dl******************@doctor.cableinet.net...
> I have ListArray with number in Eg:
> 1, 1.456, 2.43, 4, 6.78
> next i have a decimal variable containing one number EG:
> 1.786
>
> Could someone please tell me how i find the "closest match" number below the
> decimal variable from the arraylist.
>
> Thanks ever so much in advance
>
>



Nov 16 '05 #9
Mike,

I originally posted the formula:

<result> = (<index of next highest value> + 1) * -1

So, if you do this:

abs(<result of call to BinarySearch>) - 1

Applying that, you get 2, which is the index of the value 2.43, which is
the value that comes after the search value of 1.786. At this point, you
could compare it to the element before (at index 1) and determine which
element is closer.

Don't worry about asking for clarification. If it was a bother, I would
have stopped answering you a long time ago =)

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"mike" <sp******@hotmail.com> wrote in message
news:go*******************@doctor.cableinet.net...
Thanks Nicholas for your help i know you must be getting fed up of me,
but....

when i have a negative number how can i get the "next" element in the array? in other words if i have a negative how to i make in into the index of the
next number along?

thanks again !!

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:ut**************@TK2MSFTNGP10.phx.gbl...
Mike,

It produces a negative number to let you know that there was not an
exact match found in the array. If it gave you a positive number, then you
would think that it actually found the element you are looking for.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"mike" <sp******@hotmail.com> wrote in message
news:ld******************@doctor.cableinet.net...
Fantastic,

Sorry to be a pain but could you explain how i find the index of the

closest
match when binarysearch produces a negative number i'm really confused?
Thanks
Mike
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com>
wrote in
message news:un**************@TK2MSFTNGP10.phx.gbl...
> Mike,
>
> It seems like your array is sorted. If this is the case, then
you can
> call the static BinarySearch method on the Array class to find out.
>
> You would pass your array, along with your number. If the

result is
a
> non negative number, then that is the index in the array that it is

found
> in. If the number is negative, then that is equal to:
>
> <result> = (<index of next highest value> + 1) * -1
>
> So, in your case, it will return -3.
>
> Hope this helps.
>
>
> --
> - Nicholas Paldino [.NET/C# MVP]
> - mv*@spam.guard.caspershouse.com
>
>
> "mike" <sp******@hotmail.com> wrote in message
> news:Dl******************@doctor.cableinet.net...
> > I have ListArray with number in Eg:
> > 1, 1.456, 2.43, 4, 6.78
> > next i have a decimal variable containing one number EG:
> > 1.786
> >
> > Could someone please tell me how i find the "closest match" number

below
> the
> > decimal variable from the arraylist.
> >
> > Thanks ever so much in advance
> >
> >
>
>



Nov 16 '05 #10
Yup, keep asking questions :)

Clarification = good
Buggy code = bad

--
Adam Clauss
ca*****@tamu.edu
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:Ol*************@TK2MSFTNGP10.phx.gbl...
Mike,

I originally posted the formula:

<result> = (<index of next highest value> + 1) * -1

So, if you do this:

abs(<result of call to BinarySearch>) - 1

Applying that, you get 2, which is the index of the value 2.43, which is
the value that comes after the search value of 1.786. At this point, you
could compare it to the element before (at index 1) and determine which
element is closer.

Don't worry about asking for clarification. If it was a bother, I would
have stopped answering you a long time ago =)

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"mike" <sp******@hotmail.com> wrote in message
news:go*******************@doctor.cableinet.net...
Thanks Nicholas for your help i know you must be getting fed up of me,
but....

when i have a negative number how can i get the "next" element in the

array?
in other words if i have a negative how to i make in into the index of the
next number along?

thanks again !!

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote

in
message news:ut**************@TK2MSFTNGP10.phx.gbl...
Mike,

It produces a negative number to let you know that there was not an
exact match found in the array. If it gave you a positive number, then

you
would think that it actually found the element you are looking for.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"mike" <sp******@hotmail.com> wrote in message
news:ld******************@doctor.cableinet.net...
> Fantastic,
>
> Sorry to be a pain but could you explain how i find the index of the
closest
> match when binarysearch produces a negative number i'm really confused? >
> Thanks
> Mike
>
>
> "Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
> message news:un**************@TK2MSFTNGP10.phx.gbl...
> > Mike,
> >
> > It seems like your array is sorted. If this is the case, then you can
> > call the static BinarySearch method on the Array class to find out.
> >
> > You would pass your array, along with your number. If the

result
is
a
> > non negative number, then that is the index in the array that it is
found
> > in. If the number is negative, then that is equal to:
> >
> > <result> = (<index of next highest value> + 1) * -1
> >
> > So, in your case, it will return -3.
> >
> > Hope this helps.
> >
> >
> > --
> > - Nicholas Paldino [.NET/C# MVP]
> > - mv*@spam.guard.caspershouse.com
> >
> >
> > "mike" <sp******@hotmail.com> wrote in message
> > news:Dl******************@doctor.cableinet.net...
> > > I have ListArray with number in Eg:
> > > 1, 1.456, 2.43, 4, 6.78
> > > next i have a decimal variable containing one number EG:
> > > 1.786
> > >
> > > Could someone please tell me how i find the "closest match" number
below
> > the
> > > decimal variable from the arraylist.
> > >
> > > Thanks ever so much in advance
> > >
> > >
> >
> >
>
>



Nov 16 '05 #11
I must be in a head block, i'm jsut not getting it at all:
my problem is the negative numbers, there just not coming out how i expect
here is the code:
=================================

Object objitem= itemsWeight;
int myIndex = arrWeightList.BinarySearch(objitem);

if(myIndex < 0)

{

myIndex = (Math.Abs(myIndex) + 1) * -1;

}

==========================================

Array holds: 1, 1.0587, 2, 3, 4,
objitem holds: 1.500
myIndex outputs: -4

objitem holds: 3.500
myIndex outputs: -6
===========================

I'm completely stuck with this and I've searched quite a bit on the net I
can't find anything :(

Is my code right? I bet it's so blindingly obvious, I just can't see it.

Thanks

Mike


"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:Ol*************@TK2MSFTNGP10.phx.gbl...
Mike,

I originally posted the formula:

<result> = (<index of next highest value> + 1) * -1

So, if you do this:

abs(<result of call to BinarySearch>) - 1

Applying that, you get 2, which is the index of the value 2.43, which is the value that comes after the search value of 1.786. At this point, you
could compare it to the element before (at index 1) and determine which
element is closer.

Don't worry about asking for clarification. If it was a bother, I would have stopped answering you a long time ago =)

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"mike" <sp******@hotmail.com> wrote in message
news:go*******************@doctor.cableinet.net...
Thanks Nicholas for your help i know you must be getting fed up of me,
but....

when i have a negative number how can i get the "next" element in the

array?
in other words if i have a negative how to i make in into the index of the
next number along?

thanks again !!

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote

in
message news:ut**************@TK2MSFTNGP10.phx.gbl...
Mike,

It produces a negative number to let you know that there was not an exact match found in the array. If it gave you a positive number, then
you
would think that it actually found the element you are looking for.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"mike" <sp******@hotmail.com> wrote in message
news:ld******************@doctor.cableinet.net...
> Fantastic,
>
> Sorry to be a pain but could you explain how i find the index of the
closest
> match when binarysearch produces a negative number i'm really

confused? >
> Thanks
> Mike
>
>
> "Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
> message news:un**************@TK2MSFTNGP10.phx.gbl...
> > Mike,
> >
> > It seems like your array is sorted. If this is the case, then you can
> > call the static BinarySearch method on the Array class to find

out. > >
> > You would pass your array, along with your number. If the

result
is
a
> > non negative number, then that is the index in the array that it is found
> > in. If the number is negative, then that is equal to:
> >
> > <result> = (<index of next highest value> + 1) * -1
> >
> > So, in your case, it will return -3.
> >
> > Hope this helps.
> >
> >
> > --
> > - Nicholas Paldino [.NET/C# MVP]
> > - mv*@spam.guard.caspershouse.com
> >
> >
> > "mike" <sp******@hotmail.com> wrote in message
> > news:Dl******************@doctor.cableinet.net...
> > > I have ListArray with number in Eg:
> > > 1, 1.456, 2.43, 4, 6.78
> > > next i have a decimal variable containing one number EG:
> > > 1.786
> > >
> > > Could someone please tell me how i find the "closest match" number below
> > the
> > > decimal variable from the arraylist.
> > >
> > > Thanks ever so much in advance
> > >
> > >
> >
> >
>
>



Nov 16 '05 #12
I must be in a head block, i'm jsut not getting it at all:
my problem is the negative numbers, there just not coming out how i expect
here is the code:
=================================

Object objitem= itemsWeight;
int myIndex = arrWeightList.BinarySearch(objitem);

if(myIndex < 0)

{

myIndex = (Math.Abs(myIndex) + 1) * -1;

}

==========================================

Array holds: 1, 1.0587, 2, 3, 4,
objitem holds: 1.500
myIndex outputs: -4

objitem holds: 3.500
myIndex outputs: -6
===========================

I'm completely stuck with this and I've searched quite a bit on the net I
can't find anything :(

Is my code right? I bet it's so blindingly obvious, I just can't see it.

Thanks

Mike


"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:Ol*************@TK2MSFTNGP10.phx.gbl...
Mike,

I originally posted the formula:

<result> = (<index of next highest value> + 1) * -1

So, if you do this:

abs(<result of call to BinarySearch>) - 1

Applying that, you get 2, which is the index of the value 2.43, which is the value that comes after the search value of 1.786. At this point, you
could compare it to the element before (at index 1) and determine which
element is closer.

Don't worry about asking for clarification. If it was a bother, I would have stopped answering you a long time ago =)

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"mike" <sp******@hotmail.com> wrote in message
news:go*******************@doctor.cableinet.net...
Thanks Nicholas for your help i know you must be getting fed up of me,
but....

when i have a negative number how can i get the "next" element in the

array?
in other words if i have a negative how to i make in into the index of the
next number along?

thanks again !!

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote

in
message news:ut**************@TK2MSFTNGP10.phx.gbl...
Mike,

It produces a negative number to let you know that there was not an exact match found in the array. If it gave you a positive number, then
you
would think that it actually found the element you are looking for.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"mike" <sp******@hotmail.com> wrote in message
news:ld******************@doctor.cableinet.net...
> Fantastic,
>
> Sorry to be a pain but could you explain how i find the index of the
closest
> match when binarysearch produces a negative number i'm really

confused? >
> Thanks
> Mike
>
>
> "Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
> message news:un**************@TK2MSFTNGP10.phx.gbl...
> > Mike,
> >
> > It seems like your array is sorted. If this is the case, then you can
> > call the static BinarySearch method on the Array class to find

out. > >
> > You would pass your array, along with your number. If the

result
is
a
> > non negative number, then that is the index in the array that it is found
> > in. If the number is negative, then that is equal to:
> >
> > <result> = (<index of next highest value> + 1) * -1
> >
> > So, in your case, it will return -3.
> >
> > Hope this helps.
> >
> >
> > --
> > - Nicholas Paldino [.NET/C# MVP]
> > - mv*@spam.guard.caspershouse.com
> >
> >
> > "mike" <sp******@hotmail.com> wrote in message
> > news:Dl******************@doctor.cableinet.net...
> > > I have ListArray with number in Eg:
> > > 1, 1.456, 2.43, 4, 6.78
> > > next i have a decimal variable containing one number EG:
> > > 1.786
> > >
> > > Could someone please tell me how i find the "closest match" number below
> > the
> > > decimal variable from the arraylist.
> > >
> > > Thanks ever so much in advance
> > >
> > >
> >
> >
>
>




Nov 16 '05 #13
Look closer at his formula. Does:
"myIndex = (Math.Abs(myIndex) + 1) * -1;"

look like:
"abs(<result of call to BinarySearch>) - 1"

The formula you have in there now is what the number IS - you need to reverse those operations in order to get the true value.
Hence, the second formula.
--
Adam Clauss
ca*****@tamu.edu
"mike" <sp******@hotmail.com> wrote in message news:SF*******************@doctor.cableinet.net...
I must be in a head block, i'm jsut not getting it at all:
my problem is the negative numbers, there just not coming out how i expect
here is the code:
=================================

Object objitem= itemsWeight;
int myIndex = arrWeightList.BinarySearch(objitem);

if(myIndex < 0)

{

myIndex = (Math.Abs(myIndex) + 1) * -1;

}

==========================================

Array holds: 1, 1.0587, 2, 3, 4,
objitem holds: 1.500
myIndex outputs: -4

objitem holds: 3.500
myIndex outputs: -6
===========================

I'm completely stuck with this and I've searched quite a bit on the net I
can't find anything :(

Is my code right? I bet it's so blindingly obvious, I just can't see it.

Thanks

Mike


"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:Ol*************@TK2MSFTNGP10.phx.gbl...
Mike,

I originally posted the formula:

<result> = (<index of next highest value> + 1) * -1

So, if you do this:

abs(<result of call to BinarySearch>) - 1

Applying that, you get 2, which is the index of the value 2.43, which

is
the value that comes after the search value of 1.786. At this point, you
could compare it to the element before (at index 1) and determine which
element is closer.

Don't worry about asking for clarification. If it was a bother, I

would
have stopped answering you a long time ago =)

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"mike" <sp******@hotmail.com> wrote in message
news:go*******************@doctor.cableinet.net...
Thanks Nicholas for your help i know you must be getting fed up of me,
but....

when i have a negative number how can i get the "next" element in the

array?
in other words if i have a negative how to i make in into the index of the next number along?

thanks again !!

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote

in
message news:ut**************@TK2MSFTNGP10.phx.gbl...
> Mike,
>
> It produces a negative number to let you know that there was not an > exact match found in the array. If it gave you a positive number, then you
> would think that it actually found the element you are looking for.
>
>
> --
> - Nicholas Paldino [.NET/C# MVP]
> - mv*@spam.guard.caspershouse.com
>
> "mike" <sp******@hotmail.com> wrote in message
> news:ld******************@doctor.cableinet.net...
> > Fantastic,
> >
> > Sorry to be a pain but could you explain how i find the index of the
> closest
> > match when binarysearch produces a negative number i'm really

confused?
> >
> > Thanks
> > Mike
> >
> >
> > "Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com>

wrote
> in
> > message news:un**************@TK2MSFTNGP10.phx.gbl...
> > > Mike,
> > >
> > > It seems like your array is sorted. If this is the case, then

you
> can
> > > call the static BinarySearch method on the Array class to find out. > > >
> > > You would pass your array, along with your number. If the

result
is
> a
> > > non negative number, then that is the index in the array that it is > found
> > > in. If the number is negative, then that is equal to:
> > >
> > > <result> = (<index of next highest value> + 1) * -1
> > >
> > > So, in your case, it will return -3.
> > >
> > > Hope this helps.
> > >
> > >
> > > --
> > > - Nicholas Paldino [.NET/C# MVP]
> > > - mv*@spam.guard.caspershouse.com
> > >
> > >
> > > "mike" <sp******@hotmail.com> wrote in message
> > > news:Dl******************@doctor.cableinet.net...
> > > > I have ListArray with number in Eg:
> > > > 1, 1.456, 2.43, 4, 6.78
> > > > next i have a decimal variable containing one number EG:
> > > > 1.786
> > > >
> > > > Could someone please tell me how i find the "closest match" number > below
> > > the
> > > > decimal variable from the arraylist.
> > > >
> > > > Thanks ever so much in advance
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Nov 16 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Busin | last post: by
5 posts views Thread by jm.suresh | last post: by
4 posts views Thread by Thomas Mlynarczyk | last post: by
22 posts views Thread by Steve Richter | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.