嵌套的循环填充数组

Nested for loop filling an array

本文关键字:数组 填充 循环 嵌套      更新时间:2023-10-16

我正在尝试创建一个嵌套的 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 打印出来多次。有人可以告诉我问题吗?谢谢!

外部 for 循环运行

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 循环填充数组,并如上所述测量数组的大小。