嵌套for循环错误

时间:2017-05-19 08:59:07

标签: c++ c++11

为什么外部for循环在我将其设置回变量值后才会增加?我试图创建一个带有8x8矩阵和3x3内核的回旋矩阵来制作一个6x6矩阵。 for循环正确迭代,但int i只设置为变量threeRow,然后它会过早地增加1。假设int i在整个集合中处于相同的范围内。所以第一个集合int i只假设迭代0-2而第二个集合迭代1-3

#include <iostream>
using namespace std;

int main()
{
    int a[6][6];
    int eightArray[8][8];
    int kernel[3][3];
    int sum = 0, product = 0;
    int threeRow = 0, threeColumn = 0, counterOne = 0, counterTwo = 0, column = 0, row = 0;

    for(int i = threeRow; i < 8; i++){
    cout << "______________________________________________" << endl;
        for(int j = threeColumn; j < 8; j++){
           product = eightArray[i][j] * kernel[counterTwo][counterOne];
           sum += product;
           cout << "(" << i << "," << j << ")" << " * (" << counterTwo << "," << counterOne << ") | ";
           counterOne++;
           // After it checks 3 three column it goes to the next row
           if(counterOne % 3  == 0){
              counterOne = 0;
              cout << endl;
              cout << "______________________________________________" << endl;
              break;
           }
       }
       // After it checks three rows the sum of 3x3 matrix within the 8x8 is found
       ++counterTwo;
       if(counterTwo % 3  == 0){
           i = threeRow;
           cout << "Sum of the matricies: " << sum << " placed into 6x6 matrix: " << "[" << row << "][" << column <<"]";
           counterTwo = 0;
           cout << endl << endl << endl;
           // Set the 6x6 matrix with the product
           a[row][column] = sum;
           sum = 0;
           // Starts reading the 8x8 matrix on the next column
           threeColumn++;
           column++;
           // Can only shift it 6 times before out of range
           if(column % 6 == 0){
               cout << endl << "Next set" << endl;
               // Resets the column back to 0
               threeColumn = 0;
               column = 0;
               // Starts reading from next row
               threeRow++;
               row++;
               // After reading 6 rows, breaks out of loop
               if(row % 6 == 0){
                   break;
               }
           }
       }
   }
}

1 个答案:

答案 0 :(得分:0)

您的问题是,在将i设置为threeRow后,它会转到for循环的末尾,然后在再次使用之前递增i。因此,当它下次使用时,它总是为3Row + 1,并且#34;重置&#34;。

你应该简单地将i设置为threeRow-1而不是threeRow,以考虑因for循环而发生在i上的增量。

但是,以这种方式玩for循环并不是一个好主意,它很难跟踪错误,并且会产生很难修改的令人困惑的代码。

一般来说,我建议一个内循环,它总是一个嵌套的3x3循环(对于i = 0 ... 2和对于j = 0到2),并且有一组&#34 ;外部&#34;循环通过基本偏移的循环。然后将外部x / y添加到内部x / y以获得偏移值,并且不需要全部&#34; hacky&#34;然后使用循环迭代器。

这是一个带嵌套循环的参考实现,而不是手动跟踪

#include <iostream>
using namespace std;

int main()
{
    int a[6][6];
    int eightArray[8][8];
    int kernel[3][3];

    for (int i = 0; i < 6; i++) 
    {
        for (int j = 0; j < 6; j++)
        {
            cout << "______________________________________________" << endl;
            int sum = 0;
            for (int ii = 0; ii < 3; ii++)
            {
                for (int jj = 0; jj < 3; jj++)
                {
                    int product = eightArray[i + ii][j + jj] * kernel[ii][jj];
                    sum += product;
                    cout << "(" << i + ii << "," << j + jj<< ")" << " * (" << ii << "," << jj << ") | ";
                } // loop jj
                cout << endl;
                cout << "______________________________________________" << endl;
            } // loop ii

            a[i][j] = sum;
            cout << "Sum of the matricies: " << sum << " placed into 6x6 matrix: " << "[" << i << "][" << j << "]";
            cout << endl << endl << endl;
        } // loop j
        if (i < 5)
        {
            cout << endl << "Next set" << endl;
        }
    } // loop i

    std::cin.get();
}

这个似乎可以避免因尝试手动更改i / j循环迭代器而陷入的所有错误和计时问题。