2016-03-06 69 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