473,406 Members | 2,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 12283
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Busin | last post by:
bool arr; arr = true; For the element arr, is there any fastest algorithm to fast find the two elements on both side of "n" and both elements are cloest to "23"? Thanks!
3
by: Senna | last post by:
Hi Have these to work with. int counter = 0; //The total item to fill the collection with int increment = 2; //Any number int current = 244; //Quantity int max = 1290; //Max Quantity int...
1
by: vmoreau | last post by:
I have a text and I need to find a Word that are not enclosed in paranthesis. Can it be done with a regex? Is someone could help me? I am not familar with regex... Example looking for WORD:...
2
by: sturgeob | last post by:
Yep. Newby. I am asked to find the closest value to user defined value if not right on the mark. I can get it to determin it I have selected one of the generated numbers, but am at a loss how to...
5
by: jm.suresh | last post by:
Hi I have three objects, all of them are instances of classes derived from a base class. Now, given one of the instance, I want to find the closest relative of the other two. How can I do this? ...
4
by: Thomas Mlynarczyk | last post by:
Hello, I have two arrays like this: $aSearch = array( 'A', 'B', 'C.D', 'E', 'F' ); $aSubject = array( 'A' =0, 'A.B' =1, 'X' =2, 'C.D.E' =3, 'A.B.C' => 4 ); Now I want to search $aSubject...
22
by: Steve Richter | last post by:
Does the .NET framework provide a class which will find the item in the collection with a key which is closest ( greater than or equal, less than or equal ) to the keys of the collection? ex:...
11
by: C C++ C++ | last post by:
Hi all, got this interview question please respond. How can you quickly find the number of elements stored in a a) static array b) dynamic array ? Rgrds MA
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.