2

The following is my implementation of the 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 is a mistake in displaying array in reverse direction, but when I change the condition i.e for(i=0;i<=k;i++) it prints the array. Please help me solve this problem.

Monte San
  • 135
  • 2
  • 12

2 Answers2

2
int carry=0,k,i,j,num,arr[1000]={1};

for(j=0;j<=k;j++)

k is not initialized here, and will have an indeterminate value.

See https://stackoverflow.com/a/6212973/5708620

External and static variables are initialized to zero by default, this is guaranteed. Automatic and register variables that do not have en explicit initializer will have an indeterminate value (either an unspecified value or a trap representation).

Community
  • 1
  • 1
Danny_ds
  • 11,201
  • 1
  • 24
  • 46
0

Yes ,,It depends upon the compiler and OS version.Generally i am guessing in your case it might has been assigned with garbage value.