title: Extracting a subsequence from an array
body: <p>I'm trying to solve an algorithm for extracting a subsequence from an array. It should display the longest subsequence of prime numbers. I have written the whole algorithm but I still get an infinite cycle and I can't figure out where and why. I'm incrementing both indices and modifying the first index at the end, but it is still not working. Thanks a lot !!!</p>
  
  <p>P.S: citire reads the array, prim detects if a number is prime or composed, afisare displays the subsequence and detSecv determines the longest subsequence.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;math.h&gt;
  
  using namespace std;
  
  void citireSecv(int &amp;n,int x[50])
  {
      cout&lt;&lt;"Da n: ";
      cin&gt;&gt;n;
      for(int i=1;i&lt;=n;i++)
      {
          cout&lt;&lt;"Da un nr: ";
          cin&gt;&gt;x[i];
      }
  }
  
  int prim(int n)
  {
      int d=2;
      while(d&lt;=sqrt(n) &amp;&amp; n%d!=0)
      {
          if(d==2)
              d=3;
          else
              d=d+2;
      }
      if(d&gt;sqrt(n)) return 1;
      else          return 0;
  }
  
  void afisare(int n,int x[50],int st,int f)
  {
      for(int i=st;i&lt;=f;i++)
          cout&lt;&lt;x[i]&lt;&lt;" ";
  }
  
  void detSecv(int n,int x[100],int &amp;st,int &amp;f)
  {
      st=1; f=0;
      int i=1,j;
      while(i&lt;=n-1)
      {
          while(i&lt;=n-1)
          {
              if(prim(x[i])==0 &amp;&amp; prim(x[i+1])==0) i++;
          }
          j=i+1;
          while(j&lt;=n-1)
              if(prim(x[j])==0 &amp;&amp; prim(x[j+1])==0) j++;
          if((j-i) &gt; (f-st))
          {
              st=i;
              f=j;
          }
          i=j+1;
      }
  
  }
  
  
  
  int main()
  {
      int n,x[100],st,f;
      citireSecv(n,x);
      detSecv(n,x,st,f);
      afisare(n,x,st,f);
      return 0;
  }
  </code></pre>
  
  <p>Input data:</p>
  
  <pre><code>n=2
  First number is: 5
  Second number is: 7
  </code></pre>