0

why is core getting dumped

#include <iostream> using namespace std; void sk(int a[],int b[],int k,int n) { int count=0; b[k-1]=1; int m; for(int i=0 ; i<n ;i++) { if(a[k-1]&a[i]) { count++; b[i]=1; sk(a,b,i+1,n); } } } int main() { int n,k; cin>>n>>k; int a[n],b[n]; for(int i=0 ; i<n ;i++) { cin>>a[i]; b[i]=0; } sk(a,b,k,n); int d; for(int i=0 ; i<n ; i++) if(b[i]==0) d++; cout<<d; }

24th Apr 2020, 12:26 PM
Shubhank Kulshreshtha
Shubhank Kulshreshtha - avatar
1 ответ
+ 6
You are getting stack overflow because of infinite recursion. if condition a[k-1]&a[i] is true for the first call then it will be true for every subsequent call: a[k-1] is a[i_prev], where i_prev is i from previous call (caller). i_prev can be any number in range [0...n-1]. i is iterate from 0 to n-1. So, on some iteration i will be equal to i_prev. a[i] & a[i] is true for any nonzero a[i]. (if the first call was executed then a[k-1] has nonzero value).
24th Apr 2020, 1:06 PM
andriy kan
andriy kan - avatar