comp.lang.ada
 help / color / mirror / Atom feed
* creating an array
@ 2006-02-14  6:06 isaac2004
  2006-02-14 13:59 ` jimmaureenrogers
  2006-02-14 19:25 ` Björn Persson
  0 siblings, 2 replies; 23+ messages in thread
From: isaac2004 @ 2006-02-14  6:06 UTC (permalink / raw)


hello

how would i go about taking numbers generated by a function and storing
them into arrays in the purpose of having them on record




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

* Re: creating an array
  2006-02-14  6:06 creating an array isaac2004
@ 2006-02-14 13:59 ` jimmaureenrogers
  2006-02-14 15:20   ` isaac2004
  2006-02-14 19:25 ` Björn Persson
  1 sibling, 1 reply; 23+ messages in thread
From: jimmaureenrogers @ 2006-02-14 13:59 UTC (permalink / raw)


isaac2004 wrote:
> how would i go about taking numbers generated by a function and storing
> them into arrays in the purpose of having them on record

I am unclear about your goals.
Do you want to run a function and put successive results in array
elements?
Do you want to save that array to a file?

Jim Rogers




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

* Re: creating an array
  2006-02-14 13:59 ` jimmaureenrogers
@ 2006-02-14 15:20   ` isaac2004
  2006-02-14 18:44     ` jimmaureenrogers
  0 siblings, 1 reply; 23+ messages in thread
From: isaac2004 @ 2006-02-14 15:20 UTC (permalink / raw)


>I am unclear about your goals.
Do you want to run a function and put successive results in array
elements?
Do you want to save that array to a file?

that would be nice as well but i want to store them to a temp directory
but saving them to a file would be nice to know as well




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

* Re: creating an array
  2006-02-14 15:20   ` isaac2004
@ 2006-02-14 18:44     ` jimmaureenrogers
  0 siblings, 0 replies; 23+ messages in thread
From: jimmaureenrogers @ 2006-02-14 18:44 UTC (permalink / raw)



isaac2004 wrote:
> >I am unclear about your goals.
> Do you want to run a function and put successive results in array
> elements?
> Do you want to save that array to a file?
>
> that would be nice as well but i want to store them to a temp directory
> but saving them to a file would be nice to know as well

Do you want to save the data in a binary format, or as human-readable
text?

Jim Rogers




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

* Re: creating an array
  2006-02-14  6:06 creating an array isaac2004
  2006-02-14 13:59 ` jimmaureenrogers
@ 2006-02-14 19:25 ` Björn Persson
  2006-02-14 19:39   ` Dmitry A. Kazakov
  1 sibling, 1 reply; 23+ messages in thread
From: Björn Persson @ 2006-02-14 19:25 UTC (permalink / raw)


isaac2004 wrote:
> how would i go about taking numbers generated by a function and storing
> them into arrays in the purpose of having them on record

for Index in The_Array_Type'Range loop
    The_Array(Index) := The_Function;
end loop;

That's probably not what you want, but it does take numbers (or some 
other data) generated by a function and store them in an array.

-- 
Bj�rn Persson                              PGP key A88682FD
                    omb jor ers @sv ge.
                    r o.b n.p son eri nu



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

* Re: creating an array
  2006-02-14 19:25 ` Björn Persson
@ 2006-02-14 19:39   ` Dmitry A. Kazakov
  2006-02-14 21:14     ` isaac2004
  2006-02-15  7:42     ` Maciej Sobczak
  0 siblings, 2 replies; 23+ messages in thread
From: Dmitry A. Kazakov @ 2006-02-14 19:39 UTC (permalink / raw)


On Tue, 14 Feb 2006 19:25:05 GMT, Bj�rn Persson wrote:

> isaac2004 wrote:
>> how would i go about taking numbers generated by a function and storing
>> them into arrays in the purpose of having them on record
> 
> for Index in The_Array_Type'Range loop
>     The_Array(Index) := The_Function;
> end loop;

The_Array_Type := (others => The_Function);

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



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

* Re: creating an array
  2006-02-14 19:39   ` Dmitry A. Kazakov
@ 2006-02-14 21:14     ` isaac2004
  2006-02-14 22:17       ` jimmaureenrogers
  2006-02-15  7:42     ` Maciej Sobczak
  1 sibling, 1 reply; 23+ messages in thread
From: isaac2004 @ 2006-02-14 21:14 UTC (permalink / raw)


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.

here is my code i cant get the split function right

with Ada.Text_Io;
use Ada.Text_Io;
with Ada.Integer_Text_Io;
use Ada.Integer_Text_Io;
procedure test is

-----------------------------------------------------------------------------------------------
   --| Recieves two inputs, one being a number les than ten and the
other number is a number with the same amount of digits
   --| as the first recieved number. the second number is then
seperated, the digits stored in an array and put into ascending order
   --| Author: Isaac Levin, Western Washington University
   --| Last Modified: February 2006

----------------------------------------------------------------------------

   type Index is Natural range 1 .. 10
   type Number is Natural range 000000000 .. 999999999;
   type Positivearray is array (Index) of Positive;

   Num1      : Number;
   Num_Array : Positivearray;
   Prompt    : Index;
   Numdigit  : Number;

   procedure Split (
         N1 : in     Number;
         A  : in out Positivearray ) is
      Temp : Integer := 1;
      Remain    : Number;
   begin
      for I in 1..(Prompt - 1) loop
         Temp := Temp * 10;
      end loop;
      for I in Positivearray'range loop
         A(N1) := Temp rem 10;
         Remain := Remain rem Temp;
         Temp := Temp / 10;
         if Num1(N1) /= 0 then
            N1 := N1 + 1;
         end if;
      end loop;
   end Split;

   procedure Sort (
         Thearray : in out Positivearray;
         Inuse    : in     Natural        ) is
      -- Sorts the elements of the array from smallest to largest
      Min      : Positive;
      Indexmin : Positive;
      Temp     : Positive;
   begin

      for I in 1..Inuse - 1 loop
         Min := Thearray(I);
         Indexmin := I;
         for J in I + 1 .. Inuse loop
            if Thearray(J) < Min then
               Min := Thearray(J);
               Indexmin := J;
            end if;
         end loop;
         Temp := Thearray(I);
         Thearray(I) := Min;
         Thearray(Indexmin) := Temp;
      end loop;
   end Sort;

   -- end of ascending procedure



begin
   Put(Item => " Please enter a number with ");
   Put(Item => Prompt);
   Put(Item => " or fewer digits: ");
   New_Line;
   Get(Item => Num1);
   New_Line;

   Split (Num1, Prompt);
   Sort (Num1, Prompt);
   New_Line;
   Put ("The numbers in sorted order are");
   New_Line;
   for I in 1..Prompt loop
      Put (Num1(I), 0);
      New_Line;
   end loop;

end test;

any help would be greatly appreciated




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

* Re: creating an array
  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
  0 siblings, 2 replies; 23+ messages in thread
From: jimmaureenrogers @ 2006-02-14 22:17 UTC (permalink / raw)


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.

Jim Rogers




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

* Re: creating an array
  2006-02-14 22:17       ` jimmaureenrogers
@ 2006-02-14 22:30         ` isaac2004
  2006-02-14 22:45         ` Ludovic Brenta
  1 sibling, 0 replies; 23+ messages in thread
From: isaac2004 @ 2006-02-14 22:30 UTC (permalink / raw)


>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.

it is for a class i have and the ta told me to create a program that
did what i have already explained. he said it was fine just that i
needed some work with the split function, but the reason for first
prompt number is that it decides the length of the array so extra
numbers throw a exception handle that needs to be put in there




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

* Re: creating an array
  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
  1 sibling, 1 reply; 23+ messages in thread
From: Ludovic Brenta @ 2006-02-14 22:45 UTC (permalink / raw)


"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.



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

* Re: creating an array
  2006-02-14 22:45         ` Ludovic Brenta
@ 2006-02-14 22:54           ` isaac2004
  2006-02-14 23:10             ` Ludovic Brenta
  0 siblings, 1 reply; 23+ messages in thread
From: isaac2004 @ 2006-02-14 22:54 UTC (permalink / raw)


>If I understand the problem correctly, what you want is:

Inputs: 4 and 1977
Output: 1779


that is sort of correct but the code you just gave me just puts the
array into a string and parses it out when the operation should be done
on the array, i see that you can do it that way but that would make
throw away all of my old code. how can the old code be changed,
especially the procedure to split the file, so that i can preform these
actions on an array




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

* Re: creating an array
  2006-02-14 22:54           ` isaac2004
@ 2006-02-14 23:10             ` Ludovic Brenta
  2006-02-14 23:37               ` isaac2004
  0 siblings, 1 reply; 23+ messages in thread
From: Ludovic Brenta @ 2006-02-14 23:10 UTC (permalink / raw)


"isaac2004" <isaac_2004@yahoo.com> writes:

>>If I understand the problem correctly, what you want is:
>
> Inputs: 4 and 1977
> Output: 1779
>
>
> that is sort of correct but the code you just gave me just puts the
> array into a string and parses it out when the operation should be done
> on the array, i see that you can do it that way but that would make
> throw away all of my old code. how can the old code be changed,
> especially the procedure to split the file, so that i can preform these
> actions on an array

No, my program does not "put the array into a string".  It obtains
user input as a string (which is its natural form anyway), transforms
is into an array of Naturals, and then walks the array to display
sorted digits.  Your program takes the user input as a string (its
natural form), converts it to a number (by means of
Ada.Integer_Text_IO.Get), and then tries to massage it back into an
array.  This is not only convoluted, it is also inefficient.

If that's any help, you can convert a number to an array by working
backwards; units first, then tens, then hundreds etc.  This should get
you going:

type Index_Type is range 0 .. 9;
type Occurrences_Type is array (Index_Type) of Natural;

procedure Split (Number      : in     Natural;
                 Occurrences :    out Occurrences_Type) is
   Occurrences := (others => 0);
begin
   while Number > 0 loop
      Index := Index_Type (Number mod 10);
      Occurrences (Index) := Occurrences (Index) + 1;
      Number := Number / 10;
   end loop;
end Split;

But I still think my solution is much, much better :)

-- 
Ludovic Brenta.



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

* Re: creating an array
  2006-02-14 23:10             ` Ludovic Brenta
@ 2006-02-14 23:37               ` isaac2004
  2006-02-15  7:45                 ` Anders Wirzenius
  2006-02-15 21:53                 ` Ludovic Brenta
  0 siblings, 2 replies; 23+ messages in thread
From: isaac2004 @ 2006-02-14 23:37 UTC (permalink / raw)


>If that's any help, you can convert a number to an array by working
backwards; units first, then tens, then hundreds etc.  This should get
you going:


thanx jus thave to match varible names and such

>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.

happy coding !




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

* Re: creating an array
  2006-02-14 19:39   ` Dmitry A. Kazakov
  2006-02-14 21:14     ` isaac2004
@ 2006-02-15  7:42     ` Maciej Sobczak
  2006-02-15 10:37       ` Jean-Pierre Rosen
  2006-02-15 13:30       ` Dmitry A. Kazakov
  1 sibling, 2 replies; 23+ messages in thread
From: Maciej Sobczak @ 2006-02-15  7:42 UTC (permalink / raw)


Dmitry A. Kazakov wrote:

>>for Index in The_Array_Type'Range loop
>>    The_Array(Index) := The_Function;
>>end loop;
> 
> The_Array_Type := (others => The_Function);
            ^^^^^
Rather The_Array := ...

But what's more important - could you point to a specific paragraph in 
AARM that guarantees that the two above are equivalent? I mean - is it 
guaranteed that (others=>The_Function) has the meaning of increasing 
order of indexes? Is is possible for the implementation to call 
The_Function just once and reuse its return value for all elements' 
assignments?
5.2 seems to be a relevant chapter, but I haven't found anything that 
would apply here. 4.3.3 does not help either.


-- 
Maciej Sobczak : http://www.msobczak.com/
Programming    : http://www.msobczak.com/prog/



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

* Re: creating an array
  2006-02-14 23:37               ` isaac2004
@ 2006-02-15  7:45                 ` Anders Wirzenius
  2006-02-15 20:44                   ` Björn Persson
  2006-02-15 21:53                 ` Ludovic Brenta
  1 sibling, 1 reply; 23+ messages in thread
From: Anders Wirzenius @ 2006-02-15  7:45 UTC (permalink / raw)


"isaac2004" <isaac_2004@yahoo.com> writes:


> 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.
> 
> happy coding !
> 

Quick and dirty just to get it running:

Correct the spelling error: 
end if;  -> end loop;

you have to instantiate the Ada.Text_IO.Integer_IO: 
package Anders is new Ada.Text_IO.Integer_IO (Integer);

and then use your new package:
Anders.Get (Expected_Number_Of_Digits);

If the operator "not" is not defined, change the 
if statement to:
if ... then
   null;
else
   ...;
end if;

Good luck!
-- 
Anders



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

* Re: creating an array
  2006-02-15  7:42     ` Maciej Sobczak
@ 2006-02-15 10:37       ` Jean-Pierre Rosen
  2006-02-15 13:30       ` Dmitry A. Kazakov
  1 sibling, 0 replies; 23+ messages in thread
From: Jean-Pierre Rosen @ 2006-02-15 10:37 UTC (permalink / raw)


Maciej Sobczak a �crit :

> But what's more important - could you point to a specific paragraph in 
> AARM that guarantees that the two above are equivalent? I mean - is it 
> guaranteed that (others=>The_Function) has the meaning of increasing 
> order of indexes? Is is possible for the implementation to call 
> The_Function just once and reuse its return value for all elements' 
> assignments?
> 5.2 seems to be a relevant chapter, but I haven't found anything that 
> would apply here. 4.3.3 does not help either.
> 
> 
4.3.3(23): each component is evaluated once (it is thus not allowed to 
evaluate the function only once), but this happens in an arbitrary order 
(there is no guarantee that the function is called in increasing index 
order).

-- 
---------------------------------------------------------
            J-P. Rosen (rosen@adalog.fr)
Visit Adalog's web site at http://www.adalog.fr



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

* Re: creating an array
  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
  1 sibling, 1 reply; 23+ messages in thread
From: Dmitry A. Kazakov @ 2006-02-15 13:30 UTC (permalink / raw)


On Wed, 15 Feb 2006 08:42:09 +0100, Maciej Sobczak wrote:

> Dmitry A. Kazakov wrote:
> 
>>>for Index in The_Array_Type'Range loop
>>>    The_Array(Index) := The_Function;
>>>end loop;
>> 
>> The_Array_Type := (others => The_Function);
>             ^^^^^
> Rather The_Array := ...

Yes, of course.
 
> But what's more important - could you point to a specific paragraph in 
> AARM that guarantees that the two above are equivalent? I mean - is it 
> guaranteed that (others=>The_Function) has the meaning of increasing 
> order of indexes? Is is possible for the implementation to call 
> The_Function just once and reuse its return value for all elements' 
> assignments?

Jean-Pierre has answered this.

Functions shouldn't have side effects relevant to the call context. So the
result of The_Function should be independent on Index. It shouldn't read a
stream, but it can call to random generator, it can create a new object, it
can start a task etc.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



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

* Re: creating an array
  2006-02-15 13:30       ` Dmitry A. Kazakov
@ 2006-02-15 16:23         ` isaac2004
  0 siblings, 0 replies; 23+ messages in thread
From: isaac2004 @ 2006-02-15 16:23 UTC (permalink / raw)


>Quick and dirty just to get it running:

Correct the spelling error:
end if;  -> end loop;


you have to instantiate the Ada.Text_IO.Integer_IO:
package Anders is new Ada.Text_IO.Integer_IO (Integer);


and then use your new package:
Anders.Get (Expected_Number_Of_Digits);


If the operator "not" is not defined, change the
if statement to:
if ... then
   null;
else
   ...;
end if;


this whole program throws flags up everywhere in Adagide, undefined
varibles and misuse of packages. i dont know where to start on how to
fix it. thanks though




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

* Re: creating an array
  2006-02-15  7:45                 ` Anders Wirzenius
@ 2006-02-15 20:44                   ` Björn Persson
  2006-02-16  6:59                     ` Anders Wirzenius
  0 siblings, 1 reply; 23+ messages in thread
From: Björn Persson @ 2006-02-15 20:44 UTC (permalink / raw)


Anders Wirzenius wrote:
> you have to instantiate the Ada.Text_IO.Integer_IO: 
> package Anders is new Ada.Text_IO.Integer_IO (Integer);

Or use Ada.Integer_Text_IO.

> If the operator "not" is not defined, change the 
> if statement to:

Of course "not" is defined! Insert parentheses!

-- 
Bj�rn Persson                              PGP key A88682FD
                    omb jor ers @sv ge.
                    r o.b n.p son eri nu



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

* Re: creating an array
  2006-02-14 23:37               ` isaac2004
  2006-02-15  7:45                 ` Anders Wirzenius
@ 2006-02-15 21:53                 ` Ludovic Brenta
  2006-02-15 23:29                   ` isaac2004
  1 sibling, 1 reply; 23+ messages in thread
From: Ludovic Brenta @ 2006-02-15 21:53 UTC (permalink / raw)


"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




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

* Re: creating an array
  2006-02-15 21:53                 ` Ludovic Brenta
@ 2006-02-15 23:29                   ` isaac2004
  2006-02-16  3:09                     ` jimmaureenrogers
  0 siblings, 1 reply; 23+ messages in thread
From: isaac2004 @ 2006-02-15 23:29 UTC (permalink / raw)


hey finally got it to work thanks to everybody for helping.

here is code
with Ada.Text_Io;
use Ada.Text_Io;
with Ada.Integer_Text_Io;
use Ada.Integer_Text_Io;
procedure Lab6 is

-----------------------------------------------------------------------------------------------
   --| Recieves two inputs, one being a number les than ten and the
other number is a number with the same amount of digits
   --| as the first recieved number. the second number is then
seperated, the digits stored in an array and put into ascending order
   --| Author: Isaac Levin, Western Washington University
   --| Last Modified: February 2006

----------------------------------------------------------------------------
   subtype Index is Natural range 1 .. 10;
   subtype Number is Natural range 000000000 .. 999999999;


   type Positivearray is array (1 .. 10) of Natural;
   A         : Positivearray;
   Promptnum : Index;
   Fullnum   : Number;

   procedure Split (
         N          : in     Number;
         Num_Digits : in     Index;
         Dl         : in out Positivearray) is
      -- set the variables
      Temp      : Natural := 1;
      Remain    : Number  := N;
      Numdigits : Natural := 1;
      -- set this flag to TRUE initially to signify that we should
ignore the leading zeros
      Leadingzero : Boolean := True;
      -- Calculate the largest number to divide the input number by so
we can extract the left most digit.
      -- So if # of digits specified are: 3, then Temp would be: 100
   begin
      -- Loop to extract all the digits
      -- Note: Numdigit is used as the index variable for our array,
this helps us take out the leading zeros if the number entered
      -- has less digits than what the user specified
      for I in 1..Num_Digits loop
         -- Get the individual digit by remaindering


         A(I) := Remain mod 10;
         -- Store the remainder so in the next loop we can extract the
next left most digit
         Remain := Remain / 10;
      end loop;
   end Split;



   -- procedure to sort parts of array
   procedure Sort (
         Thearray : in out Positivearray;
         Inuse    : in     Natural) is
      -- Sorts the elements of the array from smallest to largest
      Min      : Positive;
      Indexmin : Positive;
      Temp     : Positive;
   begin
      for I in 1..Inuse - 1 loop
         Min := Thearray(I);
         Indexmin := I;
         for J in I + 1 .. Inuse loop
            if Thearray(J) < Min then
               Min := Thearray(J);
               Indexmin := J;
            end if;
         end loop;
         Temp := Thearray(I);
         Thearray(I) := Min;
         Thearray(Indexmin) := Temp;
      end loop;
   end Sort;


begin  -- actual interface

   Put ("Please enter the number of digits.");
   Get(Item => Promptnum);
   New_Line;
   Put(Item => "Please enter a number with ") ;
   Put (
      Item  => Promptnum,
      Width => 0) ;
   Put (Item => " digits.");

   Get(Item => Fullnum);


   Split(Fullnum, Promptnum, A);
    Sort (A, Promptnum); -- sort array
   New_Line;
   Put ("The numbers in sorted order are");
   New_Line;
   for I in 1..Promptnum loop
      Put (A(I), 0);
      New_Line;
   end loop;


end Lab6;


thank you 
Happy Coding!




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

* Re: creating an array
  2006-02-15 23:29                   ` isaac2004
@ 2006-02-16  3:09                     ` jimmaureenrogers
  0 siblings, 0 replies; 23+ messages in thread
From: jimmaureenrogers @ 2006-02-16  3:09 UTC (permalink / raw)


Now that you have a complete program, here is the solution I was
thinking of.

with Ada.Text_Io;

procedure Sort_Digits is
   procedure Sort_Characters (Buffer : in out String) is
      Exchanged : Boolean;
      Temp : Character;
   begin
      loop
         Exchanged := False;
         for I in Buffer'First..Buffer'Last - 1 loop
            if Buffer(I) > Buffer(I + 1) then
               Temp := Buffer(I + 1);
               Buffer(I + 1) := Buffer(I);
               Buffer(I) := Temp;
               Exchanged := True;
            end if;
         end loop;
         exit when not Exchanged;
      end loop;
   end Sort_Characters;
   Input_String : String(1..10);
   Length       : Natural;
begin
   Ada.Text_Io.Put_Line("Enter a number of no more than 10 digits");
   Ada.Text_Io.Get_Line(Item => Input_String, Last => Length);
   Sort_Characters(Input_String(1..Length));
   Ada.Text_Io.Put_Line(Input_String(1..Length));
end Sort_Digits;


Note that there is only one input, the number to be sorted.
The resulting output matches your program requirements.
There can never be a mis-match between stated and actual sizes
of the input string. There is no need to raise an exception due to
parameter inconsistencies.

Jim Rogers




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

* Re: creating an array
  2006-02-15 20:44                   ` Björn Persson
@ 2006-02-16  6:59                     ` Anders Wirzenius
  0 siblings, 0 replies; 23+ messages in thread
From: Anders Wirzenius @ 2006-02-16  6:59 UTC (permalink / raw)


Björn Persson <spam-away@nowhere.nil> writes:

> Anders Wirzenius wrote:
> > you have to instantiate the Ada.Text_IO.Integer_IO: package Anders
> > is new Ada.Text_IO.Integer_IO (Integer);
> 
> Or use Ada.Integer_Text_IO.
> 
> > If the operator "not" is not defined, change the if statement to:
> 
> Of course "not" is defined! Insert parentheses!
> 

A less dirty solution than my quick and dirty. :)

Not is defined for boolean type.

-- 
Anders



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

end of thread, other threads:[~2006-02-16  6:59 UTC | newest]

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

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