gpt4 book ai didi

c++ - 我的代码出错 - bool 真值表

转载 作者:搜寻专家 更新时间:2023-10-31 02:17:22 24 4
gpt4 key购买 nike

我目前正在开发一个打印 5 变量真值表的程序。我正在使用二维数组。我的代码当前生成了该表,但表示它已损坏并且“变量“表”周围的堆栈已损坏。有帮助吗?

#include <iostream>
using namespace std;

int main() {
bool table[5][32];

for (int i = 0; i < 32; i++) {
for (int j = 0; j < 5; j++) {
table[i][j] = ((i >> j)& 1);
}
}

for (int i = 0; i < 32; i++) {
for (int j = 0; j < 5; j++) {
cout << table[i][j] << " ";
}
cout << endl;
}
return 0;
}

这是作业,所以我想了解它,而不仅仅是得到答案。

最佳答案

索引错误。只有table[0]table[4]可用,所以访问table[5]table[31] 是非法的。

试试这个:

#include <iostream>
using namespace std;

int main() {
bool table[32][5]; // swap 32 and 5

for (int i = 0; i < 32; i++) {
for (int j = 0; j < 5; j++) {
table[i][j] = ((i >> j)& 1);
}
}

for (int i = 0; i < 32; i++) {
for (int j = 0; j < 5; j++) {
cout << table[i][j] << " ";
}
cout << endl;
}
return 0;
}

关于c++ - 我的代码出错 - bool 真值表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35834425/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com