+ 6

[Solved] Can anyone tell me why this code doesn't printing anything and memory is core dumped ??

I've made the code with this problem statement https://www.sololearn.com/post/301657/?ref=app https://code.sololearn.com/cKbEobxxYPjc/?ref=app

9th Apr 2020, 11:31 AM
Preity
Preity - avatar
2 Antworten
+ 6
You mentioned that it says that the core dumped. Here, the problem, as Jayakrishna🇮🇳 mentioned above, is the function call. In the call, you are incorrectly sending n and m as the first two parameters of the function, which would try to access those indices. However, 3 isn't a valid index here, and the farthest the indices go is 2. Hence, your call in main must pass n-1 and m-1 as arguments. Also, this recursive call would keep going beyond 0, to indices -1, -2, and so on. That needs to be fixed too.
9th Apr 2020, 11:57 AM
Param Siddharth
Param Siddharth - avatar
+ 5
if(n==0 &&m==0) return (arr[n][m]==k ? 1: 0); else return (noofpaths(n,m-1,k-arr[n][m],arr) + noofpaths(n-1,m,k-arr[n][m],arr)); } In this a[n][m] is a[3][3] is accessing index out of bounds... maximum array index here is a[2][2] only... Accessing beyond that will segment error.... And also n==0 && m==0 also will cause access - 1, - 2 indexes so try || instead &, n==0 || m==0 may works....
9th Apr 2020, 11:50 AM
Jayakrishna 🇮🇳