-4

2次元配列を昇順でソートし、それらを1次元配列(低から高)に格納しようとしています。

16 22 99 4 18
-258 4 101 5 98
105 6 15 2 45
33 88 72 16 3

しかし、私が持っているのはループし続けることであり、その理由はわかりません

 int main()                  
 {                             
     const int SKIP=-999999;
     const int SIZE=20;
     const int ROWS=4;
     const int COLS=5;         
     int unsorted_array[ROWS][COLS]= {(16,22,99,41,18),
                                      (-258,4,101,5,98),
                                      (105,6,15,2,45),
                                      (33,88,72,16,3)};   
     int s_index=0;
     int min_value,rowMin,rowCol,row,col,colMin;
     int sorted[SIZE];

     cout <<"Array Sorted"<<endl
         <<"___________"<<endl<<endl;


 while (s_index < SIZE)
 {
     rowMin=0;
     rowCol=0;
     min_value = unsorted_array[0][0];
     row=0;
     while (row < ROWS)
     {
         col=0;
         while (col < COLS)
         {
             if (unsorted_array[row][col] < min_value)
             {
                 min_value = unsorted_array[row][col];
                 rowMin = row;
                 colMin = col;
             } 
             ;
             col = col + 1;
         }
         row = row + 1;  
     }
     sorted[s_index] = min_value; 

     while (sorted[s_index] >= 0)
     {
         cout<<" "<<sorted[s_index];
     }
     unsorted_array[rowMin][colMin]=SKIP; 
     s_index=s_index+1; 
}
cout<<endl;
4

4 に答える 4

7

が 1 回 true の場合sorted[s_index] >= 0、これは無限ループになります。

 while (sorted[s_index]>=0)
 {
     cout<<" "<<sorted[s_index];
 }

そのループ内で s_index が変更されることはありません。

于 2012-04-27T23:00:50.507 に答える
3

ここに問題があります。while 条件はループ内で変化しないため、述語が true の場合、ループは終了しません。

while (sorted[s_index]>=0){
  cout<<" "<<sorted[s_index];
}  
于 2012-04-27T23:01:22.943 に答える
2

述語が真の場合、これは明らかな無限ループです。

while (sorted[s_index]>=0)
   {
     cout<<" "<<sorted[s_index];

   }
于 2012-04-27T23:01:11.817 に答える
1

他の人がすでに指摘したように、あなたの cout ループは無限です。より良い使用:

     for (int i=0; i < SIZE ;i++){
         cout<<" "<<sorted[i];
     }
     cout << endl;
于 2012-04-27T23:12:33.493 に答える