首页 > 解决方案 > adding numbers 100-299 to an array

问题描述

I'm trying to make an array of 200 integers, filled with integers 100-299. I currently have this:

int myArray[200];

int j;

for(j = 100; j < 300; j++){

    myArray[j] = j;

}

When I display myArray[0] and myArray[200], it displays random junk numbers, so I know that my array isn't filling properly. I also have another array filled with ints from 0-99, and it's working as intended.

标签: c++arrays

解决方案


由于j从 100-299 循环,myArray[j]永远不会填充 0-99 的元素myArray,并且在循环的后半部分超出范围。

你要:

for (j = 0; j < 200; j++)
{
    myArray[j] = j+100;
}

推荐阅读