c - Why my code is getting Runtime error? -
it took me 3 hrs logic solve question , code accordingly.
getting runtime error. can please me know mistake doing ? edit : running not printing anything.
#include <stdio.h> #include<math.h> float distance(float n1,float m1,float n2,float m2){ float d=0;float sum=0; d =sqrt(pow(m2-m1,2)+pow(n2-n1,2)); sum+=d; printf("%.2f",sum); return sum; } int main(void) { int t,n,i,j;float sum=0; scanf("%d",&t); while(t--){ scanf("%d",&n); int r=0,s=0,a=0,b=0; int x[n],y[n],p[n],q[n],min[n],max[n]; for(i=0;i<n;i++){ scanf("%d %d",&x[i],&y[i]);} for(j=0;j<10001;j++){ for(i=0;i<n;i++){ if(j==x[i]){ p[r++]=x[i];q[s++]=y[i]; } }} for(i=0,j=i+1;i<n,j<n;i++,j++){ if(p[i]==p[j]){ if(q[i]>q[j]){min[a++]=p[i]; max[b++]=q[i];} else{min[a++]=p[i]; max[b++]=q[j];} } else{min[a++]=p[i]; max[b++]=q[i];} } for(i=0;i<n;i++){ distance(min[i],max[i],min[i+1],max[i+1]); } } }
this line:
int x[n],y[n],p[n],q[n],min[n],max[n];
is not correct. in c/c++ cannot declare plain arrays of variable size (unlike, say, in basic). size of such array must known @ compile time.
possible solutions are:
- use dynamic memory (
malloc()
/free()
) - use statically allocated arrays set maximum size (and either run risk of buffer overflow , memory corruption or make sure use no-more-than space have allocated for)
- use
std::vector<int>
Comments
Post a Comment