Tuesday 15 March 2011

python - Why is the following code giving Segmentation fault in codechef, but is working absolutely right elsewhere -



python - Why is the following code giving Segmentation fault in codechef, but is working absolutely right elsewhere -

the next code runs pretty in codeblocks , several online compilers, gives segmentation fault whenever submitted.

as per knowledge, seg fault occurs whenever seek access memory not allocated, how , taking place ??

#include<stdio.h> int minimum(int a[][300],int n) { int flag=0,minval=100000000,mini,i,j,temp; for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(a[j][i]!=0&&flag==0) { temp=a[j][i]; flag=1; } else if(a[j][i]!=0&&temp!=a[j][i]) { flag=0; break; } } if(flag==1&&temp<minval) { minval=temp; mini=i; flag=0; } } homecoming mini; } int main() { int i,j,t,n,a[300][300],copy[300],column,subt; scanf("%d",&t); while(t--) { scanf("%d",&n); for(i=0;i<n;i++) for(j=0;j<n;j++) { if(j!=i) scanf("%d",&a[i][j]); else a[i][j]=0; } column=minimum(a,n); for(i=0;i<n;i++) if(a[i][column]==0) break; for(j=0;j<n;j++) copy[j]=a[i][j]; subt=a[(i+1)%n][column]; for(i=0;i<n;i++) { if(copy[i]==0) copy[i]+=subt; else copy[i]+=1; } for(i=0;i<n;i++) printf("%d ",copy[i]); printf("\n"); } homecoming 0; }

i tried submit exact same code in python(exact same logic). that's running pretty everywhere, gives nzec whenever submitted.

def minimum(a,n): flag=0 minval=max(max(a))+1 in range(n): j in range(n): if a[j][i]!=0 , flag==0: temp=a[j][i] flag=1 elif a[j][i]!=0 , temp!=a[j][i]: flag=0 break if flag==1 , temp<minval: minval=temp mini=i flag=0 homecoming mini in range(input()): n=input() a=[] copy=[] j in range(n): temp=(map(int,raw_input().split())) temp.insert(j,0) copy=temp[:] a.append(copy) del temp column=minimum(a,n) print column k in range(n): if a[k][column]==0: break copy=a[k][:] subt=a[(k+1)%n][column] k in range(n): if copy[k]==0: copy[k]=copy[k]+subt else: copy[k]=copy[k]+1 del item in copy: print item, print

how can same code generate 2 types of run-time errors in 2 programming languages

your function minimum perchance homecoming uninitialized value, if mini never set during scan of array. i'm not sure logic supposed be, perhaps should initialize mini = 0 @ top of function or else prevent happening. didn't specify supposed in given matrix, illustration matrix of zeroes trigger behavior.

python c python-2.7 segmentation-fault

No comments:

Post a Comment