# Thread: Printing a pattern to the screen

1. ## Printing a pattern to the screen

Hi everybody. I'm in my first programming course ever and have a few questions about an assignment that we've been given.

I'm trying to print the following pattern to the screen: The pattern is supposed to contain 5 rows and each subsequent row has 2 additional asterisks from the row above making roughly a pyramid shape.

I've been working on creating code to do this using For loops (this was part of the instructions) and here's what I have so far:

Code:
```#include <stdio.h>

int main ()

{
int row;
int col;

for (row = 1; row <= 5; row++) //rows
{
for (col = 1; col <= row; col++) //columns
{
printf_s("*");
}

printf_s("\n");
}
return 0;
}
```

The problem with my code is that I am not accounting for the required empty spaces to get the alignment correct. With the above current code here's what the output looks like: I'm hoping someone can point me in the right direction as to how to re-write my code to get the correct alignment. 2. I guess you can always say that the point of the pyramid is always in the middle, and the middle is the base / 2, right?
I guess that gives you a starting ammount of spaces, then it decreases linearly no?

Maybe try to get the right ammount of asterisks per row first (right now you have only half), and then we'll see about the spaces. 3. Originally Posted by Fiskker I guess you can always say that the point of the pyramid is always in the middle, and the middle is the base / 2, right?
I guess that gives you a starting ammount of spaces, then it decreases linearly no?

Maybe try to get the right ammount of asterisks per row first (right now you have only half), and then we'll see about the spaces.
I think I also neglected to mention that I am also struggling to find the right code that will also give me the correct number of asterisks per row. 4. Originally Posted by PhantomJoe I think I also neglected to mention that I am also struggling to find the right code that will also give me the correct number of asterisks per row.
Oh, okay.

Well, you were fine by looping each row, and then every column, and printing an asterisk until the row was matched, that is:

1 asterisk in first row.
2 asterisks in second row.
.
n asterisks in n rows.

Now you need:
1 asterisk in first row
3 asterisks in second row.
.
(2n-1) asterisks in n row.

So your condition is now not the ammount of rows, but the double minus one, do you see this? If not I can try and explain it a bit more. 5. Like so
Code:
```for (row = 1; row <= 5; row++) //rows
int numspaces = ?
for ( s = 0 ; s < numspaces ; s++ ) printf(" ");```
Look at your diagram, and figure out a numeric expression for numspaces as a function of the row number. 6. Originally Posted by Fiskker Oh, okay.

Well, you were fine by looping each row, and then every column, and printing an asterisk until the row was matched, that is:

1 asterisk in first row.
2 asterisks in second row.
.
n asterisks in n rows.

Now you need:
1 asterisk in first row
3 asterisks in second row.
.
(2n-1) asterisks in n row.

So your condition is now not the ammount of rows, but the double minus one, do you see this? If not I can try and explain it a bit more.
Mathematically I understand your logic. 2*rows -1 will give me the correct number of asterisks per row (2*1-1=1 and 2*2-1=3 and so on) but how do I account for the leading blank spaces? 7. Originally Posted by Salem Like so
Code:
```for (row = 1; row <= 5; row++) //rows
int numspaces = ?
for ( s = 0 ; s < numspaces ; s++ ) printf(" ");```
Look at your diagram, and figure out a numeric expression for numspaces as a function of the row number.
In the first row I need 3 leading spaces, in the second row I need 2 leading spaces, and in the third row I need 1 leading space (and no leading spaces for the 4th row). I'll be honest, I'm really struggling to find an expression for numspaces as a function of the row number. Am I overlooking something obvious? 8. Originally Posted by PhantomJoe Mathematically I understand your logic. 2*rows -1 will give me the correct number of asterisks per row (2*1-1=1 and 2*2-1=3 and so on) but how do I account for the leading blank spaces?
But I want to see how you code it, what would you do?
What needs to be changed in the for loop that loops the columns, what is the new condition? Originally Posted by PhantomJoe In the first row I need 3 leading spaces, in the second row I need 2 leading spaces, and in the third row I need 1 leading space (and no leading spaces for the 4th row). I'll be honest, I'm really struggling to find an expression for numspaces as a function of the row number. Am I overlooking something obvious?
Okay so you are describing it, how do we code something that prints decreasingly?
We call the print function less and less overtime until a limit is reached, you could use a loop to do this, similar to what you've done so far.

Mathematically:
F(rows) = spaces
F(1) = 3
F(2) = 2
F(3) = 1
F(4) = 0

F(x) = ? 9. Originally Posted by Fiskker But I want to see how you code it, what would you do?
What needs to be changed in the for loop that loops the columns, what is the new condition?

Okay so you are describing it, how do we code something that prints decreasingly?
We call the print function less and less overtime until a limit is reached, you could use a loop to do this, similar to what you've done so far.

Mathematically:
F(rows) = spaces
F(1) = 3
F(2) = 2
F(3) = 1
F(4) = 0

F(x) = ?
Lol, so I've tried all kinds of changes to the for loop for columns but I either wind up printing a single asterisk or I create an infinite loop with infinity asterisks. 10. Hello PhantomJoe, I passive some sense of anxiety and frustration, because the solution has not been made clear to you. I have being faced with similar problems. Calm down and take a break from the challenge engage in something that relaxes you and later on come back to the task and review all the suggestions provided by forum members. I bet you will surmount the challenge before you. Take is slow with your approach and have some patience with your self. 11. Originally Posted by PhantomJoe Lol, so I've tried all kinds of changes to the for loop for columns but I either wind up printing a single asterisk or I create an infinite loop with infinity asterisks.
I understand, but if you don't provide information about what you are doing and what the problem is, meaning what exactly is happening with your current code, then it becomes hard to see what the mistake is and how to correct it. 12. Originally Posted by Fiskker I understand, but if you don't provide information about what you are doing and what the problem is, meaning what exactly is happening with your current code, then it becomes hard to see what the mistake is and how to correct it.
I know I need to incorporate the logic you laid out to get the correct number of asterisks (row*2-1) but because I'm unsure of how to implement that I've just been trying to insert it in different places in the inner loop.

In the below example I've used it in the inner loop in the test condition portion and that produces an infinite loop.

Code:
```int main()
{
int row;
int col;

for (row = 1; row <= 4; row++) //rows
{
for (col = 1; col = row*2-1; col++) //columns
{
printf_s("*");
}

printf_s("\n");
}

return 0;
}```
When I use it in the initiate location of the inner loop I get just a single asterisk (code below):
Code:
```int main()
{
int row;
int col;

for (row = 1; row <= 4; row++) //rows
{
for (col = row*2-1; col <= row; col++) //columns
{
printf_s("*");
}

printf_s("\n");
}

return 0;
}```

I also want to say thank you for your help and I'm trying to not get frustrated but as this is my first ever exposure to programming I sometimes feel like I'm in over my head. 13. You're still trying to solve too much of the problem at once.
Code:
```for (row = 1; row <= 5; row++) {
int numspaces = ?
printf("Numspaces=%d\n",numspaces);
}```
Just figure out how to make this print
Numspaces=4
Numspaces=3
Numspaces=2
Numspaces=1
Numspaces=0

As soon as you do, the rest falls into place. 14. Originally Posted by Salem You're still trying to solve too much of the problem at once.
Code:
```for (row = 1; row <= 5; row++) {
int numspaces = ?
printf("Numspaces=%d\n",numspaces);
}```
Just figure out how to make this print
Numspaces=4
Numspaces=3
Numspaces=2
Numspaces=1
Numspaces=0

As soon as you do, the rest falls into place.
Hi Salem. So I think I figured out the right code to get your above output:
Code:
```int main()
{
int row;
int col;
int numspaces;

for (row = 4; row >= 0; row--)
{
printf_s("Numspaces=%d\n", numspaces = row);
}
printf_s("\n");

return 0;
}```
And it prints the following: I'm sorry to be so dense but I'm not sure how to connect this to the assignment? 15. I hope this helps more than hinders,
Code:
```#include <stdio.h>

int main ()

{
int row;
int col;
int space = 16;

for (row = 1; row <= 5; row++) //rows
{
for (col = 1; col <= row ; col++) //columns
{
//printf("*");
//printf("%d",space);
printf("%*c",space,'*');
}
space--;
printf("\n");
}
return 0;
}```
but it does demonstrate how to get the star moved over before printing, it is not an complete answer. Popular pages Recent additions code, int, pattern, row, screen 