Reputation: 11
this piece of code:
int findsmallerNumber(int low, int high, string *str)
{
int counter = 0;
string ss = *str;
for(int i = low + 1; i <= high; i++)
{
if(ss[i] < ss[low])
counter++;
}
cout<<counter<<" ";
return counter;
}
produces the correct output as:
4 4 3 1 1 0
but when the counter variable isn't initialized as such:
int counter;
the output obtained is:
4 8 11 12 13 13
Can someone please explain this behavior?
Upvotes: 0
Views: 72
Reputation: 238291
Can someone please explain this behavior?
The value of an uninitialised variable is indeterminate. If you read an indeterminate value, then the behaviour of the program is undefined. That explains the behaviour that you observe. Don't read indeterminate values.
Upvotes: 2