comp.lang.ada
 help / color / mirror / Atom feed
* Question from newbie
@ 2004-02-16  2:43 Cecilia Chew
  2004-02-16 10:49 ` Luke Guest
                   ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: Cecilia Chew @ 2004-02-16  2:43 UTC (permalink / raw)


Hi all,
I'm still in learning Ada. However, there were something missing in 
coding. Could someone point out my problems. Just take an example, i'm 
now doing a program that ask user input x characters and program will 
sort the user's input into ascending order. The is below:
========================================================================
with Ada.Text_Io, Ada.Integer_Text_Io;
use Ada.Text_Io, Ada.Integer_Text_io;

procedure Sort is
    subtype Index is Integer range 1 .. 10;
    subtype Char is Character range 'a' .. 'z';
    type Str is array (Index) of Char;
    procedure Ascending (Left : in out Character; Right : in out
       Character);

    procedure Ascending (Left : in out Character; Right : in out
       Character) is
       Left : Character;
       Right : Character;
       Temp : Character;
    begin
       if Left > Right then
	 Temp := Left;
	 Left := Right;
	 Right := Temp;
       end if;
    end Ascending;

    Num : Index;
    Input : Str;
		
begin
    Put ("Please enter the number of character : ");
    Get (Num);
    New_Line;
    if Num not in Index then
       Put ("Please enter 1 to 10 characters only!");
    else
       Put ("Please enter" & Integer'Image(Num) & " characters : ");
       for num in Index loop
          Get (Input (Num));
          if Input'Length = num then

             for I in 1 .. num loop
	       Ascending_char (Input(i), Input(i+1));
             end loop;
	
	    while not End_Of_Line loop
	       Put (Input(Num));
	    end loop;
	
	else
	    exit;
	end if;
	
       end loop;
end if;
end Sort;
=============================================================================
Question:
1.  As the procedure ascending compiles as a independent unit with the
     other file name, it could sort the two characters correctly.
     However, as I put it together with sort.adb program. Specification
     part that declared the procedure ascending conflict with the
     variable left and right. Where is the mistake?

2.  Before plug in the procedure ascending, the program successfully
     being compiled. But the program can't allocate the specified space
     as x characters in outputting the result. For an example, user
     specified 2 characters, but the program still allow user to input
     less than or excess 2 characters..Where is the mistakes?

3.  From my coding, what should I pay more attention to?

Please kindly give me any comments..

Regards,
--
Cecilia

-- 
Cecilia Chew
c e c i l i a <AT> g r e e n l i m e . c o m



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

* Re: Question from newbie
  2004-02-16  2:43 Question from newbie Cecilia Chew
@ 2004-02-16 10:49 ` Luke Guest
  2004-02-16 10:53 ` Preben Randhol
  2004-02-16 11:20 ` Jacob Sparre Andersen
  2 siblings, 0 replies; 11+ messages in thread
From: Luke Guest @ 2004-02-16 10:49 UTC (permalink / raw)


This looks like coursework to me. You should really look a bit harder.

Luke.

>
============================================================================
=
> Question:
> 1.  As the procedure ascending compiles as a independent unit with the
>      other file name, it could sort the two characters correctly.
>      However, as I put it together with sort.adb program. Specification
>      part that declared the procedure ascending conflict with the
>      variable left and right. Where is the mistake?

I'll give you a hint, parameter and local variable names.

> 2.  Before plug in the procedure ascending, the program successfully
>      being compiled. But the program can't allocate the specified space
>      as x characters in outputting the result. For an example, user
>      specified 2 characters, but the program still allow user to input
>      less than or excess 2 characters..Where is the mistakes?

Check your for loop when inputting the characters.






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

* Re: Question from newbie
  2004-02-16  2:43 Question from newbie Cecilia Chew
  2004-02-16 10:49 ` Luke Guest
@ 2004-02-16 10:53 ` Preben Randhol
  2004-02-16 15:46   ` Jacob Sparre Andersen
  2004-02-18  9:52   ` Cecilia Chew
  2004-02-16 11:20 ` Jacob Sparre Andersen
  2 siblings, 2 replies; 11+ messages in thread
From: Preben Randhol @ 2004-02-16 10:53 UTC (permalink / raw)


On 2004-02-16, Cecilia Chew <cecilia@nowhere.com> wrote:
> Hi all,
> I'm still in learning Ada. However, there were something missing in 
> coding. Could someone point out my problems. Just take an example, i'm 
> now doing a program that ask user input x characters and program will 
> sort the user's input into ascending order. The is below:

Before helping two questions:

1. Is this homework?
2. Why do you do this: type Str is array (Index) of Char; 




-- 
"Saving keystrokes is the job of the text editor, not the programming
 language."



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

* Re: Question from newbie
  2004-02-16  2:43 Question from newbie Cecilia Chew
  2004-02-16 10:49 ` Luke Guest
  2004-02-16 10:53 ` Preben Randhol
@ 2004-02-16 11:20 ` Jacob Sparre Andersen
  2 siblings, 0 replies; 11+ messages in thread
From: Jacob Sparre Andersen @ 2004-02-16 11:20 UTC (permalink / raw)


Cecilia Chew worte:

> with Ada.Text_Io, Ada.Integer_Text_Io;
> use Ada.Text_Io, Ada.Integer_Text_io;
> 
> procedure Sort is
>     subtype Index is Integer range 1 .. 10;
>     subtype Char is Character range 'a' .. 'z';
>     type Str is array (Index) of Char;
>     procedure Ascending (Left : in out Character; Right : in out
>        Character);
> 
>     procedure Ascending (Left : in out Character; Right : in out
>        Character) is

>        Left : Character;
>        Right : Character;

These two identifiers are already used for the parameters passed to
the procedure.  And I don't think you want those variables anyway.

>        Temp : Character;
>     begin
>        if Left > Right then
> 	 Temp := Left;
> 	 Left := Right;
> 	 Right := Temp;
>        end if;
>     end Ascending;
> 
>     Num : Index;
>     Input : Str;
> 		
> begin
>     Put ("Please enter the number of character : ");

>     Get (Num);

I think you might want to insert a call to Ada.Text_IO.Skip_Line here.
Just to eat the newline entered after the number.

>     New_Line;
>     if Num not in Index then
>        Put ("Please enter 1 to 10 characters only!");
>     else
>        Put ("Please enter" & Integer'Image(Num) & " characters : ");

>        for num in Index loop

It is a little bit dangerous to use the name of an existing variable
for the counter in a for loop.  I think you actually wanted to write:

         for Character_Number in Index'First .. Num loop;

>           Get (Input (Num));

And this line was probably intended (as it did) to use the counter
from the for loop:

            Get (Input (Character_Number));

>           if Input'Length = num then

Input has a fixed length (10), so this will only be true, when you
have entered exactly 10 characters.

> 
>              for I in 1 .. num loop
> 	       Ascending_char (Input(i), Input(i+1));
>              end loop;
> 	
> 	    while not End_Of_Line loop
> 	       Put (Input(Num));
> 	    end loop;
> 	
> 	else
> 	    exit;
> 	end if;
> 	
>        end loop;
> end if;
> end Sort;
> =============================================================================
> Question:
> 1.  As the procedure ascending compiles as a independent unit with the
>      other file name, it could sort the two characters correctly.
>      However, as I put it together with sort.adb program. Specification
>      part that declared the procedure ascending conflict with the
>      variable left and right. Where is the mistake?

The mistake is that you have parameters and variables of the same
type.

> 2.  Before plug in the procedure ascending, the program successfully
>      being compiled. But the program can't allocate the specified space
>      as x characters in outputting the result. For an example, user
>      specified 2 characters, but the program still allow user to input
>      less than or excess 2 characters..Where is the mistakes?

The mistake is that you fix the length of the string to 10 characters
at compile time.

> 3.  From my coding, what should I pay more attention to?

You should be careful about using name overloading (until you are a
bit more sure about what you are doing).

You should look a bit more at how arrays are allocated and used in Ada
(your use of 'Length seems like you haven't understood it properly).

Greetings,

Jacob
-- 
"I've got _plenty_ of common sense!"
"I just choose to ignore it."



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

* Re: Question from newbie
  2004-02-16 10:53 ` Preben Randhol
@ 2004-02-16 15:46   ` Jacob Sparre Andersen
  2004-02-16 16:14     ` Preben Randhol
  2004-02-18  9:52   ` Cecilia Chew
  1 sibling, 1 reply; 11+ messages in thread
From: Jacob Sparre Andersen @ 2004-02-16 15:46 UTC (permalink / raw)


Preben Randhol wrote:

> 2. Why do you do this: type Str is array (Index) of Char;

Isn't that very useful, if you only want the letters allowed by the
type Char?

Jacob
-- 
�When in Rome; burn it�                        -- Diziet Sma



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

* Re: Question from newbie
  2004-02-16 15:46   ` Jacob Sparre Andersen
@ 2004-02-16 16:14     ` Preben Randhol
  0 siblings, 0 replies; 11+ messages in thread
From: Preben Randhol @ 2004-02-16 16:14 UTC (permalink / raw)


On 2004-02-16, Jacob Sparre Andersen <sparre@nbi.dk> wrote:
> Preben Randhol wrote:
>
>> 2. Why do you do this: type Str is array (Index) of Char;
>
> Isn't that very useful, if you only want the letters allowed by the
> type Char?

Of course it depends what you really try to do, but to me it sound very
Cish. I would have used a string.


-- 
"Saving keystrokes is the job of the text editor, not the programming
 language."



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

* Re: Question from newbie
  2004-02-16 10:53 ` Preben Randhol
  2004-02-16 15:46   ` Jacob Sparre Andersen
@ 2004-02-18  9:52   ` Cecilia Chew
  2004-02-18 10:19     ` Jacob Sparre Andersen
  2004-02-18 10:21     ` Adrian Hoe
  1 sibling, 2 replies; 11+ messages in thread
From: Cecilia Chew @ 2004-02-18  9:52 UTC (permalink / raw)


Hi all,
I'm an Ada learner and under a supervisor. This is one of my task during 
  learning period. From group comments that had provided, I did some 
amendments but the program still have errors. I wonder what to do.
Below is after amendments program :

===========================================================================
with Ada.Text_Io, Ada.Integer_Text_Io;
use Ada.Text_Io, Ada.Integer_Text_io;

procedure Sort is
    subtype Index is Integer range 1 .. 10;
    subtype Char is Character range 'a' .. 'z';
    type Str is array (Index) of Char;
    procedure Ascending (Left : in out Character; Right : in out 
Character);

    procedure Ascending (Left : in out Character; Right : in out 
Character) is

       Temp : Character;
    begin
       if Left > Right then
          Temp := Left;
          Left := Right;
          Right := Temp;
       end if;
    end Ascending;

    Num : Index;
    Input : Str;

begin
    Put ("Please enter the number of character : ");
    Get (Num);
    Ada.Text_Io.Skip_Line;
    New_Line;
    if Num not in Index then
       Put ("Please enter 1 to 10 characters only!");
    else
       Put ("Please enter" & Integer'Image(Num) & " characters : ");
       for Character_number in Index'First .. Num loop
	 Get (Input(Character_number));
	 for Character_Number in 1 .. num loop
	    Ascending (Left => Input(Character_Number),
	       Right => Input(Character_Number + 1));
	    Input(Num) := Input (Character_Number);
	 end loop;
	  Put (Input (Num));
       end loop;
     end if;
end Sort;
===============================================================================
This program could get user input characters but not the specified x 
character and any range of characters will occurred error as below.

    raised CONSTRAINT_ERROR : sort.adb:36 range check failed

where is the problem?

By the way, thanks the comments from Luke Guest, Jacob Sparre Andersen 
and Preben Randhol.



Preben Randhol wrote:
> On 2004-02-16, Cecilia Chew <cecilia@nowhere.com> wrote:
> 
>>Hi all,
>>I'm still in learning Ada. However, there were something missing in 
>>coding. Could someone point out my problems. Just take an example, i'm 
>>now doing a program that ask user input x characters and program will 
>>sort the user's input into ascending order. The is below:
> 
> 
> Before helping two questions:
> 
> 1. Is this homework?
> 2. Why do you do this: type Str is array (Index) of Char; 
> 
> 
> 
> 


-- 
Cecilia Chew
c e c i l i a <AT> g r e e n l i m e . c o m



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

* Re: Question from newbie
  2004-02-18  9:52   ` Cecilia Chew
@ 2004-02-18 10:19     ` Jacob Sparre Andersen
  2004-02-18 10:24       ` Adrian Hoe
  2004-02-18 10:21     ` Adrian Hoe
  1 sibling, 1 reply; 11+ messages in thread
From: Jacob Sparre Andersen @ 2004-02-18 10:19 UTC (permalink / raw)


Cecilia Chew wrote:

> with Ada.Text_Io, Ada.Integer_Text_Io;
> use Ada.Text_Io, Ada.Integer_Text_io;
> 
> procedure Sort is
>     subtype Index is Integer range 1 .. 10;
>     subtype Char is Character range 'a' .. 'z';
>     type Str is array (Index) of Char;
>     procedure Ascending (Left : in out Character; Right : in out
> Character);
> 
>     procedure Ascending (Left : in out Character; Right : in out
> Character) is
> 
>        Temp : Character;
>     begin
>        if Left > Right then
>           Temp := Left;
>           Left := Right;
>           Right := Temp;
>        end if;
>     end Ascending;
> 
>     Num : Index;
>     Input : Str;
> 
> begin
>     Put ("Please enter the number of character : ");
>     Get (Num);
>     Ada.Text_Io.Skip_Line;
>     New_Line;
>     if Num not in Index then

This check should not be necessary.  Since "Num" is of the subtype
"Index", it _can_ only have values that are in "Index".  You should on
the other hand get a "Constraint_Error", if you try to enter a number
that is not in the range of "Index".

>        Put ("Please enter 1 to 10 characters only!");
>     else
>        Put ("Please enter" & Integer'Image(Num) & " characters : ");
>        for Character_number in Index'First .. Num loop
> 	 Get (Input(Character_number));

[ here's line 36 - I think ]

Did you actually enter "Num" characters in the range 'a' .. 'z'?

A "Constraint_Error" indicates that something is out of range.  It
can't be "Character_Number", since both "Index'First" and "Num" are
valid values of the subtype "Index" (and "Character_Number" thus
always will be a valid index for the array "Input").

The alternative is that the character you entered wasn't in the valid
range for "Input (Character_Number)", i.e. in the range 'a' .. 'z'.

> This program could get user input characters but not the specified x
> character and any range of characters will occurred error as below.
> 
>     raised CONSTRAINT_ERROR : sort.adb:36 range check failed
> 
> where is the problem?

Jacob
-- 
�When Roman engineers built a bridge, they had to stand under it while
 the first legion marched across. If programmers today worked under
 similar ground rules, they might well find themselves getting much more
 interested in Ada!�                                     -- Robert Dewar



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

* Re: Question from newbie
  2004-02-18  9:52   ` Cecilia Chew
  2004-02-18 10:19     ` Jacob Sparre Andersen
@ 2004-02-18 10:21     ` Adrian Hoe
  1 sibling, 0 replies; 11+ messages in thread
From: Adrian Hoe @ 2004-02-18 10:21 UTC (permalink / raw)


Cecilia Chew wrote:
> Hi all,
> I'm an Ada learner and under a supervisor. This is one of my task during 
>  learning period. From group comments that had provided, I did some 
> amendments but the program still have errors. I wonder what to do.
> Below is after amendments program :
> 
> ===========================================================================
> with Ada.Text_Io, Ada.Integer_Text_Io;
> use Ada.Text_Io, Ada.Integer_Text_io;
> 
> procedure Sort is
>    subtype Index is Integer range 1 .. 10;
>    subtype Char is Character range 'a' .. 'z';
>    type Str is array (Index) of Char;
>    procedure Ascending (Left : in out Character; Right : in out Character);
> 
>    procedure Ascending (Left : in out Character; Right : in out 
> Character) is
> 
>       Temp : Character;
>    begin
>       if Left > Right then
>          Temp := Left;
>          Left := Right;
>          Right := Temp;
>       end if;
>    end Ascending;
> 
>    Num : Index;
>    Input : Str;
> 
> begin
>    Put ("Please enter the number of character : ");
>    Get (Num);
>    Ada.Text_Io.Skip_Line;
>    New_Line;
>    if Num not in Index then
>       Put ("Please enter 1 to 10 characters only!");
>    else
>       Put ("Please enter" & Integer'Image(Num) & " characters : ");
>       for Character_number in Index'First .. Num loop
>      Get (Input(Character_number));
>      for Character_Number in 1 .. num loop
>         Ascending (Left => Input(Character_Number),
>            Right => Input(Character_Number + 1));
>         Input(Num) := Input (Character_Number);
>      end loop;
>       Put (Input (Num));
>       end loop;
>     end if;
> end Sort;
> =============================================================================== 
> 
> This program could get user input characters but not the specified x 
> character and any range of characters will occurred error as below.
> 
>    raised CONSTRAINT_ERROR : sort.adb:36 range check failed
> 
> where is the problem?


The problem is "simple". Perhaps you did not look hard enough. You are 
trying to access an array out of bound. I suggest you go through you 
code again carefully.

More to this, your program flow has many flaws/errors.

1.) Based on your design, your program does not do what I expected. Why 
do you limit the number of characters to 10?

2.) There is a serious flaw in your loop. I suggest you to to go back to 
your design and flow charts and redesign your for...loop.

3.) Your entire sorting algorithm is wrong as well as the way you pass 
your parameter(s) to your sort procedure.
-- 
Adrian Hoe
m a i l b o x AT a d r i a n h o e . c o m




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

* Re: Question from newbie
  2004-02-18 10:19     ` Jacob Sparre Andersen
@ 2004-02-18 10:24       ` Adrian Hoe
  2004-02-18 10:24         ` Adrian Hoe
  0 siblings, 1 reply; 11+ messages in thread
From: Adrian Hoe @ 2004-02-18 10:24 UTC (permalink / raw)


Jacob Sparre Andersen wrote:

> Cecilia Chew wrote:
> 
> 
>>with Ada.Text_Io, Ada.Integer_Text_Io;
>>use Ada.Text_Io, Ada.Integer_Text_io;
>>
>>procedure Sort is
>>    subtype Index is Integer range 1 .. 10;
>>    subtype Char is Character range 'a' .. 'z';
>>    type Str is array (Index) of Char;
>>    procedure Ascending (Left : in out Character; Right : in out
>>Character);
>>
>>    procedure Ascending (Left : in out Character; Right : in out
>>Character) is
>>
>>       Temp : Character;
>>    begin
>>       if Left > Right then
>>          Temp := Left;
>>          Left := Right;
>>          Right := Temp;
>>       end if;
>>    end Ascending;
>>
>>    Num : Index;
>>    Input : Str;
>>
>>begin
>>    Put ("Please enter the number of character : ");
>>    Get (Num);
>>    Ada.Text_Io.Skip_Line;
>>    New_Line;
>>    if Num not in Index then
> 
> 
> This check should not be necessary.  Since "Num" is of the subtype
> "Index", it _can_ only have values that are in "Index".  You should on
> the other hand get a "Constraint_Error", if you try to enter a number
> that is not in the range of "Index".
> 
> 
>>       Put ("Please enter 1 to 10 characters only!");
>>    else
>>       Put ("Please enter" & Integer'Image(Num) & " characters : ");
>>       for Character_number in Index'First .. Num loop
>>	 Get (Input(Character_number));
> 
> 
> [ here's line 36 - I think ]
> 
> Did you actually enter "Num" characters in the range 'a' .. 'z'?
> 
> A "Constraint_Error" indicates that something is out of range.  It
> can't be "Character_Number", since both "Index'First" and "Num" are
> valid values of the subtype "Index" (and "Character_Number" thus
> always will be a valid index for the array "Input").
> 
> The alternative is that the character you entered wasn't in the valid
> range for "Input (Character_Number)", i.e. in the range 'a' .. 'z'.

No. That is fine. Look into line 36 carefully and you shall find the 
mistake. :)
-- 
Adrian Hoe
m a i l b o x AT a d r i a n h o e . c o m




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

* Re: Question from newbie
  2004-02-18 10:24       ` Adrian Hoe
@ 2004-02-18 10:24         ` Adrian Hoe
  0 siblings, 0 replies; 11+ messages in thread
From: Adrian Hoe @ 2004-02-18 10:24 UTC (permalink / raw)


Adrian Hoe wrote:

> Jacob Sparre Andersen wrote:
> 
>> Cecilia Chew wrote:
>>
>>>       Put ("Please enter 1 to 10 characters only!");
>>>    else
>>>       Put ("Please enter" & Integer'Image(Num) & " characters : ");
>>>       for Character_number in Index'First .. Num loop
>>>      Get (Input(Character_number));
>>
>>
>>
>> [ here's line 36 - I think ]
>>
>> Did you actually enter "Num" characters in the range 'a' .. 'z'?
>>
>> A "Constraint_Error" indicates that something is out of range.  It
>> can't be "Character_Number", since both "Index'First" and "Num" are
>> valid values of the subtype "Index" (and "Character_Number" thus
>> always will be a valid index for the array "Input").
>>
>> The alternative is that the character you entered wasn't in the valid
>> range for "Input (Character_Number)", i.e. in the range 'a' .. 'z'.
> 
> 
> No. That is fine. Look into line 36 carefully and you shall find the 
> mistake. :)

This is line 36:

 >         Ascending (Left => Input(Character_Number),
 >            Right => Input(Character_Number + 1));


-- 
Adrian Hoe
m a i l b o x AT a d r i a n h o e . c o m




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

end of thread, other threads:[~2004-02-18 10:24 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-02-16  2:43 Question from newbie Cecilia Chew
2004-02-16 10:49 ` Luke Guest
2004-02-16 10:53 ` Preben Randhol
2004-02-16 15:46   ` Jacob Sparre Andersen
2004-02-16 16:14     ` Preben Randhol
2004-02-18  9:52   ` Cecilia Chew
2004-02-18 10:19     ` Jacob Sparre Andersen
2004-02-18 10:24       ` Adrian Hoe
2004-02-18 10:24         ` Adrian Hoe
2004-02-18 10:21     ` Adrian Hoe
2004-02-16 11:20 ` Jacob Sparre Andersen

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