I'm trying to print out a hollow, open tent shape using asterisk stars "*". The code uses two for loops, the first for the rows, and the other for the columns.
following is my code:
void printTent(int n)
{
int j = 1;
int i = 1;
if (n == 1) {
printf("*");
} else {
for(i = 0; i < n; i++) {
for(j = 0; j < n; j++) {
printf(" ");
}
if(j == n) {
printf("*");
for(j = 1; j <= n; j++) {
printf(" ");
}
}
}
}
}
int main()
{
printTent(4);
}
Output obtained:
* * * *
Desired output:
*
* *
* *
* *
I don't think you will need that
We can take care of that in what you've written in the
elsepart.For
n=4, the number of spaces to be printed at the start of each line is 3, 2, 1 & 0.You seem to be trying to accomplish that with your first inner loop. But
will always print
nspaces. We need to reduce the number of spaces printed by1on each iteration of the outer loop.Coming to your second loop,
This has a similar problem only difference being the incrementation of the number of spaces printed.
Try something like this
Also, you could shorten this to
The
*in%*cwill set the number of places occupied by the character printed by the%c.