OK, attempt 2 based on your updated question. Let's start with the basic structure. Set some consts for the width and height - we will refer to these in multiple places, so consts make for nice easy changes - and create a loop with basic logic to start to get the display you want.
const int numRows = 5;
const int numCols = 21;
for (int row = 0; row < numRows; ++row)
{
for (int col = 0; col < numCols; ++col)
{
if (col == row)
{
printf("X");
}
else
{
printf(" ");
}
}
printf("\n");
}
This gives us:
X
X
X
X
X
which is the wrong way up and only gives us one "downstroke", but it is a good start. Let's use the modulus function to make the pattern repeat. The repeat happens on the 9th character, which suggests we need to use ((numRows - 1) * 2) for our modulus.
const int modulusVal = ((numRows - 1) * 2);
for (int row = 0; row < numRows; ++row)
{
for (int col = 0; col < numCols; ++col)
{
int modCol = (col % modulusVal);
if (modCol == row)
{
printf("X");
}
else
{
printf(" ");
}
}
printf("\n");
}
So now we get:
X X X
X X X
X X X
X X X
X X X
which is starting to get a lot closer to what we want. So, on to the upstrokes. The downstrokes are displayed when modCol is in the range of row, which is 0-4. When modCol is past this range, we bring it back into range by subtracting the number of rows from it.
Then we "invert" it by subtracting it from the highest value that row can be, which is numRows - 1. This means that when modCol is 0 it will become 4, when it is 1 it will become 3, when it is 2 it will be unchanged.
for (int row = 0; row < numRows; ++row)
{
for (int col = 0; col < numCols; ++col)
{
int modCol = (col % modulusVal);
if (modCol >= numRows)
{
modCol -= numRows;
modCol = ((numRows - 1) - modCol);
}
if (modCol == row)
{
printf("X");
}
else
{
printf(" ");
}
}
printf("\n");
}
Now we get:
X X X
X X X
X X X X X
X X X X X
XX XX X
Close, but no cigar. Column 5 is being treated as column 0, but we need it to be treated as column 1. Make this change to fix it:
modCol = ((numRows - 1) - (modCol + 1));
Output:
X X X
X X X X X
X X X X X
X X X X X
X X X
This has the pattern we want, but starts at the top left when we want the bottom left. Easy fix. Invert modCol after it has been calculated in the same we we invert it when it is greater/equal than/to numRows.
for (int row = 0; row < numRows; ++row)
{
for (int col = 0; col < numCols; ++col)
{
int modCol = (col % modulusVal);
if (modCol >= numRows)
{
modCol -= numRows;
modCol = ((numRows - 1) - (modCol + 1));
}
modCol = ((numRows - 1) - modCol);
if (modCol == row)
{
printf("X");
}
else
{
printf(" ");
}
}
printf("\n");
}
Finally we get the output we want:
X X X
X X X X X
X X X X X
X X X X X
X X X
I hope this helps and is reasonably easy to understand.