I was debugging my code for key index counting and found this problem. I do not understand what is happening here. I've looked at the code for too long to see if I am missing something very obvious, but it does not seem like it.
int main()
{
const int r=7,len=10;
int arr[10]={1,4,6,2,0,4,3,6,5,2};
int count[r+1]={0};
for(int i=0;i<len;i++)
{
count[arr[i]+1]++;
}
cout<<arr[0]<<" ";
for(int i=0;i<r+1;i++)
{
count[i+1]+=count[i];
}
cout<<arr[0]<<" ";
return 0;
}
This is the kind of a mock up code which generates the same bug.
Output:-
1 11
I am not changing value of arr
anywhere in my program and still it shows 11
instead of 1
in the output.
if I comment out count[arr[i]+1]++;
or count[i+1]+=count[i];
or both it gives the correct output.
1 1
What is happening please explain. (comment if I am doing something silly).
Edit: This is only happening with arr[0]
.