为什么我的for循环会跳过数组中的最后一个值
Why does my for loop skip my last values in my array?
我的程序应该计算数组中有多少个唯一值。到目前为止,我已经按升序对数组进行了排序(只是为了玩不同的算法),并且正在处理试图计算每个唯一值有多少个值(4,4,4,4,2,3,2,其中2为2,3为1)。
我有一个示例数组,我一直在测试它,它由数字1-10组成,但我的for循环将只到计数8。这可能是一些显而易见的事情,但现在我只是试图找到错误。
受影响的代码片段:
Index =数组的大小
for (int i = 0; i < index; i++)
{
uniqueness = true;
for (int l = 0; l < index; l++)
{
if (numbers[i] == temporary[l])
{
uniqueness = false;
}
}
if (uniqueness)
{
temporary[i] = numbers[i];
number_amount = 0;
for (int j = 0; j < index; j++)
{
if (numbers[j] == temporary[i])
{
number_amount++;
}
}
Output(temporary[i], number_amount);
}
}
/*从数组中找到唯一的数字
int arr = [ 1, 1, 1, 2, 2, 2, 3, 3, 4, 5, 7, 9];
int unique[arr.length];
int arrCount = 0;
for (int i = 0; i < arr.length;) {
bool isUnique = true;
int currentIndex = i + 1;
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] == arr[j]) {
isUnique = false;
} else {
currentIndex = j;
break;
}
}
i = currentIndex;
if (isUnique) {
unique[arrCount]=arr[i - 1];
arrCount++;
}
}
enter code here
对于所有的For循环,它应该是"<= index"而不是"
相关文章:
- 在c++中为double类型的数组创建一个unique_ptr
- 从数组构造一个跨度<shared_ptr<T>><shared_ptr<void>>
- 给定一个单词数组和一个字符串,如何计算给定字符串中的所有单词
- 如何将 v8::FunctionCallbackInfo<v8::Value> 数组从一个隔离复制到另一个隔离?
- 我怎么能有一个以2D数组为参数的函数,而数组有一个我想更改的参数/维度
- 为什么我的数组输出一个不在其中的元素
- 查找数组最后 3 个数字的平均值
- 数组 - 最后一个索引
- 将数组作为一个参数从一个函数传递到C 中的另一个函数
- 从字符串数组创建一个字符数组
- 如何在C 中的小写中将文本从数组的一个维度传递到另一个维度
- 在对结构数组的一个成员进行排序后,移动其成员的其余部分
- 为什么数组在一个文件中打印,但在发送到另一个文件时却缺少数字
- C++,为什么多维数组的一个元素的增加似乎增加了另一个元素
- 将数组的一个元素与整个数组元素进行比较
- 如何将数组从一个函数传递到另一个函数
- C++:通过引用传递对象数组(指向一个?的指针)
- 查找最大和连续子数组-另一个版本
- 如何使用一个整数数组作为一个无序映射的键
- 使用for循环将char数组的一个元素复制到另一个元素