#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int i, rows = 0, row_nr;
cin >> row_nr;
for (i = 1; i <= 9; i++)
{
rows++;
cout << setw(3) << i << " ";
if (rows % row_nr == 0)
cout << endl;
}
return 0;
}
Worked with this code but instead of showing result
1 4 7
2 5 8
3 6 9
it shows me this
1 2 3
4 5 6
7 8 9
your loop boils down to:
for i = 1 to 9
cout i
with an end of line every 3.
so that is, indeed,
123.. etc
one way out is to just be cheezy with it
for(1 .. 3)
{
cout << i << i+3 << i+6 << endl;
}
a double loop can do it, if you have a more complicated need.
a complicated single loop with more variables can also do it.
Last edited on