quick and simple

Discussion in 'C Programming' started by Bruce Wiggins, May 12, 2010.

  1. I did not put much thought into this but it works.

    int i;
    int x;
    int j=4;

    for(i=0; i < 9; i+=2)
    {
    for(x=0; x < j; x++) printf(" ");
    j--;
    for(x=0; x <= i; x++)
    printf("*");
    printf("\n");
    }

    j=0;
    for(i=9; i > 0; i-=2)
    {
    for(x=0; x < j; x++) printf(" ");
    j++;
    for(x=0; x < i; x++)
    printf("*");
    printf("\n");
    }


    output:

    *
    ***
    *****
    *******
    *********
    *********
    *******
    *****
    ***
    *
    Bruce Wiggins, May 12, 2010
    #1
    1. Advertising

  2. On 2010-05-12, Bruce Wiggins <> wrote:
    > int i;
    > int x;
    > int j=4;
    >
    > for(i=0; i < 9; i+=2)
    > {
    > for(x=0; x < j; x++) printf(" ");
    > j--;
    > for(x=0; x <= i; x++)
    > printf("*");
    > printf("\n");
    > }
    >
    > j=0;
    > for(i=9; i > 0; i-=2)
    > {
    > for(x=0; x < j; x++) printf(" ");
    > j++;
    > for(x=0; x < i; x++)
    > printf("*");
    > printf("\n");
    > }


    I can't quite see the point of this (a reference to some other thread?),
    but for some inexplicable reason couldn't resist applying some recursion
    to it:

    #include <stdio.h>

    void d1(int s, int n) {
    if (s) {
    putchar(' ');
    d1(s-1, n);
    putchar(' ');
    } else if (n) {
    putchar('*');
    d1(s, n-1);
    }
    }

    void d2(int s, int n) {
    d1(s, n); putchar('\n');
    if (s) d2(s-1, n+2);
    d1(s, n); putchar('\n');
    }

    int main(void) {
    d2(4, 1);
    return 0;
    }


    --
    Heikki Kallasjoki
    email: echo 'zfs+es_t_i@n_u.zf' | tr zen_muftis fuze_mints
    Heikki Kallasjoki, May 12, 2010
    #2
    1. Advertising

  3. On 2010-05-12 09:44, Bruce Wiggins wrote:
    > I did not put much thought into this but it works.

    [...]

    Here is a shorter solution:

    #include <stdio.h>

    int main(void)
    {
    const int n = 9;
    int i, j, inside;

    for (i = 0; i < n + 1; i++) {
    for (j = 0; j < n; j++) {
    inside = (i + j >= n / 2) && (i + j < 3 * n / 2 + 1)
    && (j - i >= -n / 2 - 1) && (j - i < n / 2 + 1);
    printf("%c ", inside? '*': ' ');
    }
    printf("\n");
    }
    return 0;
    }


    August
    August Karlstrom, May 12, 2010
    #3
  4. Bruce Wiggins

    Guest

    August Karlstrom <> wrote:
    > On 2010-05-12 09:44, Bruce Wiggins wrote:
    > > I did not put much thought into this but it works.

    > [...]
    >
    > Here is a shorter solution:
    > [...]


    #include <stdio.h>

    #define ABS(i) ((i) < 0 ? -(i) : (i))

    int main()
    {
    int i;

    for (i = -4; i <= 4; i++)
    printf("%.*s\n", 9 - ABS(i), " *********" + 4 - ABS(i));
    return 0;
    }
    --
    Larry Jones

    I don't NEED to compromise my principles, because they don't have
    the slightest bearing on what happens to me anyway. -- Calvin
    , May 12, 2010
    #4
  5. On 2010-05-12 18:20, wrote:
    > #include<stdio.h>
    >
    > #define ABS(i) ((i)< 0 ? -(i) : (i))
    >
    > int main()
    > {
    > int i;
    >
    > for (i = -4; i<= 4; i++)
    > printf("%.*s\n", 9 - ABS(i), " *********" + 4 - ABS(i));
    > return 0;
    > }


    Nice! Didn't know printf had these formatting capabilities. But you
    don't get the same output as the OP with this program.


    August
    August Karlstrom, May 12, 2010
    #5
  6. Bruce Wiggins

    Mark Guest

    August Karlstrom <> wrote:
    > On 2010-05-12 18:20, wrote:
    >> #include<stdio.h>
    >>
    >> #define ABS(i) ((i)< 0 ? -(i) : (i))
    >>
    >> int main()
    >> {
    >> int i;
    >>
    >> for (i = -4; i<= 4; i++)
    >> printf("%.*s\n", 9 - ABS(i), " *********" + 4 - ABS(i));
    >> return 0;
    >> }

    >
    > Nice! Didn't know printf had these formatting capabilities. But you
    > don't get the same output as the OP with this program.


    for (i=-5; i<=5; i++)
    if(i)
    printf("%.*s\n", 10 - ABS(i), " **********" + 5 - ABS(i));
    Mark, May 12, 2010
    #6
  7. Bruce Wiggins

    Guest

    August Karlstrom <> wrote:
    >
    > Nice! Didn't know printf had these formatting capabilities.


    That was only the half of it...this version goes all the way:

    #include <stdio.h>

    #define ABS(i) ((i)< 0 ? -(i) : (i))

    int main()
    {
    int i;

    for (i = -4; i<= 4; i++)
    printf("%*.*s\n", 9 - ABS(i), 9 - 2 * ABS(i), "*********");
    return 0;
    }

    > But you don't get the same output as the OP with this program.


    I agree with the other poster who said that it looks better without the
    duplicated line, but it's an easy change if you want it.
    --
    Larry Jones

    He just doesn't want to face up to the fact that I'll be
    the life of every party. -- Calvin
    , May 13, 2010
    #7
  8. Bruce Wiggins

    bart.c Guest

    "Bruce Wiggins" <> wrote in message
    news:...
    >I did not put much thought into this but it works.
    >
    > int i;
    > int x;
    > int j=4;
    >
    > for(i=0; i < 9; i+=2)

    ....

    > for(i=9; i > 0; i-=2)

    ....

    > output:
    >
    > *
    > ***
    > *****
    > *******
    > *********
    > *********
    > *******
    > *****
    > ***
    > *


    This version doesn't use any loops or variables, and avoids that duplicated
    middle line:

    #include <stdio.h>

    int main(void)
    {
    puts(" *");
    puts(" ***");
    puts(" *****");
    puts(" *******");
    puts("*********");
    puts(" *******");
    puts(" *****");
    puts(" ***");
    puts(" *");
    }

    --
    Bartc
    bart.c, May 13, 2010
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Fox

    Quick Simple Question

    Mark Fox, Aug 22, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    302
    Jacob Yang [MSFT]
    Aug 25, 2003
  2. will
    Replies:
    1
    Views:
    359
    Xavier Franc
    Sep 17, 2003
  3. JKop
    Replies:
    11
    Views:
    850
  4. Pierre Couderc
    Replies:
    1
    Views:
    344
    Victor Bazarov
    Jul 29, 2005
  5. gregNelson
    Replies:
    0
    Views:
    286
    gregNelson
    Feb 27, 2009
Loading...

Share This Page