【发布时间】:2023-03-25 15:30:01
【问题描述】:
我取了一个大整数数组并部分填充了它。然后我尝试将它打印到我输入的整数。但我不小心在最后输入的整数之后也打印出了元素。我在两次执行后才意识到我的错误,令人惊讶的是,每次都输出了一个额外的“1”。当我在最后一个输入的整数之后打印出两个元素时,结果是'1'和一个随机整数。我知道随机整数可能是 ram 中已经存在的垃圾值,但是“1”呢?这是我的疑问。
这是我的代码:
#include <iostream>
using namespace std;
int main()
{
int Arr[100], i, n; //declarations
cout<<"Enter the number of array elements!"<<endl;
cin>>n;
cout<<"Enter the elements of the array"<<endl;
for(i=0; i<n; ++i) //Reading in array elements
cin>>Arr[i];
cout<<"Elements of the array are: (including arr[n] and arr[n+1]) "<<endl; //Debugging
for(i=0;i<n+2;i++)
cout<<Arr[i]<<"\t";
return 0;
}
这是执行的示例结果!
示例 1
示例 2
【问题讨论】:
标签: c++ arrays input integer element