title: Coin Change C++
body: <p>I've tried to solve a coin change problem in such a way that it'll compute the minimum numbers of coins that can be used. I've used the algorithm post on <a href="http://www.algorithmist.com" rel="nofollow">http://www.algorithmist.com</a>. Here's the algorithm:</p>
  
  <pre><code>C(N,m) = min(C(N,m - 1),C(N - Sm,m) + 1)
  
  with the base cases:
  
      C(N,m) = 1,N = 0
      C(N,m) = 0,N &lt; 0
      C(N, m) = 0, N &gt;= 1, m &lt;= 0
  </code></pre>
  
  <p>But when I write the code it run to infinity. </p>
  
  <p>Here's the code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;algorithm&gt;
  using namespace std;
  int Types[101];
  int  Coins(int N, int m)
  {
      if(N==0)
      {
          return 1;
      }
      else if(N&lt;0)
      {
          return 0;
      }
      else if(N&gt;0 &amp;&amp; m&lt;=0)
      {
          return 0;
      }
      else
      {
          int a = Coins(N,m-1);
          int b = Coins(N-Types[m],m) + 1;
          int c = min(a,b);
          return c;
      }
  }
  
   int main()
  {
      int noOfCoins, Target;
      cin &gt;&gt; noOfCoins &gt;&gt; Target;
      for(int i = 0; i&lt;noOfCoins; i++)
      {
          cin &gt;&gt; Types[i];
      }
      cout &lt;&lt; Coins(Target, noOfCoins);
      return 0;
  }
  </code></pre>
  
  <p>What can be wrong?</p>