Code:
#include <iostream>
#include <iomanip>
using namespace std;
void box(int n) {
int j = -1, k = -1;
for (int i = 0; i < n * n; ++i) {
if (j++ == n - 1) {
k = -1;
j = 0;
cout << endl;
} else if (j == i / n) {
k = 0;
} else if (j > i / n) {
k = 1;
}
cout << setw(2) << k << ' ';
}
}
int main() {
int n;
cout << "sides = " << flush;
cin >> n;
box(n);
return 0;
}
Prints:
Code:
sides = 5
0 1 1 1 1
-1 0 1 1 1
-1 -1 0 1 1
-1 -1 -1 0 1
-1 -1 -1 -1 0
Except its the other way around! One loop.
EDIT: Darn I wanted to not give him the correct code so I did it the opposite way. Oh well. Its an easy fix to make it the other way.