Re: Question from newbie

From: Adrian Hoe (AdrianHoe_at_nowhere.com)
Date: 02/18/04


Date: Wed, 18 Feb 2004 18:21:22 +0800

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


Relevant Pages

  • Re: count occurences of font color
    ... Dim num As Long, res As Long ... Debug.Print Timer - s ... ' using isnull and not using characters ... as the loop will exit on the first character. ...
    (microsoft.public.excel.programming)
  • Re: count occurences of font color
    ... Dim num As Long, res As Long ... Debug.Print Timer - s ... ' using isnull and not using characters ... as the loop will exit on the first character. ...
    (microsoft.public.excel.programming)
  • Re: for loop problem
    ... The for loop repeats 5 times BUT only accept 3 ... Three characters insufficient; ... She says `There is nobody ... staying with us,' meaning nobody of the sort you mean." ...
    (comp.lang.c)
  • REVIEWS: The X-Axis - 18 June 2006
    ... And Cable, who has been largely unbothered by anything other characters have said about him up till now, is finally rattled by the thought that even Domino doesn't trust him in this role. ... I wondered whether there was some sort of mind control element involved here, but it really does seem as though we're expected to accept that Cable has the sort of supernatural leadership charisma that this would require. ... The thread of the Exiles chasing Proteus has really just been a device to revisit a load of old settings and wallow in the past. ... The nostalgia has never truly complemented the main plot, and the result is an overlong storyline which certainly has good moments in there, but falls flat as a whole. ...
    (rec.arts.comics.marvel.xbooks)
  • Recently Read
    ... characters to recognize those immortals is infuriating, ... I hope that this ends up being some sort of magically induced ... with it long enough for the book does to develop a plot, ... The novel is entertaining enough, ...
    (rec.arts.sf.written)

Loading