I looked up in many places and tried to understand how to get arbitrary number of nested for loops via recursion. But what I have understood is clearly wrong.

**I need to generate coordinates in an n-dimensional space**, in a grid-pattern. The actual problem has different coordinates with different ranges, but to get simpler things right first, I have used the same, integer-stepped coordinate ranges in the code below.

```
#include <iostream>
using namespace std;

void recursion(int n);

int main(){    
  recursion(3);
  return 0;
}

void recursion(int n)
{
  if(n!=0){
    for(int x=1; x<4; x++){
        cout<<x<<" ";
        recursion(n-1);
  }
}
  else cout<<endl;
}  

```

I want, and was expecting the output to be:

```
1 1 1
1 1 2
1 1 3
1 2 1
1 2 2
1 2 3
1 3 1
1 3 2
1 3 3
2 1 1
2 1 2
2 1 3
2 2 1
2 2 2
2 2 3
2 3 1
2 3 2
2 3 3
3 1 1
3 1 2
3 1 3
3 2 1
3 2 2
3 2 3
3 3 1
3 3 2
3 3 3

```

Instead, the output I'm getting is

```
1 1 1 
2 
3 
2 1 
2 
3 
3 1 
2 
3 
2 1 1 
2 
3 
2 1 
2 
3 
3 1 
2 
3 
3 1 1 
2 
3 
2 1 
2 
3 
3 1 
2 
3 

```

I just can't figure out whats wrong. Any help to figure out the mistake or even another way to generate coordinates will be greatly appreciated. Thanks!