I have created a 2d array and initialized to 0 in C++.
int n;
cin>>n;
int ar[n][n]={0};
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cout<<ar[i][j]<<" ";
}
cout<<endl;
}
When I print this array, it prints random garbage values in the first row as shown below
0 0 4294220 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
Why is this happening. I have read about variable length arrays here. But I can't find any explanation why is this happening.