comp.lang.ada
 help / color / mirror / Atom feed
From: Robert A Duff <bobduff@shell01.TheWorld.com>
Subject: Re: Need Help On Ada95 Problem
Date: Sun, 12 Feb 2012 17:26:39 -0500
Date: 2012-02-12T17:26:39-05:00	[thread overview]
Message-ID: <wccpqdjpu1s.fsf@shell01.TheWorld.com> (raw)
In-Reply-To: 43ebdc77-d90a-4213-8e74-db92ad373e14@m7g2000vbw.googlegroups.com

Will <willmann817@gmail.com> writes:

> Here Is The Solution and it does work so all of you can understand.  I
> am fairly new to Ada95 so I have not been introduced to Arrays and
> Case and all that stuff but I do understand the solutions here. I did
> use the ASCII table and if you view it and go through this by hand you
> will understand why it works. The solutions are as follows:

Not bad.  I'll review your code below.  I realize that some
of the things I say involve features you maybe haven't learned
about yet.  But you will.

You should know that you HAVE been introduced to arrays, perhaps without
realizing it.  String is an array type:

    type String is array (Positive range <>) of Character;

You should try writing the inverse function (Decrypt).
That's somewhat harder.

>    function Encrypt(PIN : String) return String is
>
>       -- Convert the 4-digit PIN to the corresponding 4-letter code.
>
>       -- Assume PIN'Length is 4 and that all the characters in PIN are
> digits.

I wouldn't assume it, I'd check it.  Or you could write it
work for strings of any length.

> 		letterWheel : string := "UROVALTINE";

You should use standard recommended style conventions.
It makes it easier for other Ada programmers to read your code.
"Letter_Wheel : String ...".

Declare constants "constant".  If you're using GNAT, use the options
that will warn you when you forget to do that.  -gnatwa is a good
idea.

> 		password : string := PIN;

No need to copy PIN.  Just refer to it in your loop.

> 		counter : integer := 1;

> 		number : Character := '0';
> 		AdaIsHard : integer :=0 ;

Above two variable initializations are overwritten below.
I'd leave them out.

> begin
>
> while counter <= 4 loop

Indent!  (Maybe you did, and some evil software garbled
it on the way here.)

This could be a 'for' loop.

> number:= password(counter);
>
> AdaIsHard := (Character'Pos(number)- 47);

Don't use magic numbers like that.  Either comment what 47
means or use a constant (or both).  Calculate it using an
expression involving Character'Pos('0').

But there's actually no need to convert Number into an integer
at all, eliminating the need for AdaIsHard.

> password(counter):= letterWheel(AdaIsHard);
>
> counter := counter + 1;
>
>  end loop;
>
>       return password;
>
>    end Encrypt;

Here's my solution:

with Text_IO; use Text_IO;
procedure Hw4 is

   subtype String4 is String(1..4);

   function Encrypt(PIN: String4) return String4 is
      -- Convert the 4-digit PIN to the corresponding 4-letter code.

      subtype Digit is Character range '0' .. '9';
      Letter_Wheel: constant array(Digit) of Character
        := "UROVALTINE";
      -- Here, we're mapping characters (in a certain range) to
      -- other characters. So we use an array indexed by characters
      -- of characters. This is why there's no need for an intermediate
      -- conversion to Integer (the Character'Pos thing in your code).
      -- String is a wrong data type for Letter_Wheel, because the index
      -- type is wrong, and because the lower bound is 1, not '0'.

      Result: String4;
   begin
      for X in PIN'Range loop
         Result(X) := Letter_Wheel(PIN(X));
      end loop;
      return Result;
   end Encrypt;

begin
   Put_Line("Encrypt(""6789"") = " & Encrypt("6789"));
   Put_Line("Encrypt(""5432"") = " & Encrypt("5432"));
   Put_Line("Encrypt(""0926"") = " & Encrypt("0926"));
   Put_Line("Encrypt(""7359"") = " & Encrypt("7359"));
end Hw4;

- Bob



  parent reply	other threads:[~2012-02-12 22:26 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-02-09  1:03 Need Help On Ada95 Problem Will
2012-02-09  1:51 ` Robert A Duff
2012-02-09  2:01 ` Shark8
2012-02-10  1:36   ` BrianG
2012-02-10  2:22     ` Shark8
2012-02-10  5:32       ` Alex
2012-02-10 15:19         ` Shark8
2012-02-10 15:45           ` Alex
2012-02-10 20:07           ` Robert A Duff
2012-02-12 19:40             ` Will
2012-02-12 19:42               ` Will
2012-02-12 22:26               ` Robert A Duff [this message]
2012-02-13  0:41                 ` Will
2012-02-13  0:43                 ` Nasser M. Abbasi
2012-02-10  5:45       ` Alex
2012-02-10 13:29         ` Robert A Duff
2012-02-10 13:50           ` Alex
2012-02-10 14:30             ` Robert A Duff
2012-02-10 15:32               ` Alex
2012-02-10 20:03                 ` Robert A Duff
2012-02-13  3:28       ` BrianG
2012-02-10  8:47   ` Simon Wright
2012-02-09  3:38 ` Alex
     [not found]   ` <90a10801-440b-4a31-88d0-f0f7c17137f1@eb6g2000vbb.googlegroups.com>
2012-02-09 13:35     ` Alex
replies disabled

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