From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,30e0ceaf4e6be70c X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-07-05 03:06:08 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!uio.no!ntnu.no!not-for-mail From: Preben Randhol Newsgroups: comp.lang.ada Subject: Re: Simple program to find average of 3 numbers Date: Sat, 5 Jul 2003 10:06:07 +0000 (UTC) Organization: Norwegian university of science and technology Message-ID: References: <3F0511BF.6000403@attbi.com> <3F053CD9.135E7340@0.0> NNTP-Posting-Host: kiuk0152.chembio.ntnu.no X-Trace: tyfon.itea.ntnu.no 1057399567 24564 129.241.83.78 (5 Jul 2003 10:06:07 GMT) X-Complaints-To: usenet@itea.ntnu.no NNTP-Posting-Date: Sat, 5 Jul 2003 10:06:07 +0000 (UTC) User-Agent: slrn/0.9.7.4 (Linux) Xref: archiver1.google.com comp.lang.ada:40076 Date: 2003-07-05T10:06:07+00:00 List-Id: Stuart Palin wrote: > "Robert I. Eachus" wrote: >> >> prashna wrote: >> > Hi friends, >> > >> > What is wrong in the following program which is giving constraint >> > error? >> > >> > procedure average is type ONE_TO_100 is range 1 .. 100; >> > >> > OBJ1, OBJ2, OBJ3, AVG : ONE_TO_100; begin OBJ1 := 70; OBJ2 := 70; >> > OBJ3 := 70; AVG := OBJ1+OBJ2+OBJ3/3; end average; >> > >> >> Oh, and then I could talk about your formatting, and identifier style. >> But I will address those by example. Try: >> >> procedure Average is >> subtype One_to_100 is Integer range 1 .. 100; >> Obj_1, Obj_2, Obj_3: One_to_100 := 70; >> Avg: One_to_100; >> begin >> Avg := (Obj_1+Obj_2+Obj_3)/3; >> -- I assume you really wanted the average, rather than 163. >> end Average; > > You might also want to consider what values the intermediate > calculation Obj_1+Obj_2+Obj_3 can take. In this instance > because your type is constrained to a number which should be > well within the bounds of integer you would not experience a > problem. Which instance are you refering to? Mr Eachus' subtype or OP's type declaration? If you do the calculations above aren't the intermediate variable of the same type (ONE_TO_100) with same contraint unless you use a subtype to Integer like Mr Eachus? -- Ada95 is good for you. http://www.crystalcode.com/codemage/MainMenu/Coding/Ada/IntroducingAda.php