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=-1.9 required=5.0 tests=BAYES_00,WEIRD_QUOTING autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,7ba49aac4e73460 X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Received: by 10.68.74.201 with SMTP id w9mr8206791pbv.0.1329085599991; Sun, 12 Feb 2012 14:26:39 -0800 (PST) Path: wr5ni17632pbc.0!nntp.google.com!news2.google.com!newsfeed2.dallas1.level3.net!news.level3.com!newsfeed-00.mathworks.com!nntp.TheWorld.com!not-for-mail From: Robert A Duff Newsgroups: comp.lang.ada Subject: Re: Need Help On Ada95 Problem Date: Sun, 12 Feb 2012 17:26:39 -0500 Organization: The World Public Access UNIX, Brookline, MA Message-ID: References: <553ceec3-ec34-41de-9723-0dc342379cfe@vv9g2000pbc.googlegroups.com> <4ea6309f-cf07-44f6-8c56-5189a0081dcc@g27g2000yqa.googlegroups.com> <43ebdc77-d90a-4213-8e74-db92ad373e14@m7g2000vbw.googlegroups.com> NNTP-Posting-Host: shell01.theworld.com Mime-Version: 1.0 X-Trace: pcls6.std.com 1329085599 1175 192.74.137.71 (12 Feb 2012 22:26:39 GMT) X-Complaints-To: abuse@TheWorld.com NNTP-Posting-Date: Sun, 12 Feb 2012 22:26:39 +0000 (UTC) User-Agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/21.3 (irix) Cancel-Lock: sha1:PWrIRiOuJbCqMcq7Y1NBsCT+4h0= Content-Type: text/plain; charset=us-ascii Date: 2012-02-12T17:26:39-05:00 List-Id: Will 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