我知道这可能是一个非常简单的修复方法,但是我无法找到我的错误,而且我在网上查看的帖子都没有能够帮助我。我在cout线上得到了错误。这是代码:
#include <iostream>
bool online(int a, int network[a][a]) {
/*post condition: returns true if every switch in a network is of even degree. Otherwise, returns false.*/
int switches;
for(int x=0; x < a; x++) {
switches = 0;
for(int y=0; y < a; y++)
if(network[x][y])
switches += 1;
if(switches & 1)
return 0;
}
return 1;
}
int main() {
int arrayOne[6][6] =
{
{0,1,1,0,0,0},
{1,0,0,1,0,0},
{1,0,0,1,0,0},
{0,1,1,0,1,1},
{0,0,0,1,0,1},
{0,0,0,1,1,0}
};
int arrayTwo[8][8] =
{
{0,1,1,0,0,0,0,0},
{1,0,0,1,0,0,0,0},
{1,0,0,1,0,0,0,0},
{0,1,1,0,1,0,0,0},
{0,0,0,1,0,1,1,0},
{0,0,0,0,1,0,0,1},
{0,0,0,0,1,0,0,1},
{0,0,0,0,0,1,1,0}
};
std::cout << online(6, arrayOne) << std::endl;
std::cout << online(8, arrayTwo) << std::endl;
}
答案 0 :(得分:4)
对于C99(支持可变长度数组),没有<iostream>
。确保您将程序编译为C而不是C ++,然后使用<stdio.h>
代替<stdbool.h>
。
printf("%d\n", online(6, arrayOne));
printf("%d\n", online(8, arrayTwo));
对于C ++,编译器可能不支持可变长度数组。您应该使用模板代替online()
。
template <unsigned a>
bool online(int, int (&network)[a][a]) {
//...
}