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;
}