[Solved] “file_name.exe has stopped working”


Assuming you are using C++, there are couple of things that you need to change in your code:

  1. scanf("%d", tcs); should rather be scanf("%d", &tcs); This mistake is the reason your program crashes, as tcs doesn’t have the correct intended value.
  2. The function int calc_fact(n) should be outside the main() function with the prototype int calc_fact(int n). Due to this mistake, your code won’t compile in the first place (are you sure when you say that your code complies when the arrays are declared statically)?

Please see the modified code below:

#include <stdio.h>
//#include <conio.h>                    //You actually don't need it.

int calc_fact(int n)
{
    if(n==1)
        return 1;
    else
        return n*calc_fact(n-1);
}

int main()
{
    int n, i, count, k, tcs, j, l, m;
    scanf("%d", &tcs);
    int fact[tcs];
    int arr[tcs];
    for(j=0; j<tcs; j++)
    {
        scanf("%d", &arr[j]);
    }

    j=0;
    m=0;
    while(m<tcs && j<tcs)
    {
        fact[m]=calc_fact(arr[j]);
        m++;
        j++;
    }


    m=0;
    for(l=0; l<tcs; l++)
    {
        i=1;
        count=0;
        while(i<=fact[m])
        {
            if((fact[m])%i==0)
            count++;
            i++;
        }
    m++;
    k=(count)%((10^9)+7);
    printf("%d\n", k);            //and not printf("\n%d", k);

    }

    return 0;
}

Working code here.

2

solved “file_name.exe has stopped working”