2016-03-06 71 views
0

我正在寫一個關於清潔2維陣列中的灰塵的真空吸塵器的代碼。我希望這個清潔程序進入一行的結尾,然後回來完成下一行,依此類推。但是在這段代碼中,它會到第一行的末尾,並再次回到第一行的開頭!我該怎麼辦?非常感謝。For Loop in Vacuum cleaner

#include <iostream> 
#include <cstdlib> 
#include <ctime> 

using namespace std; 

int main() 
{ 
    srand(time(0)); 
    int i = 0; 
    int j; 
    int x = 0; 
    int y = 0; 
    int m = 0; 
    int t = 0; 
    int array[5][5]; 
    int step = 0 , trash, total = 0, num = 0; 

    //Prepare the random array 

    for (x=0; x<5; x++) 
    { 
     for(y=0; y<5; y++) 
     { 

      array[x][y] = rand() % 2; 

      if (array[x][y] == 1) 
      { 
       total++; 
      } 
     } 
    } 

    //Print the total number of trashes based on the random number 

    cout<<endl<<"The total number of trashes are: "<<total<<endl<<endl; 

    //print the random array 

    for (x=0; x<5; x++) 
    { 
     for(y=0; y<5; y++) 
     { 
      cout<<array[x][y]<<"\t"; 
     } 
     cout<<endl; 
    } 
    cout<<endl<<endl; 

    //------Manual Garbage Collector 
    cout<<"------ Manual Garbage Collector ------"<<endl<<endl; 

    while(num != total) 
    { 
     if(m % 2 != 0) 
     { 
      if(t==1) 
       i=1; 
      else if (t==3) 
       i=3; 

      for(i ; i<5 ; i++) 
      { 
       for(j=5 ; j>0 ; j--) 
       { 
        trash = array[i][j]; 

        if(trash == 1) 
        { 
         cout<<"I found a trash in --> "<<"row: "<<i<<"\t column: "<<j<<endl; 
         num++; 
         step++; 
        } 
        else 
        { 
         step++; 
        } 
       } 
      m++; 
      t++; 
      break; 
      } 
     } 
     else if (m % 2 == 0) 
     { 
      if(t==2) 
       i=2; 
      else if (t==4) 
       i=4; 

      for(i ; i<5 ; i++) 
      { 
       for(j=0 ; j<5 ; j++) 
       { 
        trash=array[i][j]; 

        if(trash == 1) 
        { 
         cout<<"I found a trash in --> "<<"row: "<<i<<"\t column: "<<j<<endl; 
         num++; 
         step++; 
        } 
        else 
        { 
         step++; 
        } 

       } 
      m++; 
      t++; 
      break; 

      } 
     } 
    } 

    cout<<endl<<"----------------"<<endl<<"The number of steps that I have tried: "<<step<<endl; 

    return 0; 
} 
+0

的遍歷'我'永遠不會跑一次以上。在第一次(也是唯一一次)迭代結束時,你明確地將它從中斷出來;然後再輸入'i == 1' –

+0

我使用了幾種解決方案來解決這個問題,但我找不到正確的答案。任何人都可以幫我,我該怎麼辦?謝謝。 – Sahand

+0

這個問題有什麼不清楚的地方? – Sahand

回答

0

您的代碼不會真正去到下一行,當達到行的結束,而是一個問題是你的第一個j循環(65行),因爲它會出界。它應該是:

for(j = 4; j >= 0; j--) { 

但是更大的問題是整個邏輯是奇怪的和不必要的複雜。同樣的任務可以用什麼來完成simplier這樣的:

const int SIZE = 5; 
int direction[] = { 1, -1 }; 
int lower_limit[] = { 0, SIZE - 1 }; 
int count = 0; 
int k = 0; 

// for every row 
for (int i = 0; i < SIZE; i++) { 
    // sweep the row in the right direction 
    int j = lower_limit[k]; 
    for(int l = 0; l < SIZE; ++l) { 
     // clean the trash 
     if (array[i][j]) { 

      cout << "I found a trash in --> row: " << i 
       << "\t column: " << j << '\n'; 
      ++count; 

      // I don't think it's worth checking now if all the dust were 
      // collected, you'll save a few iterations but this condition 
      // has to be checked for half of the cells at average 
     } 
     j += direction[k]; 
    } 
    k = (k == 0) ? 1 : 0; 
} 

或更多類似到您的代碼:

const int SIZE = 5; 
int count = 0; 
bool going_right = true; 

// for every row 
for (int i = 0; i < SIZE; i++) { 
    // sweep the row in the right direction 
    if (going_right) { 
     for(int j = 0; j < SIZE; ++j) { 
      // clean the trash 
      if (array[i][j]) { 
       cout << "I found a trash in --> row: " << i 
        << "\t column: " << j << '\n'; 
       ++count; 
      } 
     } 
     // reverse direction 
     going_right = false; 
    } 
    else { 
     for(int j = SIZE - 1; j >= 0; --j) { 
      // clean the trash 
      if (array[i][j]) { 
       cout << "I found a trash in --> row: " << i 
        << "\t column: " << j << '\n'; 
       ++count; 
      } 
     } 
     going_right = true; 
    } 
} 

這在程序中給出了這樣的輸出:

The total number of trashes are: 16 

1 0 1 0 0 
1 1 0 1 0 
1 1 0 1 1 
1 1 1 0 0 
0 1 1 1 1 


------ Manual Garbage Collector ------ 

I found a trash in --> row: 0 column: 0 
I found a trash in --> row: 0 column: 2 
I found a trash in --> row: 1 column: 3 
I found a trash in --> row: 1 column: 1 
I found a trash in --> row: 1 column: 0 
I found a trash in --> row: 2 column: 0 
I found a trash in --> row: 2 column: 1 
I found a trash in --> row: 2 column: 3 
I found a trash in --> row: 2 column: 4 
I found a trash in --> row: 3 column: 2 
I found a trash in --> row: 3 column: 1 
I found a trash in --> row: 3 column: 0 
I found a trash in --> row: 4 column: 1 
I found a trash in --> row: 4 column: 2 
I found a trash in --> row: 4 column: 3 
I found a trash in --> row: 4 column: 4 

---------------- 
The number of steps that I have tried: 25