嵌套的循环填充数组
Nested for loop filling an array
我正在尝试创建一个嵌套的 for 循环,用于填充从 1 到 20 的数组中的值。
IE) 数组 = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}
整数数组[20];
for(int i = 0; i<21; i++)
{
for(int j =1; j<21; j++)
{
array[i] = j;
cout<< array[i];
}
}
假设,数组索引应该与"i"一起计数,并且应该等同于"j",后者也在计数。数组元素在填充时打印到控制台。
我预计 1 -20打印一次,但是当我运行代码时,1-20 打印出来多次。有人可以告诉我问题吗?谢谢!
21 次,内部 for 循环每个外部 for 循环迭代运行 20 次,因此您总共有 21 * 20 = 420
个打印语句。
你可以简单地做
for(int i = 0 ; i < array.length ; i++)
{
array[i] = i + 1;
cout << array[i] << endl;
}
如果你在完成后查看你的数组,它也只是一系列 20 秒。第一个循环说"执行此操作 20 次",然后第二个循环说"设置并打印该数组元素的值 20 次"。您需要做的是检查是否将正确的 j 值分配给正确的 array[i] 值,并且仅在该情况下设置该值。像这样:
if (j == i + 1) array[i] = j;
为什么需要一个嵌套循环?
for(int i = 0; i<20; i++)
{
array[i] = i + 1;
cout<< array[i];
}
是的,当你只需要一个循环时,你有两个循环:
for(int i = 0; i<21; i++)
{
array[i] = i + 1;
cout<< array[i];
}
为了填充数组并打印结果,您只需要两个简单的 for 循环
for(int i = 0; i<20; i++)
{
array[i] = j;
}
for(int j =0; j<20; j++)
{
cout<< array[i];
}
您在上面创建的嵌套循环将完全按照您的描述执行。对于外部 for 循环的每个循环,它将执行内部循环的完整 20 个循环。因此,您总共将执行21 * 20次。
还要小心你的索引。你想从int i = 0
到i开始,< 20
它正好循环了20次。
我不知道你为什么要在你的数组中打印一个元素,但这里没有必要使用嵌套循环;事实上,循环根本不是必需的:
// vector version
std::vector<int> vec(20);
std::iota(vec.begin(), vec.end(), 1);
// array version
int arr[20];
std::iota(std::begin(arr), std::end(arr), 1);
如果要在初始化后打印出整个数组:
std::copy(vec.begin(), vec.end(), std::ostream_iterator<int>(std::cout, "n"));
我看到很多人回答了这个问题,所以我不会重复它们,我只想提到你在数组大小之外写作。如果你有int array[20]
,你应该循环
for(int i = 0; i<<strong>20; i++) 最后一个索引是 19
外循环重复内循环 21 次
for(int i = 0; i<21; i++)
{
for(int j =1; j<21; j++)
{
array[i] = j;
cout<< array[i];
}
}
内部循环执行的操作与分配数组序列号的元素的操作相同。此外,您的代码有一个错误,因为由于外部循环,您正在尝试访问不存在的元素数组[20],因为如果数组被定义为
int arrat[20];
则有效指示为 0 - 19。
这不用费心编写正确所需的循环或可以使用标准算法的循环std::iota
例如
#include <iostream>
#include <numeric>
#include <iterator>
#include <algorithm>
int main()
{
const size_t N = 20;
int array[N];
std::iota( std::begin( array ), std::end( array ), 1 );
std::copy( std::begin( array ), std::end( array ), std::ostream_iterator<int>( std::cout, " " ) );
}
或者,您可以使用基于范围的 for 语句代替算法。例如
#include <iostream>
int main()
{
const size_t N = 20;
int array[N];
int i = 1;
for ( int &x : array )
{
x = i++;
std::cout << x << ' ';
}
}
如果你真的想使用嵌套解决方案(例如游戏板坐标),那么这就是我的解决方案。
// nesting arrays for example game board coordinates
#include <iostream>
int main(){
int x = 20;
int y = 40;
int array[x][y];
// initialize array of variable-sized.
for(int i = 0; i < x; ++i){
for(int j = 0; j < y; ++j){
array[i][j] = 0; // or something like i + j + (i * (y-1)) if you wish
// and send it to cout
std::cout << array[i][j] << " ";
}
std::cout << std::endl;
}
//notice, that when sent to cout like this, x and y flips on screen, but
//logics of coordinates is ok
// and then do something usefull with it
return EXIT_SUCCESS;
}
int size = 20;
for (int i = 0; i < size; i++)
{ int array[i];
array[i] = i + 1;
cout << array[i]<< " ";
}
您可以使用 1 for 循环填充数组,并如上所述测量数组的大小。
- 在c++中用vector填充一个简单的动态数组
- 如何使用用户输入在C++中正确填充2D数组
- 如何找到大小'x'数组是否完全填充,在C++?
- 如何正确填充在堆上分配的二维数组?
- 如何将零填充的多维数组传递给 C++ 中的函数?
- C++ 填充数组时遇到问题
- 数组填充了不正确的值
- 使用初始化列表填充C++中的多维结构数组时出现问题
- 用数据填充未指定大小的数组
- C++填充多维标准::数组
- 如何在 c++ 上将一定大小的数组填充到另一个更大大小的数组中?
- 有没有办法使用数组填充向量?
- 如何使用 PPM 格式的 uchar* 数组填充 QPixmap
- 在C 中动态分配的数组填充
- 使用数组填充对称矩阵
- C++中的{0}数组填充赋值
- 如何在OpenCV中用OpenNi的deepPixel数组填充矩阵
- 按行、列或随机从数组填充矩阵时出现错误索引问题
- 如何加快非顺序数组填充
- 将数组填充到最大值(memcpy vs vector)