Willze Fortner - 1 year ago 51
C++ Question

# Create A Number Pattern

I am trying to get this number pattern

``````Input: 7
Output:
1 1 1 1 1 1 1
1 2 2 2 2 2 1
1 2 3 3 3 2 1
1 2 3 4 3 2 1
1 2 3 3 3 2 1
1 2 2 2 2 2 1
1 1 1 1 1 1 1
``````

But I can't figure out how to make it like that, any suggestion how to make that pattern??

My code so far :

``````int n, temp1, temp2,i,j;
cin >> n;
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
temp1 = j;
temp2 = n-j+1;
if (temp1 < temp2) cout << temp1;
else cout << temp2;
}
cout << endl;
}
``````

The output so far is

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

I hope this code(working) might give you a better idea about the implementation.

``````int main() {

int n;
cin >> n;
int arr[n][n];

//Numbers in the grid vary from  1 - (n/2 + 1)
for(int i = 0; i <= n / 2; i++) {
//Start filling the array in squares
//Like fill the outer square with 1 first followed by 2...
for(int j = i; j < n - i; j++) {

arr[i][j] = i + 1;
arr[n - 1 - i][j] = i + 1;
arr[j][i] = i + 1;
arr[j][n - 1 - i] = i + 1;
}
}
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download