Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here ,
In this program when I debug , and when the control reaches the if statement which contains // symbol beside it,
At that point when I click stepover option , the control is reaching the end of the function.
Why is it happening?


C++
#include <stdio.h>
#include<stdlib.h>

void func(int i,int *nums,int j,int numsSize, int *p)
{
    if(i==(numsSize-1))
    {
        (*p)=1;
        return ;
    }
    else
    {
        int k=nums[i]+i;
        if(k>=numsSize)     //
        {
            k=numsSize-1;
        }
        for( ;(k>i) && ((nums[k]!=0)||(nums[k]==0 && k==numsSize-1)) ; k--)
        {
            func(k,nums,j,numsSize,p);
            if((*p)==1)
            {
                return ;
            }
        }
    }
}

int canJump(int* nums, int numsSize){
    int i=0;
    int j=0;
    int *p=(int*)malloc(sizeof(int));
    *p=0;
    func(i,nums,j,numsSize,p);
    if((*p)==0)
    {
        return 0;
    }
    else
    {
        return 1;
    }
}

int main()
{
    int nums[]={2,5,0,0};
    int numsSize=4;
    canJump( nums, numsSize);
}


What I have tried:

I tried searching in google...............................................................
Posted
Updated 3-Sep-21 4:07am
Comments
Richard MacCutchan 3-Sep-21 9:27am    
Instead of wasting time with Google, look at all the values of the variables in the following for statement to see why it does not get executed.
The Other John Ingram 3-Sep-21 10:08am    
put a printf statement just before the test and see just what the two variables are.
KarstenK 3-Sep-21 14:10pm    
Thats why it is best practice to only write one command at one line ...

When I ran your code I printed the values of all the variables in the for statement thus:
k: 2 > i: 0) && ((nums[k]!=0: 0)||(nums[k]==0 && k==numsSize-1: 4

which shows:
k: 2 
i: 0
nums[k]: 0
numsSize: 4

which means the first attempt to run the statement fails because the for expression is false. Hence the function returns immediately.
 
Share this answer
 
Comments
CPallini 3-Sep-21 10:08am    
Indeed.
My 5.
It jumps at the end of the function because, with
C
k = 2; 
nums[k] = 0; 
numsSize = 4;

the condition of the for loop is false (0).
 
Share this answer
 
Comments
Richard MacCutchan 3-Sep-21 10:35am    
I expect you worked it out quicker than me.
CPallini 3-Sep-21 11:45am    
No way. You was faster than I.
We can't tell: it requires you code running with your data to find out - and we have no access to either.

So start by doing a fresh full rebuild compilation (this ensures the code you look at and the EXE file are the same).
Then run your code in the debugger with a breakpoint on the if line, and the first line of code. Before you step it, look at the values of k, i, and nums[k]and work out what should happen with the if
Step it and see what did happen.

We can't do any of that for you!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900