2014-01-06 181 views
0

我想创建一个嵌套for循环,填充1到20的数组中的值。嵌套循环填充数组

IE)array = {1,2,3,4,5,6 ,7,8,9,10,11,12,13,14,15,16,17,18,19,20}

int array [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打印出多次。有人能告诉我这个问题吗?谢谢!

+0

对于我执行内循环的每个值。因此,您将打印21次阵列填充。 –

回答

1

您的外部循环运行21次,您的内部循环运行20次外部循环迭代,因此总共有21 * 20 = 420打印语句。

你可以简单地做

for(int i = 0 ; i < array.length ; i++) 
{ 
    array[i] = i + 1; 
    cout << array[i] << endl; 
} 
0

如果你看看你的阵列,当你完成,这也将是只是一系列20年代。第一个循环说“做20次”,然后第二个循环说“设置并打印该数组元素的值20次”。你需要做的是包括检查你是否将正确的j值赋值给正确的数组[i]值,并且只在这种情况下设置值。例如:

if (j == i + 1) array[i] = j; 
0

为什么你需要一个嵌套循环?

for(int i = 0; i<20; i++) 
{ 
    array[i] = i + 1; 
    cout<< array[i]; 
} 
0

是的,你有两个循环时,你只需要一个:

for(int i = 0; i<21; i++) 
{ 
    array[i] = i + 1; 
    cout<< array[i]; 
} 
0

为了填补数组,并打印你只需要两个简单的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开始到,它正好循环20次。

0

我不知道你为什么试图在你的数组中打印单个元素,但这里没有必要使用嵌套循环。实际上,一个循环根本不需要:

// 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")); 
0

我看到很多人回答了关于这个问题,所以我不会重复它们,我只是提到你正在写数组大小。 如果有int array[20],你应该循环

的for(int i = 0;我< ;我++) 最后索引是19

0

外环21次重复的内循环

for(int i = 0; i<21; i++) 
{ 
    for(int j =1; j<21; j++) 
    { 
     array[i] = j; 
     cout<< array[i]; 
    } 

} 

内部循环执行的操作与分配数组元素的相同数字相同。此外您的代码有缺陷,因为由于外循环youare试图访问元件阵列[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 << ' '; 
    } 
} 
0

如果你真的想使用嵌套解决方案(例如游戏板坐标),那么这是我的解决方案。

// 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; 
} 
-1
int size = 20; 

for (int i = 0; i < size; i++) 
    { int array[i]; 
     array[i] = i + 1; 
     cout << array[i]<< " "; 
    } 

你可以填充1 for循环的数组,衡量你的阵列像上述的大小。