From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=0.1 required=5.0 tests=AC_FROM_MANY_DOTS,BAYES_00, INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,c0581b94f07c034a X-Google-Attributes: gid103376,public From: Jeff Carter Subject: Re: Constraint Error - Please Help Date: 1998/12/03 Message-ID: <3666BCB7.B7BA6373@spam.innocon.com>#1/1 X-Deja-AN: 418282635 Content-Transfer-Encoding: 7bit References: <7445iv$gog$1@news.nyu.edu> Content-Type: text/plain; charset=us-ascii Organization: Innovative Concepts, Inc. Mime-Version: 1.0 Newsgroups: comp.lang.ada Date: 1998-12-03T00:00:00+00:00 List-Id: Fanni Kolchina wrote: > > Hi, All: > > I am writing a generic sorting program in Ada using heap sort. I am > new to Ada, and have no experience with this. When I run my program, I > get a Constraint Error: range error. I have spent lots of time > thinking what is wrong, but I just cannot comprehend it. > If someone could help me, I will greatly appreciate it. Below is my > code, and the line where the exception is thrown is marked by comment > lines above and below it. Thanks a lot! > > with ada.text_io; use ada.text_io; > with ada.integer_text_io; use ada.integer_text_io; > with ada.float_text_io; use ada.float_text_io; > > procedure test_sort is > > generic > type ITEM is private; > type SORT_ARRAY is array(Positive range <>) of ITEM; > with function "<" (u,v: ITEM) return boolean is <>; > > function sort_generic (x: SORT_ARRAY) return SORT_ARRAY; > > subtype index is positive range 1..10; > type floatArray is array (positive range <>) of float; > fa, hfa, shfa: floatArray(index); > > procedure printFloatArray (fa: floatArray) is > begin > for count in fa'first..fa'last loop This is a good place to use 'range: for Count in Fa'range loop > put(fa(count), fore=>3, aft=>1, exp =>0); > end loop; > new_line; > end; > > function sort_generic (x: SORT_ARRAY) return SORT_ARRAY is > copy: SORT_ARRAY(x'range); This is a good place to initialize Copy: Copy : Sort_Array := X; > last, left, right, max_l_r, current, this, parent: integer; > end_node, contents : ITEM; > begin > > for i in x'first..x'last loop > copy(i):=x(i); > end loop; If you initialized Copy in its declaration, as I suggested above, then this loop may be deleted. > > for i in x'first+1..x'last loop > this:=i; > parent:= (i-1)/2; > contents:=copy(this); > ---------------------------------------------------------------- > -- here's the line with the exception --- > while this /= x'first and then copy(parent) ---------------------------------------------------------------- > ... We see later in your code that this is called with X'First = 1. So the first time through this loop, I = 2, which means Parent = (2 - 1) / 2 = 1 / 2 = 0 (integer division truncates towards zero). So the condition of the while loop is 2 /= 1 and then Copy (0) < Contents Obviously, 2 is never equal to 1, so the comparison of Copy (0) to Contents is performed. Zero is not a valid index for Copy, and a run-time check detects this constraint violation and raises Constraint_Error. -- Jeff Carter E-mail: carter commercial-at innocon [period | full stop] com "Perfidious English mouse-dropping hoarders." Monty Python & the Holy Grail