Reputation: 404
This is the code and the algorithm works in Java but in Ada it still raises the CONSTRAINT_ERROR. In Ada we start indexing the array at 1 and not at 0 as in most other languages. For some reason I am still indexing out of array.
procedure spiral is
M : Matrix := ((11,22,33,44,55),(1,8,3,8,9),(10,10,20,30,1));
lastcol, firstcol, lastrow : Integer := 1;
rowsize, colsize : Integer := 0;
procedure Print ( M: in Matrix ) is
begin
rowsize := M'Length(1);
colsize := M'Length(2);
while lastrow<=rowsize loop
for I in Index range Index(firstcol)..Index(colsize) loop
Put( Elem'Image(M(Index(lastrow),Index(I))));
Put( Ascii.HT );
end loop;
lastrow := lastrow + 1;
if (lastrow>=rowsize) then
return;
end if;
for J in Index range Index(lastrow)..Index(rowsize) loop
Put( Elem'Image(M(Index(J),Index(colsize))));
Put( Ascii.HT );
end loop;
colsize := colsize - 1;
for I in reverse Index range Index(colsize)..Index(lastcol) loop
Put( Elem'Image(M(Index(rowsize), Index(I))));
Put( Ascii.HT );
end loop;
rowsize := rowsize- 1;
lastcol := lastcol+ 1;
for I in reverse Index range Index(rowsize)..Index(lastrow) loop
Put( Elem'Image(M(Index(I), Index(firstcol))));
Put( Ascii.HT );
end loop;
firstcol := firstcol + 1;
end loop;
end Print;
begin
--Put(rowDown);
Print(M);
end spiral;
the Matrix package is defined as:
package Matrix_pack is
type Index is new Integer;
type Elem is new Integer;
type Matrix is array (Index range <>, Index range <>) of Elem;
end Matrix_pack;
Upvotes: 0
Views: 377
Reputation: 6601
The solution can be done in two steps:
for I in Index range colLeft .. colRight loop
Put (Elem'Image (M (rowUp, I)));
end loop;
colLeft
and colRight
:colLeft, colRight : Index;
Upvotes: 2