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=-0.5 required=5.0 tests=BAYES_00,INVALID_MSGID, PP_MIME_FAKE_ASCII_TEXT autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII X-Google-Thread: 103376,7a5193bb70b7caf8 X-Google-Attributes: gid103376,public From: ok@goanna.cs.rmit.edu.au (Richard A. O'Keefe) Subject: Re: Help with Ada 95 Date: 1997/10/02 Message-ID: <60vl4h$t2v$1@goanna.cs.rmit.edu.au>#1/1 X-Deja-AN: 277233532 References: <3431E010.4DB9@earthlink.net> Organization: Comp Sci, RMIT University, Melbourne, Australia. NNTP-Posting-User: ok Newsgroups: comp.lang.ada Date: 1997-10-02T00:00:00+00:00 List-Id: masi@earthlink.net writes: >Here is two programs I'm supposed to make: You do not say what you are having trouble with. There is nothing in the two problems that is particularly hard in Ada, so I suppose that your problem is in understanding how to think about such problems. RUN, DO NOT WALK, TO YOUR TUTOR AND ASK FOR HELP. People on the net _cannot_ provide the same advice directed at your specific needs that a face-to-face tutor can. >1) function Root (A, B, C : in Float) return Float; >-- >-- This returns the root of the equation >-- Ax + B = C >-- if one exists, otherwise it raises a Constraint_Error. >-- How do you know whether a solution exists? How do you find a solution when one does exist? How do you raise Constraint_Error? Let's take you through the high school mathematics: A*X + B = C <=> by Isolation A*X = C-B <=> by Isolation A /= 0 and then X = (C-B)/A or else A = 0 and then C = B and then X = anything But you have to return a definite solution, so you are going to have to treat the A = 0 and C = B case as an error too. So: a solution exists if and only if (a) A is not zero (b) C-B can be computed without overflow &c (c) (C-B)/A can be computed without overflow &c If a solution exists, (C-B)/A will compute it As for raising Constraint_Error, there are two ways to do it, both of which you will find in your textbook: (x) 'raise Constraint_Error;' (y) do something that will make the run-time system raise Constraint_Error for you. Here it is in a sort of Clean/ML hybrid: root A B C | A /= 0 = (C-B)/A root A B C | A == 0 = fail_with "Constraint_Error" >2) function DigitSum ( N : Positive ) return Positive; >-- >-- This returns the sum of the digits of the number N. >-- >-- Example: DigitSum (1532) = 11 (since 1+5+3+2 = 11). Let me heartily recommend Polya: "How to Solve It" Dromey: "How to Solve It by Computer" Can you reduce this to a simpler problem? Like - find the digits of a number - sum digts that you are given This one is easiest to solve recursively. The number N[1]N[2]....N[k] has only one digit (N[k]) if it is less than 10 has the digits of N[1]...N[k-1] followed by the digit N[k] if it is greater than or equal to 10 N[1]...N[k-1] is the number divided by 10, N[k] is the remainder So, the sum of the digits of a number if it is less than 10, the sum of the one digit which is the number itself if it is greater than or equal to 10, the sum of the digits of the number divided by 10 plus the digit which is the number modulo 10 Again in a Clean/ML hybrid: digitsum N | N < 10 = N digitsum N | N >= 10 = digitsum (N/10) + (N mod 10) (I note that this is actual code in a strongly typed language; you'll have to translate it into Ada.) -- John �neas Byron O'Keefe; 1921/02/04-1997-09-27; TLG,TLTA,BBTNOTL. Richard A. O'Keefe; http://www.cs.rmit.edu.au/%7Eok; RMIT Comp.Sci.