comp.lang.ada
 help / color / mirror / Atom feed
From: ok@goanna.cs.rmit.edu.au (Richard A. O'Keefe)
Subject: Re: Help with Ada 95
Date: 1997/10/02
Date: 1997-10-02T00:00:00+00:00	[thread overview]
Message-ID: <60vl4h$t2v$1@goanna.cs.rmit.edu.au> (raw)
In-Reply-To: 3431E010.4DB9@earthlink.net

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 2934 bytes --]


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.




  reply	other threads:[~1997-10-02  0:00 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-09-30  0:00 Help with Ada 95 masi
1997-10-02  0:00 ` Richard A. O'Keefe [this message]
1997-10-02  0:00 ` Alan Brain
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox