Hi,
my aim is to print this pattern:
o----
-o---
--o--
---o-
----o
where n=5 is a side length of the square, x=3 is how many times this square has to be printed horizontally.
So the result for n=5, x=3 should be:
o----o----o----
-o----o----o---
--o----o----o--
---o----o----o-
----o----o----o
This is the code for the square, easy as it is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
int main()
{
int n=5;
for(int row=0; row<n; ++row)
{
for(int col=0; col<n; ++col)
{
if(col==row)
cout << "o";
else
cout << "-";
}
cout << endl;
}
return 0;
}
|
I know how to print this pattern a few times vertically, but my real problem is how to do this horizontally - what I see from the debugger is that the program is built from the left side of the line to the right side and then there is a new line, so I am confused with what kind of conditions I should print "o".
All I have, but it's not working fully:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
int main()
{
int n=5;
int x=3;
int i=0;
for(int row=0; row<n; ++row)
{
for(int col=0; col<x*n; ++col)
{
if(col==n*i || col==row) //MISSING CONDITION?
{
cout << "o";
++i;
}
else
cout << "-";
}
cout << endl;
}
return 0;
}
|
Please, is there a simple way that I'm missing? Could anyone help me?
Thanks so much.