0

hi. i am marsh a student

guys i have a activity but i don't know to start it with a loop. can you help me?

***** *****
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
***** *****

Edited by max2011: n/a

5
Contributors
4
Replies
23
Views
6 Years
Discussion Span
Last Post by Gonbe
0

hi. i am marsh a student

guys i have a activity but i don't know to start it with a loop. can you help me?

***** *****
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
***** *****

This is actually easier than the corresponding filled diamond because it's more obvious that you have to print the spaces to get a proper result. In this case, you can break the problem down to the smaller relevant part:

*****
****-
***--
**---
*----
**---
***--
****-
*****

I used dashes instead of spaces once again to make it clear that the whitespace is significant and needs to be printed. This problem is much easier, but once you do it, you can solve the whole problem because it's a simple matter of adding a single space and then doing the same thing in reverse to get the full result:

// Add a space for the center column
*****-
****--
***---
**----
*-----
**----
***---
****--
*****-
// Print the same line in reverse
*****-*****
****---****
***-----***
**-------**
*---------*
**-------**
***-----***
****---****
*****-*****

Done.

-1

what is the code to display "*" as pyramid?

#include <stdio.h>

static void print_n(const char symbol, unsigned int n)
{
    while (n --> 0)
        printf("%c", symbol);
}

void print_pyramid(const unsigned int height)
{
    int       i           = 0,
              width       = 0;
    const int WIDTH_TOTAL = (2 * height) - 1;

    for (i = 1; i <= height; i++)
    {
        width = (2 * i) - 1;

        print_n(' ' , (WIDTH_TOTAL - width) / 2);
        print_n('*' , width);
        print_n('\n', 1);
    }
}

int main(void)
{
    print_pyramid(10);
    return 0;
}
Comments
-1 for being an idiot
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.