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 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,3f1374bc66d2dc03 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news1.google.com!news4.google.com!news.glorb.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local01.nntp.dca.giganews.com!nntp.scarlet.biz!news.scarlet.biz.POSTED!not-for-mail NNTP-Posting-Date: Tue, 14 Feb 2006 16:46:17 -0600 From: Ludovic Brenta Newsgroups: comp.lang.ada Subject: Re: creating an array References: <1139897171.996297.230070@z14g2000cwz.googlegroups.com> <1139951664.264622.240870@z14g2000cwz.googlegroups.com> <1139955455.397334.310790@g43g2000cwa.googlegroups.com> Date: Tue, 14 Feb 2006 23:45:17 +0100 Message-ID: <878xsd8sqq.fsf@ludovic-brenta.org> User-Agent: Gnus/5.110004 (No Gnus v0.4) Emacs/21.4 (gnu/linux) Cancel-Lock: sha1:Fc6J09VyyGQqv/nO0R/1vfnZRzI= MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii NNTP-Posting-Host: 83.134.238.224 X-Trace: sv3-f703ecjV3e9afZkhDja+q0ZHFOcvpi4TaFXDt40B7CI2ZZ5cqm+49bltuEnTwf00PydAIPpVcwUiY1d!UgtMW9mWd5jEJkYOMgKY/7QqvE6EPySonlSS7WWWK1kcIIhOWW1iYHHmJMapAmsBICQjUvRqKwQ= X-Complaints-To: abuse@scarlet.be X-DMCA-Complaints-To: abuse@scarlet.biz X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.32 Xref: g2news1.google.com comp.lang.ada:2896 Date: 2006-02-14T23:45:17+01:00 List-Id: "jimmaureenrogers@worldnet.att.net" writes: > isaac2004 wrote: >> ok to everybody trying to help me thank you i didnt specify enough. i >> have a program that takes two inputs from prompts, the first number is >> an integer less than 10. the second number has the amount of digits >> that the previous number specifies. what i want to do is split the >> number up into single digits, sort them and out put the min ascending >> order. >> > > You might think through your requirements one more time. > What happens when the first parameter does not match the number of > digits in the second parameter? If this condition is never supposed to > occur then why enter the first parameter? Simply calculate the number > of digits from the number entered and sort its digits. > > Your split subprogram is a lot of work. You can achieve the same > results by converting the number to a string, which is an array of > characters. Sort that array and output the results. The 'Image > attribute will convert the number to its corresponding string > representation. Better yet: don't convert at all; Ada.Text_IO.Get_Line will give you the user's input as a raw string. There is no requirement to convert that string to a number anywhere. There is however an implicit requirement to check that all characters in that string are digits, and that there are exactly N1 of them. If I understand the problem correctly, what you want is: Inputs: 4 and 1977 Output: 1779 Is that correct? You can do the sorting by creating an array indicating how many times each digit occurs in the user's input, like so: -- Caveat student: not compiled or tested; just wrote this in 10 minutes. -- Caveat student: your teacher probably reads c.l.a, too :) with Ada.Text_IO.Integer_IO; procedure Sort_And_Display is subtype Digit_Type is Character range '0' .. '9'; type Occurrences_Type is array (Digit_Type) of Natural; Expected_Number_Of_Digits : Positive; User_Input : String (1 .. 10); Last : Natural; Occurrences : Occurrences_Type := (others => 0); Bad_Input : exception; begin Ada.Text_IO.Put ("How many digits (1 .. 10)? "); Ada.Text_IO.Integer_IO.Get (Expected_Number_Of_Digits); Ada.Text_IO.Skip_Line; if not Expected_Number_Of_Digits in 1 .. 10 then raise Bad_Input; end if; Ada.Text_IO.Put ("Now type your number consisting of " & Integer'Image (Expected_Number_Of_Digit) & " digits: "); Ada.Text_IO.Get_Line (User_Input, Last); if Last > Expected_Number_Of_Digits then raise Bad_Input; end if; -- Parse the character string for J in 1 .. Last loop if not User_Input (J) in Digit_Type then raise Bad_Input; end if; Occurrences (User_Input (J)) := Occurrences (User_Input (J)) + 1; end if; -- Now, Occurrences is naturally sorted; just walk over it. for K in Occurrences'Range loop for L in 1 .. Occurrences (K) loop Ada.Text_IO.Put (K); end loop; end loop; end Sort_And_Display; -- Ludovic Brenta.