2016-10-16 2 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; 

マトリックスパッケージは次のように定義されます解決策は2つの段階で行うことができます

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; 
関連する問題