c++ - 数组的行操作

标签 c++ matrix rows

我必须做这个练习:

Write a C + + function that, given an array with two NxN size of integers, returns true if the rows to have all the same elements (even in different locations), and false otherwise.

这是我的代码:

#include <iostream>

using namespace std;

const int N = 5;


bool fool ( const int a [N][N])
{
    int x,y = 0;
    int j=0;
    int i=0;

    bool check[N] = {false};
    bool verify = false;

    for (i=0; i<N; i++)
    {

       check[i] = false;

    }

       while(j<N && !verify )
       {

         if(a[x][i]==a[y][j] && !check[j] )
         {

            check[j]=true;
            verify=true;
         }

         j++;

       }

       return verify;

}


int main ()
{


    const int a[N][N] = {{1,3,5,6,7},
                         {5,6,7,1,3},
                         {1,6,5,3,7},
                         {6,1,3,5,7},
                         {6,5,1,7,3}};




    if ( fool(a))
    {

           cout << " in all rows there are the same elements";

    }


    else
    {

           cout << " wrong, . ";

    }

    return 0;


}

但是程序崩溃了。我该如何解决?

最佳答案

while( int j<N && !verify )

不要在循环内部声明j,它应该在外部声明。我很惊讶这甚至可以编译(它不能在 Visual Studio C++ 中编译)。

关于c++ - 数组的行操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10350253/

相关文章:

c++ - 多个函数定义错误

C++ float 组初始化

matrix - 提取矩阵的下三角部分

python - 在没有numpy的情况下更改对角线方阵 - Python

visual-studio-2010 - SSRS 有条件地从表中删除行

sql-server - 在sql server中将列转换为行及其各自的数据

copy - 如何从 NSTableView 复制行

c++ - 基数排序 base 256 性能

c++ - 按回车后在输入旁边打印输出

c++ - 位矩阵相对于位图的优势