comp.lang.ada
 help / color / mirror / Atom feed
* Help with Ada 95
@ 1997-09-30  0:00 masi
  1997-10-02  0:00 ` Alan Brain
  1997-10-02  0:00 ` Richard A. O'Keefe
  0 siblings, 2 replies; 3+ messages in thread
From: masi @ 1997-09-30  0:00 UTC (permalink / raw)



Hi, I'm taking my first Ada 95 class at my college and am having a
problem  with it.  I have small projects due soon, and need some kind of
instructions.  Could anyone help me?  

Also I'm looking for any good book for Ada.  I use "Programming in Ada
95" by Barnes, but it's not very helpful.  
Thanks in advance.

Masi
Here is two programs I'm supposed to make:


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.
--

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).




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Help with Ada 95
  1997-09-30  0:00 Help with Ada 95 masi
@ 1997-10-02  0:00 ` Alan Brain
  1997-10-02  0:00 ` Richard A. O'Keefe
  1 sibling, 0 replies; 3+ messages in thread
From: Alan Brain @ 1997-10-02  0:00 UTC (permalink / raw)



masi@earthlink.net wrote:
> 
> Hi, I'm taking my first Ada 95 class at my college and am having a
> problem  with it.  I have small projects due soon, and need some kind of
> instructions.  Could anyone help me?

Try the lovelace tutorial at http://www.adahome.com

> Here is two programs I'm supposed to make:

OK, without doing your work for you, here are some questions:

> 
> 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.


OK, See if I understand the question:
let's look at the equation first. Some really elementary
algebraic manipulation:

Ax+B = C becomes (by subtracting C from both sides)
Ax = C-B, and this becomes (by dividing both sides by A)
x = (C-B)/A

Now this will always exist, except under WHAT conditions?



> 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).

OK, the input is an integer (Positive actually). You need the
sum of the digits.
There are several ways of doing this.

Look up mod. I assume you're familiar with modular arithmetic,
such as in telling time: 11 o'clock + 2 hours later = 1 o'clock,
or 13 mod 12.

If you've covered STRINGS, also look up 'IMAGE.

Which would be the best to use?

-- 
Not the Australian Dairy Farmers Association,
   the Australian Defence Force Academy.
aebrain@dynamite.com.au abrain@cs.adfa.oz.au




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Help with Ada 95
  1997-09-30  0:00 Help with Ada 95 masi
  1997-10-02  0:00 ` Alan Brain
@ 1997-10-02  0:00 ` Richard A. O'Keefe
  1 sibling, 0 replies; 3+ messages in thread
From: Richard A. O'Keefe @ 1997-10-02  0:00 UTC (permalink / raw)


[-- 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.




^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~1997-10-02  0:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-09-30  0:00 Help with Ada 95 masi
1997-10-02  0:00 ` Alan Brain
1997-10-02  0:00 ` Richard A. O'Keefe

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