c++ - 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 int
s from 0-99, and it's working as intended.
解决方案
由于j
从 100-299 循环,myArray[j]
永远不会填充 0-99 的元素myArray
,并且在循环的后半部分超出范围。
你要:
for (j = 0; j < 200; j++)
{
myArray[j] = j+100;
}
推荐阅读
- bash - 您如何在 mac 上找到用于键盘绑定的 bash 的终端键代码?
- c# - Unity实例化预制件并获取组件
- git - 为什么我会在 Git 终端中名为 HTML Tid Bits.docx 的文件前面看到这个 ~$ML?
- f# - 如何从 F# 调用 Q# 操作
- java - 如何修复 if 和 else if 语句没有选择正确的语句
- javascript - 从嵌套函数内部的forEach循环内部退出主函数
- mule - 在 Mule 4 中使用 Groovy 脚本调用流
- java - 如何分发一个java程序?
- sql - PostgreSQL 中的循环
- python - dask categorize() 方法后重置 index() 错误