comp.lang.ada
 help / color / mirror / Atom feed
* Need Help On Ada95 Problem
@ 2012-02-09  1:03 Will
  2012-02-09  1:51 ` Robert A Duff
                   ` (2 more replies)
  0 siblings, 3 replies; 24+ messages in thread
From: Will @ 2012-02-09  1:03 UTC (permalink / raw)


I am new to Ada and need help on an exercise for school.  Can anyone
help.  The problem is as follows ( you can skip all the build up
background A Christmas Story references):

Problem 1: Secret Decoder Ring

You may remember Ralphie's disappointment when he received his Little
Orphan Annie Secret Decoder Ring.
What you may not realize is that, after Ralphie threw away his ring,
his brother Randy dug it out of the trash. Randy grew up to run the IT
department of his small hometown bank, and was responsible for storing
the 4-digit Personal Identification Numbers (PINs) chosen by his
bank's customers. He knew better than to store these PINs in the
clear, so he encrypted them using Ralphie's ring.

The decoder ring had two wheels, one printed with letters and one
printed with numbers. In the video clip, you see Ralphie decoding the
numbers read on the radio, looking up each number and copying down the
corresponding letter. Randy uses the ring in a similar way, but
instead of disguising letters as numbers, he does the reverse,
disguising numbers as letters. To encrypt a PIN, he finds each digit
on the wheel, and turns it into a letter. (The ring contains some two-
digit numbers as well, but he ignores those, using only the single-
digit numbers.)

Here is a small portion of the decoder ring:

     number wheel ->    0  1  2  3  4  5  6  7  8  9
     letter wheel ->    U  R  O  V  A  L  T  I  N  E

So, for example, Randy encrypts the PIN 9537 as ELVI.

Complete the function Encrypt(PIN) that takes a 4-digit PIN (as a
string) and returns the corresponding 4-letter code (also as a
string).

Note: It would be possible to convert digits to letters using a giant
IF statement, but don't do this. Instead, the letter wheel is given to
you as a string, so use each digit to read the appropriate letter from
the string.



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

* Re: Need Help On Ada95 Problem
  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-09  3:38 ` Alex
  2 siblings, 0 replies; 24+ messages in thread
From: Robert A Duff @ 2012-02-09  1:51 UTC (permalink / raw)


Will <willmann817@gmail.com> writes:

> I am new to Ada and need help on an exercise for school.

I'm curious, what school?

- Bob



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

* Re: Need Help On Ada95 Problem
  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  8:47   ` Simon Wright
  2012-02-09  3:38 ` Alex
  2 siblings, 2 replies; 24+ messages in thread
From: Shark8 @ 2012-02-09  2:01 UTC (permalink / raw)


On Feb 8, 7:03 pm, Will <willmann...@gmail.com> wrote:
> I am new to Ada and need help on an exercise for school.  Can anyone
> help.  The problem is as follows ( you can skip all the build up
> background A Christmas Story references):
>
> Problem 1: Secret Decoder Ring
>
> You may remember Ralphie's disappointment when he received his Little
> Orphan Annie Secret Decoder Ring.
> What you may not realize is that, after Ralphie threw away his ring,
> his brother Randy dug it out of the trash. Randy grew up to run the IT
> department of his small hometown bank, and was responsible for storing
> the 4-digit Personal Identification Numbers (PINs) chosen by his
> bank's customers. He knew better than to store these PINs in the
> clear, so he encrypted them using Ralphie's ring.
>
> The decoder ring had two wheels, one printed with letters and one
> printed with numbers. In the video clip, you see Ralphie decoding the
> numbers read on the radio, looking up each number and copying down the
> corresponding letter. Randy uses the ring in a similar way, but
> instead of disguising letters as numbers, he does the reverse,
> disguising numbers as letters. To encrypt a PIN, he finds each digit
> on the wheel, and turns it into a letter. (The ring contains some two-
> digit numbers as well, but he ignores those, using only the single-
> digit numbers.)
>
> Here is a small portion of the decoder ring:
>
>      number wheel ->    0  1  2  3  4  5  6  7  8  9
>      letter wheel ->    U  R  O  V  A  L  T  I  N  E
>
> So, for example, Randy encrypts the PIN 9537 as ELVI.
>
> Complete the function Encrypt(PIN) that takes a 4-digit PIN (as a
> string) and returns the corresponding 4-letter code (also as a
> string).
>
> Note: It would be possible to convert digits to letters using a giant
> IF statement, but don't do this. Instead, the letter wheel is given to
> you as a string, so use each digit to read the appropriate letter from
> the string.

Alright this is actually a simple problem; there are several ways to
go about this,
but let's play to Ada's strengths and use types.

So start by describing things in terms of types.

      Type Cypher_Character is Private;
      Type Pin_Character is Private;

      Type Pin_String is Array(1..4) of Pin_Character;

--- Later in the private section.
      Type Cypher_Character is new Character;
      Type Pin_Character is new Character;

In this mindset things become crystal clear: this is merely type-
conversion.

So you'll need a conversion function, something like:

  Function Convert_Pin_Character( Item : In Pin_Character ) Return
Cypher_Character;

with an implementation of:
  Function Convert_Pin_Character( Item : In Pin_Character ) Return
Cypher_Character is
  begin
   Return Result : Cypher_Character do
      ----- ACTUAL CONVERSION GOES HERE
   end return;
  end;

Reading the problem the note says:
It would be possible to convert digits to letters using a giant
IF statement, but don't do this. Instead, the letter wheel is given to
you as a string, so use each digit to read the appropriate letter from
the string.

Meaning that they're hinting strongly that you use the CASE statement.



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

* Re: Need Help On Ada95 Problem
  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-09  3:38 ` Alex
       [not found]   ` <90a10801-440b-4a31-88d0-f0f7c17137f1@eb6g2000vbb.googlegroups.com>
  2 siblings, 1 reply; 24+ messages in thread
From: Alex @ 2012-02-09  3:38 UTC (permalink / raw)


Will,

My first tip to you is to remember that Ada strings are simply arrays
of characters, each character of which can be individually accessed by
a number representing its location in the string. You might look at
pages 430 and 431 of Feldman, if that's your text book.

My second tip is to remember to document all help received in
accordance with DWW.

Good luck!

Will wrote:

> I am new to Ada and need help on an exercise for school.  Can anyone
> help.  The problem is as follows ( you can skip all the build up
> background A Christmas Story references):
> 
> Problem 1: Secret Decoder Ring
> 
> You may remember Ralphie's disappointment when he received his Little
> Orphan Annie Secret Decoder Ring.
> What you may not realize is that, after Ralphie threw away his ring,
> his brother Randy dug it out of the trash. Randy grew up to run the IT
> department of his small hometown bank, and was responsible for storing
> the 4-digit Personal Identification Numbers (PINs) chosen by his
> bank's customers. He knew better than to store these PINs in the
> clear, so he encrypted them using Ralphie's ring.
> 
> The decoder ring had two wheels, one printed with letters and one
> printed with numbers. In the video clip, you see Ralphie decoding the
> numbers read on the radio, looking up each number and copying down the
> corresponding letter. Randy uses the ring in a similar way, but
> instead of disguising letters as numbers, he does the reverse,
> disguising numbers as letters. To encrypt a PIN, he finds each digit
> on the wheel, and turns it into a letter. (The ring contains some two-
> digit numbers as well, but he ignores those, using only the single-
> digit numbers.)
> 
> Here is a small portion of the decoder ring:
> 
>      number wheel ->    0  1  2  3  4  5  6  7  8  9
>      letter wheel ->    U  R  O  V  A  L  T  I  N  E
> 
> So, for example, Randy encrypts the PIN 9537 as ELVI.
> 
> Complete the function Encrypt(PIN) that takes a 4-digit PIN (as a
> string) and returns the corresponding 4-letter code (also as a
> string).
> 
> Note: It would be possible to convert digits to letters using a giant
> IF statement, but don't do this. Instead, the letter wheel is given to
> you as a string, so use each digit to read the appropriate letter from
> the string.



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

* Re: Need Help On Ada95 Problem
       [not found]   ` <90a10801-440b-4a31-88d0-f0f7c17137f1@eb6g2000vbb.googlegroups.com>
@ 2012-02-09 13:35     ` Alex
  0 siblings, 0 replies; 24+ messages in thread
From: Alex @ 2012-02-09 13:35 UTC (permalink / raw)



Will wrote:

> I think you mean DAW assuming you and I go to the same school.  

Yes, that's what I meant, but we don't go to the same school, we just
know the same people.

> I did end up
> receiving help from a classmate of mine who was able to explain it to
> me much better and I will cite him.  

Great! I hope your classmate didn't just give you the answer, though.
You'll become a better programmer if you learn how to solve these
problems with hints rather than being given the full solution outright.
It's the whole "give a man a fish/teach a man to fish" thing.

> One last thing...using my super
> h4X0r skills does ur last name happen to end in "banks"?

No, it doesn't.



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

* Re: Need Help On Ada95 Problem
  2012-02-09  2:01 ` Shark8
@ 2012-02-10  1:36   ` BrianG
  2012-02-10  2:22     ` Shark8
  2012-02-10  8:47   ` Simon Wright
  1 sibling, 1 reply; 24+ messages in thread
From: BrianG @ 2012-02-10  1:36 UTC (permalink / raw)


On 02/08/2012 09:01 PM, Shark8 wrote:
> On Feb 8, 7:03 pm, Will<willmann...@gmail.com>  wrote:
>> I am new to Ada and need help on an exercise for school.  Can anyone
>> help.  The problem is as follows ( you can skip all the build up
>> background A Christmas Story references):
>>
>> Problem 1: Secret Decoder Ring
...
>>       number wheel ->      0  1  2  3  4  5  6  7  8  9
>>       letter wheel ->      U  R  O  V  A  L  T  I  N  E
>>
>> So, for example, Randy encrypts the PIN 9537 as ELVI.
>>
>> Complete the function Encrypt(PIN) that takes a 4-digit PIN (as a
>> string) and returns the corresponding 4-letter code (also as a
>> string).
>>
>> Note: It would be possible to convert digits to letters using a giant
>> IF statement, but don't do this. Instead, the letter wheel is given to
>> you as a string, so use each digit to read the appropriate letter from
>> the string.
>
> Alright this is actually a simple problem; there are several ways to
> go about this,
> but let's play to Ada's strengths and use types.
>
...
> Reading the problem the note says:
> It would be possible to convert digits to letters using a giant
> IF statement, but don't do this. Instead, the letter wheel is given to
> you as a string, so use each digit to read the appropriate letter from
> the string.
>
> Meaning that they're hinting strongly that you use the CASE statement.

I don't see why this would require either an if or a case.

-- 
---
BrianG
000
@[Google's email domain]
.com



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

* Re: Need Help On Ada95 Problem
  2012-02-10  1:36   ` BrianG
@ 2012-02-10  2:22     ` Shark8
  2012-02-10  5:32       ` Alex
                         ` (2 more replies)
  0 siblings, 3 replies; 24+ messages in thread
From: Shark8 @ 2012-02-10  2:22 UTC (permalink / raw)


On Feb 9, 7:36 pm, BrianG <m...@null.email> wrote:
> On 02/08/2012 09:01 PM, Shark8 wrote:
>
>
>
>
>
>
>
>
>
> > On Feb 8, 7:03 pm, Will<willmann...@gmail.com>  wrote:
> >> I am new to Ada and need help on an exercise for school.  Can anyone
> >> help.  The problem is as follows ( you can skip all the build up
> >> background A Christmas Story references):
>
> >> Problem 1: Secret Decoder Ring
> ...
> >>       number wheel ->      0  1  2  3  4  5  6  7  8  9
> >>       letter wheel ->      U  R  O  V  A  L  T  I  N  E
>
> >> So, for example, Randy encrypts the PIN 9537 as ELVI.
>
> >> Complete the function Encrypt(PIN) that takes a 4-digit PIN (as a
> >> string) and returns the corresponding 4-letter code (also as a
> >> string).
>
> >> Note: It would be possible to convert digits to letters using a giant
> >> IF statement, but don't do this. Instead, the letter wheel is given to
> >> you as a string, so use each digit to read the appropriate letter from
> >> the string.
>
> > Alright this is actually a simple problem; there are several ways to
> > go about this,
> > but let's play to Ada's strengths and use types.
>
> ...
> > Reading the problem the note says:
> > It would be possible to convert digits to letters using a giant
> > IF statement, but don't do this. Instead, the letter wheel is given to
> > you as a string, so use each digit to read the appropriate letter from
> > the string.
>
> > Meaning that they're hinting strongly that you use the CASE statement.
>
> I don't see why this would require either an if or a case.
>
> --
> ---
> BrianG
> 000
> @[Google's email domain]
> .com

Well, you COULD use Ada's Maps; but that's just going to do the same
thing: you still have to define the mapping.
You can't just index into Character (via 'POS, 'VAL and index-
manipulation) because the mapping isn't sequential;
or even partly-sequential in the ASCII codes.

See the (3,4,5) association with (V,A,L) and (6,7,8) with (T,I,N).



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

* Re: Need Help On Ada95 Problem
  2012-02-10  2:22     ` Shark8
@ 2012-02-10  5:32       ` Alex
  2012-02-10 15:19         ` Shark8
  2012-02-10  5:45       ` Alex
  2012-02-13  3:28       ` BrianG
  2 siblings, 1 reply; 24+ messages in thread
From: Alex @ 2012-02-10  5:32 UTC (permalink / raw)


Sure you can just use 'Pos! While the positions of the digit characters
do not correspond directly to their values, they are sequential in the
ASCII mapping, so all you have to do is subtract the position of the
Character '0' from each one to calculate its value. Check it out:

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;

procedure Main is

begin

   for I in Character'('0') .. Character'('9') loop
      Put(Character'Pos(I) - Character'Pos('0'));
      New_Line;
   end loop;

end Main;



Shark8 wrote:

> On Feb 9, 7:36�pm, BrianG <m...@null.email> wrote:
> > On 02/08/2012 09:01 PM, Shark8 wrote:
> > 
> > 
> > 
> > 
> > 
> > 
> > 
> > 
> > 
> > > On Feb 8, 7:03 pm, Will<willmann...@gmail.com> �wrote:
> > >> I am new to Ada and need help on an exercise for school. �Can
> > anyone >> help. �The problem is as follows ( you can skip all the
> > build up >> background A Christmas Story references):
> > 
> > >> Problem 1: Secret Decoder Ring
> > ...
> > >> � � � number wheel -> � � �0 �1 �2 �3 �4 �5 �6 �7 �8 �9
> > >> � � � letter wheel -> � � �U �R �O �V �A �L �T �I �N �E
> > 
> > >> So, for example, Randy encrypts the PIN 9537 as ELVI.
> > 
> > >> Complete the function Encrypt(PIN) that takes a 4-digit PIN (as a
> > >> string) and returns the corresponding 4-letter code (also as a
> > >> string).
> > 
> > >> Note: It would be possible to convert digits to letters using a
> > giant >> IF statement, but don't do this. Instead, the letter wheel
> > is given to >> you as a string, so use each digit to read the
> > appropriate letter from >> the string.
> > 
> > > Alright this is actually a simple problem; there are several ways
> > > to go about this,
> > > but let's play to Ada's strengths and use types.
> > 
> > ...
> > > Reading the problem the note says:
> > > It would be possible to convert digits to letters using a giant
> > > IF statement, but don't do this. Instead, the letter wheel is
> > > given to you as a string, so use each digit to read the
> > > appropriate letter from the string.
> > 
> > > Meaning that they're hinting strongly that you use the CASE
> > > statement.
> > 
> > I don't see why this would require either an if or a case.
> > 
> > --
> > ---
> > BrianG
> > 000
> > @[Google's email domain]
> > .com
> 
> Well, you COULD use Ada's Maps; but that's just going to do the same
> thing: you still have to define the mapping.
> You can't just index into Character (via 'POS, 'VAL and index-
> manipulation) because the mapping isn't sequential;
> or even partly-sequential in the ASCII codes.
> 
> See the (3,4,5) association with (V,A,L) and (6,7,8) with (T,I,N).



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

* Re: Need Help On Ada95 Problem
  2012-02-10  2:22     ` Shark8
  2012-02-10  5:32       ` Alex
@ 2012-02-10  5:45       ` Alex
  2012-02-10 13:29         ` Robert A Duff
  2012-02-13  3:28       ` BrianG
  2 siblings, 1 reply; 24+ messages in thread
From: Alex @ 2012-02-10  5:45 UTC (permalink / raw)


Oh, I see how you're confused. Once you get the value of each digit in
the PIN, though, it doesn't matter that the 3-4-5 corresponding to
V-A-L  isn't in ASCII order. . . it's still just a direct index-based
lookup (just off-by-one because Ada Strings are indexed starting at 1
rather than 0).

Shark8 wrote:

> On Feb 9, 7:36�pm, BrianG <m...@null.email> wrote:
> > On 02/08/2012 09:01 PM, Shark8 wrote:
> > 
> > 
> > 
> > 
> > 
> > 
> > 
> > 
> > 
> > > On Feb 8, 7:03 pm, Will<willmann...@gmail.com> �wrote:
> > >> I am new to Ada and need help on an exercise for school. �Can
> > anyone >> help. �The problem is as follows ( you can skip all the
> > build up >> background A Christmas Story references):
> > 
> > >> Problem 1: Secret Decoder Ring
> > ...
> > >> � � � number wheel -> � � �0 �1 �2 �3 �4 �5 �6 �7 �8 �9
> > >> � � � letter wheel -> � � �U �R �O �V �A �L �T �I �N �E
> > 
> > >> So, for example, Randy encrypts the PIN 9537 as ELVI.
> > 
> > >> Complete the function Encrypt(PIN) that takes a 4-digit PIN (as a
> > >> string) and returns the corresponding 4-letter code (also as a
> > >> string).
> > 
> > >> Note: It would be possible to convert digits to letters using a
> > giant >> IF statement, but don't do this. Instead, the letter wheel
> > is given to >> you as a string, so use each digit to read the
> > appropriate letter from >> the string.
> > 
> > > Alright this is actually a simple problem; there are several ways
> > > to go about this,
> > > but let's play to Ada's strengths and use types.
> > 
> > ...
> > > Reading the problem the note says:
> > > It would be possible to convert digits to letters using a giant
> > > IF statement, but don't do this. Instead, the letter wheel is
> > > given to you as a string, so use each digit to read the
> > > appropriate letter from the string.
> > 
> > > Meaning that they're hinting strongly that you use the CASE
> > > statement.
> > 
> > I don't see why this would require either an if or a case.
> > 
> > --
> > ---
> > BrianG
> > 000
> > @[Google's email domain]
> > .com
> 
> Well, you COULD use Ada's Maps; but that's just going to do the same
> thing: you still have to define the mapping.
> You can't just index into Character (via 'POS, 'VAL and index-
> manipulation) because the mapping isn't sequential;
> or even partly-sequential in the ASCII codes.
> 
> See the (3,4,5) association with (V,A,L) and (6,7,8) with (T,I,N).



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

* Re: Need Help On Ada95 Problem
  2012-02-09  2:01 ` Shark8
  2012-02-10  1:36   ` BrianG
@ 2012-02-10  8:47   ` Simon Wright
  1 sibling, 0 replies; 24+ messages in thread
From: Simon Wright @ 2012-02-10  8:47 UTC (permalink / raw)


Shark8 <onewingedshark@gmail.com> writes:

>> Complete the function Encrypt(PIN) that takes a 4-digit PIN (as a
>> string) and returns the corresponding 4-letter code (also as a
>> string).
>>
>> Note: It would be possible to convert digits to letters using a giant
>> IF statement, but don't do this. Instead, the letter wheel is given to
>> you as a string, so use each digit to read the appropriate letter from
>> the string.
>
> Alright this is actually a simple problem; there are several ways to
> go about this,
> but let's play to Ada's strengths and use types.
>
> So start by describing things in terms of types.
>
>       Type Cypher_Character is Private;
>       Type Pin_Character is Private;
>
>       Type Pin_String is Array(1..4) of Pin_Character;

Really, this is overkill.

> Reading the problem the note says:
> It would be possible to convert digits to letters using a giant
> IF statement, but don't do this. Instead, the letter wheel is given to
> you as a string, so use each digit to read the appropriate letter from
> the string.
>
> Meaning that they're hinting strongly that you use the CASE statement.

No, they want you to use the encoding as an array.

But I think it would be better to use a character mapping:

   The_Ring : constant Ada.Strings.Maps.Character_Mapping
     := Ada.Strings.Maps.To_Mapping ("0123456789", "UROVALTINE");

   function Encode (PIN : String) return String
   is
   begin
      return Ada.Strings.Fixed.Translate (PIN, The_Ring);
   end Encode;

OK, if you encode 9537s you get ELVIs ...

But we don't know what the professor feels about students who are too
clever for their own good and have clearly been reading ahead in the
textbook! (or asking for help on the net ...)



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

* Re: Need Help On Ada95 Problem
  2012-02-10  5:45       ` Alex
@ 2012-02-10 13:29         ` Robert A Duff
  2012-02-10 13:50           ` Alex
  0 siblings, 1 reply; 24+ messages in thread
From: Robert A Duff @ 2012-02-10 13:29 UTC (permalink / raw)


"Alex" <alex@foo.invalid> writes:

> Oh, I see how you're confused. Once you get the value of each digit in
> the PIN, though, it doesn't matter that the 3-4-5 corresponding to
> V-A-L  isn't in ASCII order. . . it's still just a direct index-based
> lookup (just off-by-one because Ada Strings are indexed starting at 1
> rather than 0).

There's no off-by-one issue here.  The OP wants a mapping from
digit characters to other characters.  That can be implemented
as a simple array, with the right index subtype.  That array
isn't a String, because they keys are not integers.

- Bob



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

* Re: Need Help On Ada95 Problem
  2012-02-10 13:29         ` Robert A Duff
@ 2012-02-10 13:50           ` Alex
  2012-02-10 14:30             ` Robert A Duff
  0 siblings, 1 reply; 24+ messages in thread
From: Alex @ 2012-02-10 13:50 UTC (permalink / raw)


This is a homework assignment and I think the translation lookup table
is provided as a string, not an array. As far as I know, there is no
way in Ada to make string indexing start at 0.

You're right, an array of chars indexed starting at 0 would be better,
but at this point of the course, I don't think they've covered arrays.
I think they use strings to introduce the concepts and then go into
arrays later.

Robert A Duff wrote:

> "Alex" <alex@foo.invalid> writes:
> 
> > Oh, I see how you're confused. Once you get the value of each digit
> > in the PIN, though, it doesn't matter that the 3-4-5 corresponding
> > to V-A-L  isn't in ASCII order. . . it's still just a direct
> > index-based lookup (just off-by-one because Ada Strings are indexed
> > starting at 1 rather than 0).
> 
> There's no off-by-one issue here.  The OP wants a mapping from
> digit characters to other characters.  That can be implemented
> as a simple array, with the right index subtype.  That array
> isn't a String, because they keys are not integers.
> 
> - Bob



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

* Re: Need Help On Ada95 Problem
  2012-02-10 13:50           ` Alex
@ 2012-02-10 14:30             ` Robert A Duff
  2012-02-10 15:32               ` Alex
  0 siblings, 1 reply; 24+ messages in thread
From: Robert A Duff @ 2012-02-10 14:30 UTC (permalink / raw)


"Alex" <alex@foo.invalid> writes:

> This is a homework assignment and I think the translation lookup table
> is provided as a string, not an array.

As I understood the problem statement, the input and output
are of type String.  Writing the translation table is the
homework assignment, and String makes no sense for that.
But I might have misunderstood.

>... As far as I know, there is no
> way in Ada to make string indexing start at 0.

Well, "X: String(0 .. -1000);" creates a String starting at zero,
but that's beside the point here.

> You're right, an array of chars indexed starting at 0 would be better,

That's not what I had in mind, but I'll leave it at that.

Maybe the OP would like to post his solution when it's all done.

> but at this point of the course, I don't think they've covered arrays.
> I think they use strings to introduce the concepts and then go into
> arrays later.

- Bob



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

* Re: Need Help On Ada95 Problem
  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
  0 siblings, 2 replies; 24+ messages in thread
From: Shark8 @ 2012-02-10 15:19 UTC (permalink / raw)


On Feb 9, 11:32 pm, "Alex" <a...@foo.invalid> wrote:
> Sure you can just use 'Pos! While the positions of the digit characters
> do not correspond directly to their values, they are sequential in the
> ASCII mapping, so all you have to do is subtract the position of the
> Character '0' from each one to calculate its value. Check it out:
>

I know that, I've done it before; I think you misunderstood what I was
trying to say here (or perhaps I was simply using the wrong language):

>
> Shark8 wrote:
> > You can't just index into Character (via 'POS, 'VAL and index-
> > manipulation) because the mapping isn't sequential;
> > or even partly-sequential in the ASCII codes.
>
> > See the (3,4,5) association with (V,A,L) and (6,7,8) with (T,I,N).

Do, let me rephrase:
Letting F(I) be the function taking an integer and returning the
correct character we see that F(3) = V, F(4) = A, and F(5) = L;
this means that the internals of F cannot be a simple offset indexing
into ASCII. Furthermore, as F(6) = T, F(7) = I, and F(8) = N we see
that there is no intuitively obvious subcycle such as one sees when
numbering a chessboard (the multiplication/mod relationship).

Therefore, using a mathematical (arithmetic) function to relate these
numbers and the associated ASCII codes is likely inappropriate.



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

* Re: Need Help On Ada95 Problem
  2012-02-10 14:30             ` Robert A Duff
@ 2012-02-10 15:32               ` Alex
  2012-02-10 20:03                 ` Robert A Duff
  0 siblings, 1 reply; 24+ messages in thread
From: Alex @ 2012-02-10 15:32 UTC (permalink / raw)



Robert A Duff wrote:

> Writing the translation table is the
> homework assignment, and String makes no sense for that.
> But I might have misunderstood.

You may be right. The OP's post said the assignment was to

"Complete the function Encrypt(PIN) that takes a 4-digit PIN (as a
string) and returns the corresponding 4-letter code (also as a
string)"

so I assumed the translation table was also a string, given as part of
the provided code and local to the function. If it's not, and the
student is supposed to provide the mapping, then there are definitely
better options than using a string.

I'm curious about your code sample:

X : String(0 .. -1000);

I tried it and it works, but I don't understand how it's possible.
String is defined in ARM 3.6.3 as

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

and your range is definitely outside of Positive. If I try 

X : String(0 .. 500); 

I get the expected compiler error. What's going on that allows the
range to be from 0 to a negative number?





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

* Re: Need Help On Ada95 Problem
  2012-02-10 15:19         ` Shark8
@ 2012-02-10 15:45           ` Alex
  2012-02-10 20:07           ` Robert A Duff
  1 sibling, 0 replies; 24+ messages in thread
From: Alex @ 2012-02-10 15:45 UTC (permalink / raw)




Shark8 wrote:

> I know that, I've done it before; I think you misunderstood what I was
> trying to say here (or perhaps I was simply using the wrong language):
> 

I think we're just operating on different assumptions. I thought the
translation lookup structure was provided to the student as a String as
part of the provided function that he was supposed to fill in. If it
is, then it's easy to look up a particular character in that string
based on its position.



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

* Re: Need Help On Ada95 Problem
  2012-02-10 15:32               ` Alex
@ 2012-02-10 20:03                 ` Robert A Duff
  0 siblings, 0 replies; 24+ messages in thread
From: Robert A Duff @ 2012-02-10 20:03 UTC (permalink / raw)


"Alex" <alex@foo.invalid> writes:

> I'm curious about your code sample:
>
> X : String(0 .. -1000);
>
> I tried it and it works, but I don't understand how it's possible.
> String is defined in ARM 3.6.3 as
>
> type String is array(Positive range <>) of Character;
>
> and your range is definitely outside of Positive. If I try 
>
> X : String(0 .. 500); 
>
> I get the expected compiler error.

Constraint_Error at run-time, I assume.
And maybe a compile-time warning (which you wouldn't
get if 500 were known only at run time).

>...What's going on that allows the
> range to be from 0 to a negative number?

There is a check that the bounds belong to the index subtype,
but that only applies if it's not a null range.  For String,
you certainly want to allow 1..0 for an empty string.

Allowing 0..0 or 0..-1000 for String is a language
design flaw, which has been discussed here many times.
For that matter, allowing 5..10 is a flaw.
This causes needless inefficiency and obscure bugs.

- Bob



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

* Re: Need Help On Ada95 Problem
  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
  1 sibling, 1 reply; 24+ messages in thread
From: Robert A Duff @ 2012-02-10 20:07 UTC (permalink / raw)


Shark8 <onewingedshark@gmail.com> writes:

> Letting F(I) be the function taking an integer and returning the
> correct character ...

Read the problem statement again.  The above-described F has
the wrong profile!  You're making it harder than it needs to be.

I'm trying to be vague here, to avoid ruining the fun for
the OP, who wants to figure it out for himself.  ;-)

- Bob



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

* Re: Need Help On Ada95 Problem
  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
  0 siblings, 2 replies; 24+ messages in thread
From: Will @ 2012-02-12 19:40 UTC (permalink / raw)


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:



with Ada.Text_IO; use Ada.Text_IO;

with Ada.Characters.Handling; use Ada.Characters.Handling;

with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;

with Ada.Strings.Fixed; use Ada.Strings.Fixed;



procedure hw4 is



   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.

      -- Example: Encrypt("9537") = "ELVI"



      -- FILL IN FOUR MORE TEST CASES.

		--Test Case 1: Encrypt("6789") = "TINE"

		--Test Case 2: Encrypt("5432") = "LAVO"

		--Test Case 3: Encrypt("0926") = "UEOT"

		--Test Case 4: Encrypt("7359") = "IVLE"

		letterWheel : string := "UROVALTINE";

		password : string := PIN;

		counter : integer := 1;

		number : Character := '0';

		AdaIsHard : integer :=0 ;

begin

while counter <= 4 loop

number:= password(counter);

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

password(counter):= letterWheel(AdaIsHard);

counter := counter + 1;

 end loop;

      return password;

   end Encrypt;



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

* Re: Need Help On Ada95 Problem
  2012-02-12 19:40             ` Will
@ 2012-02-12 19:42               ` Will
  2012-02-12 22:26               ` Robert A Duff
  1 sibling, 0 replies; 24+ messages in thread
From: Will @ 2012-02-12 19:42 UTC (permalink / raw)


Here are the test cases

put("Encrypt = ");

	put( Encrypt("6789") );

	new_line;



   put("Encrypt = ");

	put( Encrypt("5432") );

	new_line;



	put("Encrypt = ");

	put( Encrypt("0926") );

	new_line;



	put("Encrpyt = ");

	put( Encrypt("7359") );

	new_line;



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

* Re: Need Help On Ada95 Problem
  2012-02-12 19:40             ` Will
  2012-02-12 19:42               ` Will
@ 2012-02-12 22:26               ` Robert A Duff
  2012-02-13  0:41                 ` Will
  2012-02-13  0:43                 ` Nasser M. Abbasi
  1 sibling, 2 replies; 24+ messages in thread
From: Robert A Duff @ 2012-02-12 22:26 UTC (permalink / raw)


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



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

* Re: Need Help On Ada95 Problem
  2012-02-12 22:26               ` Robert A Duff
@ 2012-02-13  0:41                 ` Will
  2012-02-13  0:43                 ` Nasser M. Abbasi
  1 sibling, 0 replies; 24+ messages in thread
From: Will @ 2012-02-13  0:41 UTC (permalink / raw)


I did write the inverse decrypt as well.  I did 4 "for" loops and
nested them inside eachother going from 0..9 so it covered every
combination from 0000 to 9999.  It also worked.  I can send you the
code if you would like to view it.  I do like your code much better.
It practices good coding habits and it is much shorter.  In the near
future I believe our class will begin talking about types and
subtypes  and using things like constant array etc.

Thanks a bunch.

-Will



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

* Re: Need Help On Ada95 Problem
  2012-02-12 22:26               ` Robert A Duff
  2012-02-13  0:41                 ` Will
@ 2012-02-13  0:43                 ` Nasser M. Abbasi
  1 sibling, 0 replies; 24+ messages in thread
From: Nasser M. Abbasi @ 2012-02-13  0:43 UTC (permalink / raw)


On 2/12/2012 4:26 PM, Robert A Duff wrote:

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

</RANT>

This is a terrible choice of the Ada standard for variable names
convention.

Variable names should be all lower case, as in

        letter_wheel

easier to read, easier on the eye since it is uniform size, vs.

        Letter_Wheel

Many other languages uses the convention of lower case only
for variable names.

Sorry, do not mean to jump in like this, or change the topic,
but this is something I always disliked about Ada code. Makes
it ugly for me to read with variables using MiXed Upper_Case like
this.

But, since it is the convention, I agree with you that it
should be followed, even if one does not like it.

</END RANT>

regards,
--Nasser







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

* Re: Need Help On Ada95 Problem
  2012-02-10  2:22     ` Shark8
  2012-02-10  5:32       ` Alex
  2012-02-10  5:45       ` Alex
@ 2012-02-13  3:28       ` BrianG
  2 siblings, 0 replies; 24+ messages in thread
From: BrianG @ 2012-02-13  3:28 UTC (permalink / raw)


On 02/09/2012 09:22 PM, Shark8 wrote:
> On Feb 9, 7:36 pm, BrianG<m...@null.email>  wrote:
>> On 02/08/2012 09:01 PM, Shark8 wrote:
>>
...
>>
>>> Meaning that they're hinting strongly that you use the CASE statement.
>>
>> I don't see why this would require either an if or a case.
>>
>
> Well, you COULD use Ada's Maps; but that's just going to do the same
> thing: you still have to define the mapping.
> You can't just index into Character (via 'POS, 'VAL and index-
> manipulation) because the mapping isn't sequential;
> or even partly-sequential in the ASCII codes.
>
> See the (3,4,5) association with (V,A,L) and (6,7,8) with (T,I,N).

Nothing to do with any of that.  It's the same function I use to Get/Put 
hexadecimal.  Definitely a case where you don't need, or want, to use 
predefined types.

(Basically what Robert presented.)

-- 
---
BrianG
000
@[Google's email domain]
.com



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

end of thread, other threads:[~2012-02-13  3:28 UTC | newest]

Thread overview: 24+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
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

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