In the first program you are setting the values of i
and j
outside the for loops and then only changing j
inside the if statement in the outer for loop.
//...
if(i>j)
{
j=i; //<- set j here
k=0;
taken=0;
}
for(;j<n;++j) //<- using j from above
{
//...
In your second piece of code you are resetting j
to zero each time the for loop is ran. You also never set j
to a value before you enter the first for loop so when you use j
in your if statement it will invoke undefined behavior.
//...
if(i>j) // j is uninitialized here so you could get anything
{
j=i; //<- set j here
k=0;
taken=0;
}
for(j=0;j<n;++j) //<- set j to zero here
{
//...
2
solved Why is the output of these 2 programs different on codeforces while they are the same on ideone?