c - No display of array -
the following implementation of spoj problem: http://www.spoj.com/problems/fctrl2/
#include <stdio.h> int main() { int t; scanf("%d",&t); while(t--) { int carry=0,k,i,j,num,arr[1000]={1}; scanf("%d",&num); for(i=1;i<=num;i++) { for(j=0;j<=k;j++) { arr[j]=arr[j]*i+carry; carry=arr[j]/10; arr[j]=arr[j]%10; } while(carry) { k++; arr[k]=carry%10; carry/=10; } } for(i=k;i>=0;i--)//doubt { printf("%d",arr[i]); } printf("\n"); } return 0; }
i guess there mistake in displaying array in reverse direction, when change condition i.e for(i=0;i<=k;i++) prints array. please me solve problem.
int carry=0,k,i,j,num,arr[1000]={1}; for(j=0;j<=k;j++)
k
not initialized here, , have indeterminate value.
see https://stackoverflow.com/a/6212973/5708620
external , static variables initialized 0 default, guaranteed. automatic , register variables not have en explicit initializer have indeterminate value (either unspecified value or trap representation).
Comments
Post a Comment