0

I can't figure it out why my program won't start executing the for loop: the logic for the for loop is correct, but when the program runs it just skips the for loop, without executing it. If i is equal to count, it should read last remaining elements from array called se and write to array finallyDone.

while(check && i < len){
    int fi = first[i];
    int se = second[j];
    if(fi < se){
        finallyDone[count] = fi;
        i++;
    }
    else{
        finallyDone[count] = se;
        j++;
    }
    int l;
    if(i >= len){
        for(l = count; l < len * 2 - count; l++){
            finallyDone[count + 1] = se;
        }
        check = false;
    }
    count++;
}
2
  • 4
    Just debug it. check is probably false and/or i is smaller than len Commented Jul 4, 2014 at 7:05
  • To figure the for loop, we need to know the value of count and len upon the loop. And from this portion of code, I won't know the value of both of them. Therefore just trace through the loop with a debugger. Commented Jul 4, 2014 at 7:07

1 Answer 1

3

This is where it goes wrong:

while(check && i < len){
   //and later
    if(i >= len){

You can only walk to the IF-statement if i is smaller than len, but you will only walk into the IF-statement if i is equal to len or bigger. Those two statements are contradictory.

Sign up to request clarification or add additional context in comments.

2 Comments

Not entirely true, if i == len-1 and fi < se then i will be equal to len. You need the input data to be able to correctly analyze/debug this.
Nope, the i++ happens at the location of your //and later comment, and I see no continue or break statement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.