Ribbon Pattern??

So I was given a problem (helping a friend), and its says there to generate the ribbon depending on the number I input.
Like this for example:

if I inputted mySize = 5 it must be like this (depends on what number I entered).
PS: The periods(".") should be spaces(" ").

*......*
**..**
*****
**..**
*.....*
I can't seem to find out how I'll use nested loop here. Thanks in advance!
Last edited on
It's not at all clear what the algorithm is supposed to be for generating the diagram.
You say the diagram is for a size of 5, but what you have drawn has lines of 8,6,5,6,8 characters. How do those values relates to a size of 5?

Before you can design the loops you need, you need to understand the the algorithm.

And since you haven't described the algorithm to us, we can't make any suggestions.

Last edited on
It looks like you are supposed to draw mirrored triangles.

If so the algorithm for the left is:

Increase by one the number of asterisk to print each line until the middle then (maybe using a second loop) decrease.

For the mirrored trinagle you need to calculate the spaces. Which is

number_of_spaces_per_line = max_asterisks (in this case = 5) - number_of_asterisk_per_line * 2

So you have two outer loops (that iterates max_asterisks / 2) and three inner loops.

draw ->
number_of_asterisk_per_line
number_of_spaces_per_line
number_of_asterisk_per_line
@MDLC1997: do you mean like this:

For 5:
*   *
** **
*****
** **
*   *


For 9:
*       *
**     **
***   ***
**** ****
*********
**** ****
***   ***
**     **
*       *


I'd call that a bow-tie, not a ribbon.

i-j nested loop: for each element just decide whether to draw a blank or an asterisk.


Suppose that you count down the page i (1 <= i <= size)
and inside that you count across the page j (1 <= j <= size). This is the nested loop.

The two diagonals are j = i and j = size+1-i. On each line (i), if the distance across the page (j) is less-than-or-equal-to the min() or greater-than-or-equal-to the max() of those diagonals then output an asterisk; otherwise output a blank. Make sure that you end a line after the inner loop.

That's assuming I inferred your shape correctly!
Last edited on
Topic archived. No new replies allowed.