1. select

------=_NextPart_000_0007_01C014A3.E46939E0
        charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Hi coders and friends.
I have a little problem . I want to select the number closest to the =
number that is inputed by the user . Example : I have two numbers , 10 =
and 30 . the user inputs the number 25 , so the program MUST select the =
number closest , and that number is 30 . How can I do this ? Thanks . O, =
and by the way , I just made my first REAL program , a awsome sprite =
editor . And you can save the pic you made to files in sequences e.g. =
{1,12,66,8,29 ....... . And it took me 30 min flat ! Its realy easy now =
.=20

Ferdinand

------=_NextPart_000_0007_01C014A3.E46939E0
        charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content=3D"text/html; charset=3Diso-8859-1" =
http-equiv=3DContent-Type>
<META content=3D"MSHTML 5.00.2614.3500" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial>Hi coders and friends.</FONT></DIV>
<DIV>I have a little problem . I want to select the number closest to =
the number=20
that is inputed by the user . Example : I have two numbers , 10 and 30 . =
the=20
user inputs the number 25 , so the program MUST select the number =
closest , and=20
that number is 30 . How can I do this ? Thanks . O, and by the way , I =
just made=20
my first REAL program , a awsome sprite editor . And you can save the =
pic you=20
made to files in sequences e.g. {1,12,66,8,29 ....... . And it took me =
30 min=20
flat ! Its realy easy now . </DIV>
<DIV>&nbsp;</DIV>

------=_NextPart_000_0007_01C014A3.E46939E0--

new topic     » topic index » view message » categorize

2. Re: select

------=_NextPart_000_0006_01C0150F.271A4660
        charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Ferdinand wrote:

Hi coders and friends.

I have a little problem . I want to select the number closest to the =
number that is
inputed by the user . Example : I have two numbers , 10 and 30 . the =
user inputs the number 25 , so the program MUST select the number =
closest , and that number is 30 .
How can I do this ? Thanks . O, and by the way , I just made my first =
REAL program , a awsome sprite editor . And you can save the pic you =
made to files in sequences e.g. {1,12,66,8,29 ....... . And it took me =
30 min flat ! Its realy easy now .

Ferdinand

Interesting! I'm sure people like to see your sprite editor.
Underneath is my solution to your problem:

-- tested code!

include get.e

integer low, high, input
object key

low =3D 10
high =3D 30
input =3D 25

if (input - low) < (high - input) then
    printf(1, "%d is the closest", low)
else
    printf(1, "%d is the closest", high)
end if

key =3D wait_key()

-- find out for yourself what to do when input < low or input > high
-- end of tested code
  ----- Oorspronkelijk bericht -----=20
  Van: =0E=20
  Aan: EUPHORIA at LISTSERV.MUOHIO.EDU=20
  Verzonden: zaterdag 2 september 2000 6:06
  Onderwerp: select


  Hi coders and friends.
  I have a little problem . I want to select the number closest to the =
number that is inputed by the user . Example : I have two numbers , 10 =
and 30 . the user inputs the number 25 , so the program MUST select the =
number closest , and that number is 30 . How can I do this ? Thanks . O, =
and by the way , I just made my first REAL program , a awsome sprite =
editor . And you can save the pic you made to files in sequences e.g. =
{1,12,66,8,29 ....... . And it took me 30 min flat ! Its realy easy now =
.=20

  Ferdinand

------=_NextPart_000_0006_01C0150F.271A4660
        charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content=3D"text/html; charset=3Diso-8859-1" =
http-equiv=3DContent-Type>
<META content=3D"MSHTML 5.00.2314.1000" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV>Ferdinand wrote:</DIV>
<DIV>&nbsp;</DIV>
<DIV>Hi coders and friends.</DIV>
<DIV>&nbsp;</DIV>
<DIV>I have a little problem . I want to select the number closest to =
the number=20
that is<BR>inputed by the user . Example : I have two numbers , 10 and =
30 . the=20
user inputs the number 25 , so the program MUST select the number =
closest , and=20
that number is 30 .<BR>How can I do this ? Thanks . O, and by the way , =
I just=20
made my first REAL program , a awsome sprite editor . And you can save =
the pic=20
you made to files in sequences e.g. {1,12,66,8,29 ....... . And it took =
me 30=20
min flat ! Its realy easy now .</DIV>
<DIV>&nbsp;</DIV>
<DIV>Ferdinand</DIV>
<DIV>&nbsp;</DIV>
<DIV>Interesting! I'm sure people like to see&nbsp;your sprite =
editor.</DIV>
<DIV>Underneath is my solution to your problem:</DIV>
<DIV>&nbsp;</DIV>
<DIV>-- tested code!</DIV>
<DIV>&nbsp;</DIV>
<DIV>include get.e</DIV>
<DIV>&nbsp;</DIV>
<DIV>integer low, high, input<BR>object key</DIV>
<DIV>&nbsp;</DIV>
<DIV>low =3D 10<BR>high =3D 30<BR>input =3D 25</DIV>
<DIV>&nbsp;</DIV>
<DIV>if (input - low) &lt; (high - input) then<BR>&nbsp;&nbsp;&nbsp; =
printf(1,=20
"%d is the closest", low)<BR>else<BR>&nbsp;&nbsp;&nbsp; printf(1, "%d is =
the=20
closest", high)<BR>end if</DIV>
<DIV>&nbsp;</DIV>
<DIV>key =3D wait_key()</DIV>
<DIV>&nbsp;</DIV>
<DIV>-- find out for yourself what to do when input &lt; low or input =
&gt;=20
high</DIV>
<DIV>-- end of tested code</DIV>
<BLOCKQUOTE=20
style=3D"BORDER-LEFT: #000000 2px solid; MARGIN-LEFT: 5px; MARGIN-RIGHT: =
0px; PADDING-LEFT: 5px; PADDING-RIGHT: 0px">
  <DIV style=3D"FONT: 10pt arial">----- Oorspronkelijk bericht ----- =
</DIV>
  <DIV=20
  style=3D"BACKGROUND: #e4e4e4; FONT: 10pt arial; font-color: =
black"><B>Van:</B>=20
  <A href=3D"mailto:mwfch at MWEB.CO.ZA" title=3Dmwfch at MWEB.CO.ZA>=0E</A> =
</DIV>
  <DIV style=3D"FONT: 10pt arial"><B>Aan:</B> <A=20
  href=3D"mailto:EUPHORIA at LISTSERV.MUOHIO.EDU"=20
  title=3DEUPHORIA at LISTSERV.MUOHIO.EDU>EUPHORIA at LISTSERV.MUOHIO.EDU</A> =
</DIV>
  <DIV style=3D"FONT: 10pt arial"><B>Verzonden:</B> zaterdag 2 september =
2000=20
  6:06</DIV>
  <DIV style=3D"FONT: 10pt arial"><B>Onderwerp:</B> select</DIV>
  <DIV><BR></DIV>
  <DIV><FONT face=3DArial>Hi coders and friends.</FONT></DIV>
  <DIV>I have a little problem . I want to select the number closest to =
the=20
  number that is inputed by the user . Example : I have two numbers , 10 =
and 30=20
  . the user inputs the number 25 , so the program MUST select the =
number=20
  closest , and that number is 30 . How can I do this ? Thanks . O, and =
by the=20
  way , I just made my first REAL program , a awsome sprite editor . And =
you can=20
  save the pic you made to files in sequences e.g. {1,12,66,8,29 ....... =
. And=20
  it took me 30 min flat ! Its realy easy now . </DIV>
  <DIV>&nbsp;</DIV>

------=_NextPart_000_0006_01C0150F.271A4660--

new topic     » goto parent     » topic index » view message » categorize

3. Re: select

Ferdinand wrote:

> Example : I have two numbers , 10 and 30 . the user
> inputs the number 25 , so the program MUST select
> the number closest , and that number is 30 . How can
> I do this?

Closest in numeric terms = the smallest difference.

Use subtraction, and abs(). For example:

   abs( 10 - 25 ) = 15
   abs( 30 - 25 ) = 5

For some reason Euphoria doesn't have an abs() function. Here's a simple
version that doesn't work on lists:

   function abs( atom a )
      if a < 0 then
         return -a
      else
         return a
      end if
   end function

Here's some untested code. first and second are the two numbers, and picked
is the value your user entered:

   if abs( first - picked ) = abs( second - picked ) then
     puts( 1, "%d is equally close to to %d and %d\n",
{picked,first,second})

   elsif abs( first - picked ) < abs( second - picked ) then
      puts( 1, "%d is closer to to %d than %d \n", {picked,first,second})

   else
      puts( 1, "%d is closer to to %d than %d \n", {picked,second,first})

   end if

-- David Cuny

new topic     » goto parent     » topic index » view message » categorize

4. Re: select

------=_NextPart_000_003E_01C014DE.28561800
        charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Here is a generic solution when you have several possible numbers and =
want the closest--since a binary search is used, this will work for =
thousands of numbers.

--Tested code ahead

global function select(atom x,sequence choices)
-- assumes choices is sorted and has at least two elements,=20
-- and all elements are atoms
--uses binary search
integer len,hi,lo,mid,c
len=3Dlength(choices)
hi=3Dlen
lo=3D1=20
while hi>=3Dlo do
   mid=3Dfloor((hi+lo)/2)
   c=3Dchoices[mid]
   if x=3Dc then
      return x
   elsif x>c then
      lo=3Dmid+1
   else
      hi=3Dmid-1
   end if
end while
if lo=3D1 then return choices[1] end if
if hi=3Dlen then return choices[len] end if
if x-choices[hi]<choices[lo]-x then
   return choices[hi]
else=20
   return choices[lo]
end if
end function

-- Mike Nelson
  ----- Original Message -----=20
  From: =0E=20
  To: EUPHORIA at LISTSERV.MUOHIO.EDU=20
  Sent: Friday, September 01, 2000 9:06 PM
  Subject: select


  Hi coders and friends.
  I have a little problem . I want to select the number closest to the =
number that is inputed by the user . Example : I have two numbers , 10 =
and 30 . the user inputs the number 25 , so the program MUST select the =
number closest , and that number is 30 . How can I do this ? Thanks . O, =
and by the way , I just made my first REAL program , a awsome sprite =
editor . And you can save the pic you made to files in sequences e.g. =
{1,12,66,8,29 ....... . And it took me 30 min flat ! Its realy easy now =
.=20

  Ferdinand

------=_NextPart_000_003E_01C014DE.28561800
        charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content=3D"text/html; charset=3Diso-8859-1" =
http-equiv=3DContent-Type>
<META content=3D"MSHTML 5.00.2919.6307" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT size=3D2>Here is a generic solution when you have several =
possible=20
numbers and want the closest--since a binary search is used, this will =
work for=20
thousands of numbers.</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT size=3D2>--Tested code ahead</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT size=3D2>global function select(atom x,sequence =
choices)<BR>-- assumes=20
choices is sorted and has at least two elements, <BR>-- and all elements =
are=20
atoms<BR>--uses binary search<BR>integer=20
len,hi,lo,mid,c<BR>len=3Dlength(choices)<BR>hi=3Dlen<BR>lo=3D1 <BR>while =
hi&gt;=3Dlo=20
do<BR>&nbsp;&nbsp; mid=3Dfloor((hi+lo)/2)<BR>&nbsp;&nbsp;=20
c=3Dchoices[mid]<BR>&nbsp;&nbsp; if x=3Dc =
then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
return x<BR>&nbsp;&nbsp; elsif x&gt;c =
then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
lo=3Dmid+1<BR>&nbsp;&nbsp; else<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
hi=3Dmid-1<BR>&nbsp;&nbsp; end if<BR>end while<BR>if lo=3D1 then return =
choices[1]=20
end if<BR>if hi=3Dlen then return choices[len] end if<BR>if=20
x-choices[hi]&lt;choices[lo]-x then<BR>&nbsp;&nbsp; return =
choices[hi]<BR>else=20
<BR>&nbsp;&nbsp; return choices[lo]<BR>end if<BR>end =
function</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT size=3D2>-- Mike Nelson</FONT></DIV>
<BLOCKQUOTE=20
style=3D"BORDER-LEFT: #000000 2px solid; MARGIN-LEFT: 5px; MARGIN-RIGHT: =
0px; PADDING-LEFT: 5px; PADDING-RIGHT: 0px">
  <DIV style=3D"FONT: 10pt arial">----- Original Message ----- </DIV>
  <DIV=20
  style=3D"BACKGROUND: #e4e4e4; FONT: 10pt arial; font-color: =
black"><B>From:</B>=20
  <A href=3D"mailto:mwfch at MWEB.CO.ZA" title=3Dmwfch at MWEB.CO.ZA>=0E</A> =
</DIV>
  <DIV style=3D"FONT: 10pt arial"><B>To:</B> <A=20
  href=3D"mailto:EUPHORIA at LISTSERV.MUOHIO.EDU"=20
  title=3DEUPHORIA at LISTSERV.MUOHIO.EDU>EUPHORIA at LISTSERV.MUOHIO.EDU</A> =
</DIV>
  <DIV style=3D"FONT: 10pt arial"><B>Sent:</B> Friday, September 01, =
2000 9:06=20
  PM</DIV>
  <DIV style=3D"FONT: 10pt arial"><B>Subject:</B> select</DIV>
  <DIV><BR></DIV>
  <DIV><FONT face=3DArial>Hi coders and friends.</FONT></DIV>
  <DIV>I have a little problem . I want to select the number closest to =
the=20
  number that is inputed by the user . Example : I have two numbers , 10 =
and 30=20
  . the user inputs the number 25 , so the program MUST select the =
number=20
  closest , and that number is 30 . How can I do this ? Thanks . O, and =
by the=20
  way , I just made my first REAL program , a awsome sprite editor . And =
you can=20
  save the pic you made to files in sequences e.g. {1,12,66,8,29 ....... =
. And=20
  it took me 30 min flat ! Its realy easy now . </DIV>
  <DIV>&nbsp;</DIV>

------=_NextPart_000_003E_01C014DE.28561800--

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu