如何将数组中的连续数字设置为0 C++
Posted
技术标签:
【中文标题】如何将数组中的连续数字设置为0 C++【英文标题】:How to get consecutive numbers in array set to 0 C++ 【发布时间】:2020-08-06 03:50:27 【问题描述】:这是它应该做的: 此函数应采用 2D 数组,并应检查每一行和每一列是否存在在行或列上连续重复/匹配 3 次的任何值。它还应该识别是否有超过 3 个并且应该将其全部视为 1 个匹配项。匹配的值应全部设置为零。使用相同的常量大小。您不知道这些值可能是什么(在下面的示例中,它们是 1,2 和 3,但它可以是除零以外的任何值)。 void checkMatches(int[ARRAY_SIZE][ARRAY_SIZE] arr)
这就是我所拥有的:
const int ARRAY_SIZE=8;
void checkMatches(int arr9[ARRAY_SIZE][ARRAY_SIZE])
int count = 0;
//check each row
for (int row = 0; row < ARRAY_SIZE; row++)
int i = arr9[row][0];
for (int col = 1; col < ARRAY_SIZE; col++)
if ( arr9[i] == arr9[i + 1])
count++;
if (count >= 3)
i = 0;
else
count = 0;
//check each column
for (int col = 0; col < ARRAY_SIZE; col++)
int i = arr9[0][col];
for (int row = 1; row < ARRAY_SIZE; row++)
if (arr9[i] == arr9[i + 1])
count++;
if (count > +3)
i = 0;
else
count = 0;
int main()
int val;
int arr9test[8][8];
for (int i = 0; i < 8; i++)
for (int j = 0; j < 8; j++)
cout << "Enter number to populate the array (include a few consecutive numbers): ";
cin >> val;
arr9test[j][i] = val;
checkMatches(arr9test);
cout << "Replacing consecutive numbers with 0's: " << arr9test << endl;
任何提示将不胜感激!
这是我得到的输出。 (非常不是数组) enter image description here
【问题讨论】:
问题是什么?我认为该程序没有做正确的事情。如果是这样,请准确描述会发生什么。 也使用它发布main
。所以它会变成Minimal Reproducible Example。
由于其他一些问题,我目前无法运行该程序,但我想知道我的代码是否会按照问题要求执行,或者我是否走错了路。 @cigien
找出问题的最佳方法是解决问题,运行代码并查看它的作用。
我想知道我的代码是否会按照问题所说的那样做,由于其他问题,我无法运行程序来测试它。我也添加了主!谢谢!
【参考方案1】:
目前你的代码有很多问题。
void checkMatches(int arr9[ARRAY_SIZE][ARRAY_SIZE]) // 01
int count = 0;
//check each row
for (int row = 0; row < ARRAY_SIZE; row++)
int i = arr9[row][0]; // 02
for (int col = 1; col < ARRAY_SIZE; col++)
if ( arr9[i] == arr9[i + 1]) // 03
count++;
if (count >= 3)
i = 0; // 04, 05
else
count = 0;
//check each column
for (int col = 0; col < ARRAY_SIZE; col++)
int i = arr9[0][col]; // 02
for (int row = 1; row < ARRAY_SIZE; row++)
if (arr9[i] == arr9[i + 1]) // 03
count++;
if (count > +3)
i = 0; // 04, 05
else
count = 0;
我已经用 // xx 标记了有问题的行,其中 xx 是问题编号,如下所示:
-
您正在传入一个没有维度的数组。
也尝试传递尺寸。例如:
void checkMatches(int[][] myArray, size_t xSz, size_t ySz)
-
您正在创建一个无用的堆栈变量。删除它。
您正在比较“未来”地址。这与您的循环不兼容(从 1 开始),并且会导致越界行为。这应该修复为使用“过去”地址:
if (arr9[row][col] == arr9[row][col - 1])
-
您正在写入临时变量“i”,它没有任何作用。您需要写入表本身。
arr9[row][col] = 0;
但是,这本身就是一个问题,因为它只会在第 3 次匹配时写入 0,并且会有效地重置计数,因为下一次比较将与刚刚写入的 0 进行比较。
-
您在进行第二遍之前写入结果。
请考虑下表:
1 2 3 4 5 6 7
2 3 4 4 4 4 6
3 2 1 4 1 2 3
如果我理解正确,您希望得到以下内容:
1 2 3 0 5 6 7
2 3 0 0 0 0 6
3 2 1 0 1 2 3
这意味着您应该在阅读时记录地址,并在最后执行更新。
这是您的方法的伪代码重写。为简洁起见,我使用了 C# 元组表示法:
void checkMatches(int[][] arr9, size_t ySz, size_t xSz) // 01
std::vector<(y, x)> lst; // a vector of some type of tuple structure.
int count = 0;
//check each row
for (size_t row = 0; row < ySz; row++)
for (size_t col = 1; col < xSz; col++)
if ( arr9[row][col] != arr9[row][col - 1])
if (count > 3)
size_t cl = col - 1;
while (count-- > 0)
lst.push_back(row, cl--);
else
count = 0;
else
count++;
if (count > 3)
size_t cl = xSz - 1;
while (count-- > 0)
lst.push_back(row, cl--);
// check each column
// TODO: apply the above code to check columns this time. I won't copy/paste here, you can do that. Keep adding to lst.
// Update the table.
for ((size_t, size_t) xy : lst)
arr9[xy.y][xy.x] = 0;
【讨论】:
以上是关于如何将数组中的连续数字设置为0 C++的主要内容,如果未能解决你的问题,请参考以下文章