From: Ludovic Brenta <ludovic@ludovic-brenta.org>
Subject: Re: creating an array
Date: Wed, 15 Feb 2006 22:53:46 +0100
Date: 2006-02-15T22:53:46+01:00 [thread overview]
Message-ID: <87u0b070gl.fsf@ludovic-brenta.org> (raw)
In-Reply-To: 1139960269.338824.166090@g14g2000cwa.googlegroups.com
"isaac2004" <isaac_2004@yahoo.com> writes:
>>But I still think my solution is much, much better :)
> sorry havent learned that far yet, but i cant seem to get your code
> to work by itself, it throws a error about a loop where the first if
> statement is. oh well it wasnt tested but i would like ot see a
> running program of that if possible.
OK, it took me another minute or two. Here is the corrected version,
followed by the diff so you can see what I changed.
(in retrospect, "if X not in 1 .. 10" reads better than "if not X in 1
.. 10", don't you think?
--
Ludovic Brenta.
-- Caveat student: not compiled or tested; just wrote this in 10 minutes.
-- Caveat student: your teacher probably reads c.l.a, too :)
with Ada.Integer_Text_IO;
with Ada.Text_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.Integer_Text_IO.Get (Expected_Number_Of_Digits);
Ada.Text_IO.Skip_Line;
if Expected_Number_Of_Digits not in 1 .. 10 then
raise Bad_Input;
end if;
Ada.Text_IO.Put ("Now type your number consisting of " &
Integer'Image (Expected_Number_Of_Digits) &
" 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 User_Input (J) not in Digit_Type then
raise Bad_Input;
end if;
Occurrences (User_Input (J)) := Occurrences (User_Input (J)) + 1;
end loop;
-- 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;
--- /tmp/ediff10085Ca 2006-02-15 22:51:09.000000000 +0100
+++ /tmp/sort_and_display.adb 2006-02-15 22:51:09.000000000 +0100
@@ -1,6 +1,7 @@
-- 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;
+with Ada.Integer_Text_IO;
+with Ada.Text_IO;
procedure Sort_And_Display is
subtype Digit_Type is Character range '0' .. '9';
type Occurrences_Type is array (Digit_Type) of Natural;
@@ -11,13 +12,13 @@
Bad_Input : exception;
begin
Ada.Text_IO.Put ("How many digits (1 .. 10)? ");
- Ada.Text_IO.Integer_IO.Get (Expected_Number_Of_Digits);
+ Ada.Integer_Text_IO.Get (Expected_Number_Of_Digits);
Ada.Text_IO.Skip_Line;
- if not Expected_Number_Of_Digits in 1 .. 10 then
+ if Expected_Number_Of_Digits not 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) &
+ Integer'Image (Expected_Number_Of_Digits) &
" digits: ");
Ada.Text_IO.Get_Line (User_Input, Last);
if Last > Expected_Number_Of_Digits then
@@ -26,11 +27,11 @@
-- Parse the character string
for J in 1 .. Last loop
- if not User_Input (J) in Digit_Type then
+ if User_Input (J) not in Digit_Type then
raise Bad_Input;
end if;
Occurrences (User_Input (J)) := Occurrences (User_Input (J)) + 1;
- end if;
+ end loop;
-- Now, Occurrences is naturally sorted; just walk over it.
for K in Occurrences'Range loop
next prev parent reply other threads:[~2006-02-15 21:53 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
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 [this message]
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