-1

I have written a code. but before taking input and giving output it is terminating. How can i fix this? I can not give input to the code. It says:

Process returned 4256912 (0x40F490) execution time : 0.069 s.

#include<bits/stdc++.h>

using namespace std;

int fact(int n) {
   if ((n==0)||(n==1)||(n<0))
      return 1;
   else
      return n*fact(n-1);
}  
int main()
{
   ios_base:: sync_with_stdio(false);
   cin.tie(NULL);

   int t;
   cin>>t;

   for(int i=0;i<t;i++)
   {
       int x1,y1,x2,y2,n,m,r,N,res;

       cin>>x1>>y1>>x2>>y2;

        n=y2-y1;
        m=x2-x1;
        N=n+m;
        r=n;

       res= fact(N)/(fact(r)*fact(N-r));

       cout<<res;
   }
}
bad_coder
  • 11,289
  • 20
  • 44
  • 72
imtinan
  • 191
  • 12
  • 2
    Besides posting here, what steps have you taken to reduce the problem? I.e. Starting with an otherwise-empty `main` that simply dumps "Hello, World!\n" to `std::cout` . Does that work? How about then adding in your `fact` function and invoking it *once* from `main` with a known-answer test value (e.g. no stdio sync, no cin tie, just a single `fact` call). Does *that* work ? Bring in more and more of your code one piece at a time until it exhibits your problem. It is *highly* likely the last thing you brought to the table is the culprit of your issue. – WhozCraig May 27 '20 at 11:12
  • On what input,your program gets terminated? Also,note that factorial of a negative number is not possible,so it's better to check input such that n should not be negative in fact(n). – Amit Nandal May 27 '20 at 11:30
  • [Why should I not #include ?](https://stackoverflow.com/q/31816095/5910058) – Jesper Juhl May 28 '20 at 19:43

1 Answers1

0

The problem is with fact(int n). Change it to this and you should be good to go:

int fact(int n) {
   if ((n==0)||(n==1))
      return 1;
   else
      return n*fact(n-1);
}  

The problem was that you were calling fact function even when n < 0. Which leads to very long recursion stack and ultimately program terminates. In other words your program will never halt.

Deep Raval
  • 377
  • 4
  • 7