扫描阵列特定次数,然后找出元素
Scan through the array a particular amount of times then cout the element
我想遍历一个数组特定次数(k
(,看看它在哪里停止,例如,我1,2,3,4
输出值1
k = 5
因为当它到达数组的末尾时,它会重新开始并继续计数,直到达到k = 5
;我希望这是有道理的,这就是我目前所拥有的:
int numbers[] = {1,2,3,4};
int count =0;
int K =5;
while (count !=k)
{
//dont know what to do here
}
请解释一下逻辑,我无法得到这个。
为什么要遍历数组?只需按数组的大小K
modnumbers
.
int numbers[] = {1,2,3,4};
int K = 5;
int arraySize = sizeof(numbers) / sizeof(numbers[0]);
int count = K % arraySize;
return numbers[count];
由于您的数组大小为 4,如果您想知道当您在数组中循环 k 次时您将在哪个元素中,请:
int c = numbers[k % 4];
在您的情况下above c = 1;
您无需循环。
相关文章:
- 如何为 std::vector 分配内存,然后稍后为某些元素调用构造函数?
- 有没有办法搜索向量的元素,<String>然后检查它是否包含特定的字符,如果它确实打印了它
- 如何在类中制作 2D 数组元素,然后在其构造函数中指定其维度?
- 将集合的随机元素添加到列表中,然后将其从原始集合中移除
- 将函数应用于元组中的每个元素,将每个元素强制转换为类型包中的不同类型,然后作为参数包传递
- 如何打印第一个和最后一个元素的和,然后打印第二个和倒数第二个元素的总和,依此类推
- 对数组进行排序的算法,先是第一个元素,然后是前 2 个元素,然后是前 3 个元素,依此类推
- 如何检查单向链表的每个 3 个元素,然后删除其中一些元素?
- 扫描阵列特定次数,然后找出元素
- 从地图上擦除元素,然后将其放回原处
- 如何在多维数组中搜索单个行,然后对某些行中的每个单个元素执行计算
- 如何将元素的索引从2D数组存储到1D数组中,然后交换这些值
- 如何使用 for 循环将元素加载到我的每个数组中,然后将每个数组的总和输出到一个 sum 数组中
- 如何将一个数组的每个元素与另一个元素进行比较(如果存在),然后打印是,否则否
- C++如何将结构数组初始化为null,然后在while循环中检查该数组的元素是否为null
- 将数组传递到空隙函数,然后递归打印元素
- 在2D向量中找到一个元素,然后将其删除
- 在数组中输入5个元素,检查验证,然后输出C++
- 创建结构元素的动态数组,然后创建该结构的动态数组.C++
- 在数组的元素中插入一个新值,然后移动其余元素