2016-10-16 3 views
0

이것은 코드이며 알고리즘은 Java에서 작동하지만 Ada에서는 여전히 CONSTRAINT_ERROR가 발생합니다. Ada에서는 배열을 다른 대부분의 언어와 마찬가지로 1에서 인덱싱하기 시작하고 0에서는 인덱싱하지 않습니다. 웬일인지 나는 아직도 배열에서 색인하고있다.배열 인덱스의 Ada 유형 충돌

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; 

매트릭스 패키지는 다음과 같이 정의된다 :

이 솔루션은 두 단계로 수행 할 수 있습니다
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; 
+0

왜 colLeft 등 인덱스를하지? 배열을 색인하기 위해'M (Index (rowUp), Index (I)) '과 같은 불필요한 타입 변환보다 깨끗합니다. –

+0

그때 나는 그들에 대한 조작을 할 수 없기 때문에. – Jonaswg

+0

또한'M (Index (rowUp), Index (I))를 수행 할 때 CONSTRAINT_ERROR : 인덱스 검사가 실패했습니다. – Jonaswg

답변

2

:

  • 첫째 :
  • : 명시 적 루프에 사용 된 색인 유형을 확인
for I in Index range colLeft .. colRight loop 
    Put (Elem'Image (M (rowUp, I))); 
end loop; 
  • 둘째 : colLeftcolRight의 선언을 수정 :
colLeft, colRight : Index;