Can this asterisk pyramid code be further simplified?

Asked

Viewed 2,598 times

9

I have a simple exercise where I need to do an asterisk pyramide using repeating structure.

The pyramid has 17 columns and 9 rows.

My program displays it correctly, It is possible to reduce some line or some for?

#include <stdio.h>
#include <conio.h>


main()


{
    int l,c,e;


    for (l = 1; l <= 10; l = l + 1)
    {
        for(e=1; e<=(l-1);e = e + 1)
        {
            printf(" ");
        }
        for (c = 1; c <= 10-l; c = c + 1)
        {
            printf("*");
        }

        for (c = 2; c <= 10-l; c = c + 1)
        {
            printf("*");
        }
    printf("\n");
    }
getch;
}
  • 2

    by the way, avoid having variables with the name l - is a symbol difficult to distinguish visually from 1 and makes reading your code harder. (In the case of the source here to stackoverflow, the symbol is virtually identical to 1 - but even with other sources is pretty bad)

2 answers

17


An alternative:

#include <stdio.h>
int main() {
    int l, c;
    for (l=1; l<10; l++) {
        for(c=0; c<=7+l; c++)
            printf(c<9-l?" ":"*");
        printf("\n");
    }
}

See working on IDEONE.

10

Does it give:

#include <stdio.h>

int main() {
    for (int x = 9; x > 0; x--) {
        for(int y = 9 - x; y > 0; y--) printf(" ");
        for (int y = 1; y <= x * 2 - 1; y++) printf("*");
        printf("\n");
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

In addition to eliminating one of the ties that only repeated what the other did, I gave an organized and modernized in the code. I preferred to invert the loop count and use easier to read variable name.

Do not use conio.h. Is obsolete.

But if you want to make it simpler and use a loop, you can do this:

int main(void) {
    for (int x = 0; x < 1; x++) {
        printf("*****************\n");
        printf(" ***************\n");
        printf("  *************\n");
        printf("   ***********\n");
        printf("    *********\n");
        printf("     *******\n");
        printf("      *****\n");
        printf("       ***\n");
        printf("        *\n");
    }
}

When less deviations and variables and changes of state, simpler, even if it is not the shortest.

If you still want the shortest you have a few tricks possible. An option:

#include <stdio.h>
int main(void) { for (int x = 9; x > 0; x--) printf("%.*s%.*s\n", 9 - x, "         ", x * 2 - 1, "*****************"); }

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • 1

    I liked this last option too much, it seems like a joke but I once heard that: to do something efficient sometimes we need to "think like a lazy person". Thus the first 3 algorithms solve the problem effectively but the last was the most efficient.

  • 3

    @iTSangar I would present this one for who asked the exercise to let go of being a beast :) Until I understand the objective, but artificial requirements deserve creative solutions to circumvent them. The guy has to learn how to do the requisite to force the real bond and not artificially. I kept doing this with my teacher who ordered "create functions that only had a responsibility", it was not his intention, but I delivered practically an Assembly, had things like Soma(x,y){ return x+y}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.