为什么它会在我们没有在数组中包含的内容中进行cout?

Mel*_*ik4 0 c++ arrays

当以下程序有以下输入时(从cin读取):

1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1
Run Code Online (Sandbox Code Playgroud)

输出令人惊讶:

1 1 1 2 2 1 1 1 1 1 1 1 1 1 1 1

#include<iostream>
using namespace std;
int main()
{
    int arey[3][3];
    int i,j;
    for(j=0;j<=3;j++)
    {
        for(i=0;i<=3;i++)
        {
            cin>>arey[j][i];
        }
    }
    arey[0][0]=1;
    arey[3][3]=1;
    i=0,j=0;
    for(j=0;j<=3;j++)
    {
        for(i=0;i<=3;i++)
        {
            cout<<arey[j][i];
        }
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

有人可以解释我应该改变什么来获得与输入相同的输出?

Kar*_*ath 6

矩阵是3x3还是4x4?

你创建了3x3,但循环运行4个元素,你也更新[3] [3]

基本上你的索引溢出,你覆盖矩阵中的不同单元格.

更新:cheecked您的输入,使用:int arey[4][4];