comp.lang.ada
 help / color / mirror / Atom feed
From: Ludovic Brenta <ludovic@ludovic-brenta.org>
Subject: Re: creating an array
Date: Tue, 14 Feb 2006 23:45:17 +0100
Date: 2006-02-14T23:45:17+01:00	[thread overview]
Message-ID: <878xsd8sqq.fsf@ludovic-brenta.org> (raw)
In-Reply-To: 1139955455.397334.310790@g43g2000cwa.googlegroups.com

"jimmaureenrogers@worldnet.att.net" <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.



  parent reply	other threads:[~2006-02-14 22:45 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-02-14  6:06 creating an array isaac2004
2006-02-14 13:59 ` jimmaureenrogers
2006-02-14 15:20   ` isaac2004
2006-02-14 18:44     ` jimmaureenrogers
2006-02-14 19:25 ` Björn Persson
2006-02-14 19:39   ` Dmitry A. Kazakov
2006-02-14 21:14     ` isaac2004
2006-02-14 22:17       ` jimmaureenrogers
2006-02-14 22:30         ` isaac2004
2006-02-14 22:45         ` Ludovic Brenta [this message]
2006-02-14 22:54           ` isaac2004
2006-02-14 23:10             ` Ludovic Brenta
2006-02-14 23:37               ` isaac2004
2006-02-15  7:45                 ` Anders Wirzenius
2006-02-15 20:44                   ` Björn Persson
2006-02-16  6:59                     ` Anders Wirzenius
2006-02-15 21:53                 ` Ludovic Brenta
2006-02-15 23:29                   ` isaac2004
2006-02-16  3:09                     ` jimmaureenrogers
2006-02-15  7:42     ` Maciej Sobczak
2006-02-15 10:37       ` Jean-Pierre Rosen
2006-02-15 13:30       ` Dmitry A. Kazakov
2006-02-15 16:23         ` isaac2004
replies disabled

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