0

Edit: Corrected Code (Thanks for the answers and help!)

#include <iostream>
using namespace std;

int arr1(const int n,int i, int j){
  if(j != 0) {
    /* if(i == 2*n ){
    cout<<"\n";
    --j;}*/
   if((i <=  (n-j) || (i >= (j+n)) && i <2*n)){
    cout<<" ";
   }
  if(i < n && i > (n-j)){
    cout<<"\\";
  }
  if( i > n && i < (n+j)){
    cout<<"/";

  }
  if(i == n){
    cout<<"v";

  }
  if(i == 2*n ){
    cout<<"\n";
    i = 0;
    --j;}
  return arr1(n,++i,j);

}
  return 0;
}

  int main(){
    int c;
    cin>>c;
    arr1(c,1,c);

  }

I'm trying to write a program that takes an integer n and recursively prints an arrowhead type design

n=1 ->  v
n=2 ->  \v/
         v

n=3 ->  \\v//
         \v/
          v

etc:

This is my code so far, yet I keep getting a segmentation error. I assume this because of an infinite loop somewhere in the code.

#include <iostream>
using namespace std;

int arr1(const int n, int i, int j)
{
    if (j != 0)
    {
        if (i == 2 * n)
        {
            cout << "\n";
            --j;
        }
        if (i <= n - j || i >= j + n)
        {
            cout << "_";
        }
        if (i < j)
        {
            cout << "\\";
        }
        if (i > j)
        {
            cout << "/";

        }
        if (i == n)
        {
            cout << "v";

        }
        return arr1(n, ++i, j);

    }
    return 0;
}

int main()
{
    int c;

    cin >> c;
    arr1(c, 1, c);

    return 0;
}
user2821771
  • 13
  • 1
  • 1
  • 5
  • Evidently, `j` never becomes zero? Or at least not soon enough before you have a stack overflow due to excessive recursion of your `arr1` function. – lurker Sep 27 '13 at 02:29
  • So, I added this: if(i == 2*n ){ cout<<"\n"; return arr1(n,1,--j);} Now it prints, but there is still a formatting error. – user2821771 Sep 27 '13 at 02:40
  • Why does the decrement on j I have in the original code I posted not change the value of j? My thought was that it would decrement j and then reach the recursive step. part of my problem was also that I did not reset the counter i after j was changed. – user2821771 Sep 27 '13 at 02:45
  • Wow, yes, other than those few mistakes, my logic was completely off too. Just fixed it. Thank you very much! – user2821771 Sep 27 '13 at 03:17

2 Answers2

0

The decision of whether to recurse comes down to the value of j -- but you receive j from main, and never modify it afterwards unless i==2*n, passing the exact same value when it calls itself recursively. So yes, that leads to infinite recursion (assuming you originally pass a non-zero value for j, anyway).

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
0

You logic in the arr1 function is wrong, for example, if c=3 in the beginning,

the i, j, n values in arr1 will be in each iteration

i = 1 j = 3  n = 3
i = 2 j = 3  n = 3
i = 3 j = 3  n = 3
i = 4 j = 3  n = 3
i = 5 j = 3  n = 3
i = 6 j = 2  n = 3
i = 7 j = 2  n = 3
i = 8 j = 2  n = 3
i = 9 j = 2  n = 3
i = 10 j = 2  n = 3
i = 11 j = 2  n = 3

...

then it goes for ever until stack overflow.

CS Pei
  • 10,869
  • 1
  • 27
  • 46