C函数计算数组中的唯一列
在为程序编写工作函数时遇到问题,函数需要给出列元素不重复的列数 例如,对于阵列:C函数计算数组中的唯一列,c,C,在为程序编写工作函数时遇到问题,函数需要给出列元素不重复的列数 例如,对于阵列: 1234 1 3 1 1 二,二, 在这种情况下,唯一列为: 34 11 2.2 程序应该给出:2 这是代码部分 int uniqueColumns = 0; int fail; for (int currentColumn = 0; currentColumn < totalColumns; currentColumn++) { fail = 0; for (int current
- 1234
- 1 3 1 1
- 二,二,
- 34
- 11
- 2.2
int uniqueColumns = 0;
int fail;
for (int currentColumn = 0; currentColumn < totalColumns; currentColumn++)
{
fail = 0;
for (int currentRow = 0; currentRow < totalRows; currentRow++)
{
for (int rowOffset = currentRow + 1; currentRow < totalRows; currentRow++)
{
if (array[currentRow][currentColumn] == array[currentRow+rowOffset][currentColumn])
{
fail = 1;
break;
}
else
{
fail = 0;
}
}
if (fail == 1)
{
break;
}
}
if (fail == 0)
{
uniqueColumns++;
}
}
int uniqueColumns=0;
int失败;
对于(int currentColumn=0;currentColumn
我假设缺少的问题是“为什么我的代码不工作?”
这可能不是你唯一的问题,但一定是其中之一。您正在比较:
array[currentRow][currentColumn] == array[currentRow+rowOffset][currentColumn]
当rowOffset
的范围从currentRow+1
向上时。这显然不是你想要的。如果你比较一下就可以了
array[currentRow][currentColumn] == array[rowOffset][currentColumn]
或者将rowOffset
的范围从1向上
我可能完全错了,但我认为这是一个编程练习。如果是这样的话,我认为你的导师很可能希望你做一些更聪明的事情来检测重复的元素,而不是比较所有的对。你知道数组元素的值吗?如果是,你能想出更好的办法吗?元素的值在主程序中自动生成,我不会尝试比较所有对。至少我希望Iam不会,因为我的目标是将当前行元素与列中的所有下一个元素进行比较。您知道它们的最小值和最大值吗?目前,Iam使用0到10的随机整数测试代码