Is there any suggestion to rewrite these simple codes?

Discussion in 'C Programming' started by Junmin H., Sep 16, 2007.

  1. Junmin H.

    Junmin H. Guest

    Hello, just wrote down 4 simple codes to print numbers with order
    for practice in C.

    Just would like to know if you guys have any suggestion about them.
    thanks

    Junmin


    =1=1=1=1=1=1=1=1=1=1=1=1=1=1=1=1=1=1=1=1=1=1=1=
    #include <stdio.h>

    main(void){
    int i = 0;
    while(i < 9){
    int j = 0;
    while(j < i){
    printf("- ");
    ++j;
    }
    int x = ++i;
    while(x <= 9){
    printf("%d ",x);
    ++x;
    }
    printf("\n");
    }
    return 0;
    }

    OUTPUT:
    junmin@T61Gentoo ~/C $ ./number
    1 2 3 4 5 6 7 8 9
    - 2 3 4 5 6 7 8 9
    - - 3 4 5 6 7 8 9
    - - - 4 5 6 7 8 9
    - - - - 5 6 7 8 9
    - - - - - 6 7 8 9
    - - - - - - 7 8 9
    - - - - - - - 8 9
    - - - - - - - - 9

    =2=2=2=2=2=2=2=2=2=2=2=2=2=2=2=2=2=2=2=2=2=2=
    #include <stdio.h>

    main(void){
    int i = 0;
    int k = 9;
    while(i < 5){
    int j = 0;
    while(j < i){
    printf("- ");
    ++j;
    }
    int x = ++i;
    while(x <= k){
    printf("%d ", x);
    ++x;
    }
    --k;
    printf("\n");
    }
    return 0;
    }

    OUTPUT:
    junmin@T61Gentoo ~/C $ ./number2
    1 2 3 4 5 6 7 8 9
    - 2 3 4 5 6 7 8
    - - 3 4 5 6 7
    - - - 4 5 6
    - - - - 5

    =3=3=3=3=3=3=3=3=3=3=3=3=3=3=3=3=3=3=3=3=3=3=3=3=3=3=
    #include <stdio.h>

    main(void){
    int i;
    for(i = 0; i < 9; i++,printf("\n")){
    int j = 1;
    while(j <= 9){
    printf("%d ",j);
    if(j == i){
    printf("+");
    }
    ++j;
    }
    }
    return 0;
    }

    OUTPUT:
    junmin@T61Gentoo ~/C $ ./number3
    1 2 3 4 5 6 7 8 9
    1 +2 3 4 5 6 7 8 9
    1 2 +3 4 5 6 7 8 9
    1 2 3 +4 5 6 7 8 9
    1 2 3 4 +5 6 7 8 9
    1 2 3 4 5 +6 7 8 9
    1 2 3 4 5 6 +7 8 9
    1 2 3 4 5 6 7 +8 9
    1 2 3 4 5 6 7 8 +9


    =4=4=4=4=4=4=4=4=4=4=4=4=4=4=4=4=4=4=4=4=4=4=4=4=
    #include <stdio.h>

    main(void){
    int i;
    for(i = 0; i < 9; i++,printf("\n")){
    int j = 1;
    while(j <= 9){
    if(j == i)
    printf("(");
    printf("%d ", j);
    if(j == i)
    printf("+");
    if(j == i + 1 && i + 1 != 1)
    printf(")");
    ++j;
    }
    }
    return 0;
    }

    OUTPUT:
    junmin@T61Gentoo ~/C $ ./number4
    1 2 3 4 5 6 7 8 9
    (1 +2 )3 4 5 6 7 8 9
    1 (2 +3 )4 5 6 7 8 9
    1 2 (3 +4 )5 6 7 8 9
    1 2 3 (4 +5 )6 7 8 9
    1 2 3 4 (5 +6 )7 8 9
    1 2 3 4 5 (6 +7 )8 9
    1 2 3 4 5 6 (7 +8 )9
    1 2 3 4 5 6 7 (8 +9 )

    ================================
     
    Junmin H., Sep 16, 2007
    #1
    1. Advertising

  2. Junmin H.

    pete Guest

    Junmin H. wrote:
    >
    > Hello, just wrote down 4 simple codes to print numbers with order
    > for practice in C.
    >
    > Just would like to know if you guys have any suggestion about them.
    > thanks
    >
    > Junmin
    >
    > =1=1=1=1=1=1=1=1=1=1=1=1=1=1=1=1=1=1=1=1=1=1=1=
    > #include <stdio.h>
    >
    > main(void){
    > int i = 0;
    > while(i < 9){
    > int j = 0;
    > while(j < i){
    > printf("- ");
    > ++j;
    > }
    > int x = ++i;
    > while(x <= 9){
    > printf("%d ",x);
    > ++x;
    > }
    > printf("\n");
    > }
    > return 0;
    > }


    Explicitly declaring main as returning type int,
    is the right way to write modern code.
    However, declaring automatic objects anywhere
    except at the begining of a compound statement,
    is a little too modern for my old compiler.
    When I first started learning C,
    I didn't want to learn any other IO functions besides printf,
    because I thought the IO library was complicated,
    but some of the other functions like putchar, are worth learning.

    I like to rewrite other people's code:

    /* BEGIN new.c */

    #include <stdio.h>

    int main(void)
    {
    int i, j, x;

    for (i = 0; 9 > i; ++i) {
    for (j = 0; i > j; ++j) {
    printf("- ");
    }
    for (x = j + 1; 9 >= x; ++x) {
    printf("%d ",x);
    }
    putchar('\n');
    }
    return 0;
    }

    /* END new.c */

    --
    pete
     
    pete, Sep 17, 2007
    #2
    1. Advertising

  3. Junmin H.

    pete Guest

    Junmin H. wrote:
    >


    > =2=2=2=2=2=2=2=2=2=2=2=2=2=2=2=2=2=2=2=2=2=2=
    > #include <stdio.h>
    >
    > main(void){
    > int i = 0;
    > int k = 9;
    > while(i < 5){
    > int j = 0;
    > while(j < i){
    > printf("- ");
    > ++j;
    > }
    > int x = ++i;
    > while(x <= k){
    > printf("%d ", x);
    > ++x;
    > }
    > --k;
    > printf("\n");
    > }
    > return 0;
    > }


    #include <stdio.h>

    int main(void)
    {
    int i, j, k, x;

    for (k = 9, i = 0; 5 > i; --k, ++i) {
    for (j = 0; i > j; ++j){
    printf("- ");
    }
    for (x = j + 1; k >= x; ++x) {
    printf("%d ", x);
    }
    putchar('\n');
    }
    return 0;
    }

    > =3=3=3=3=3=3=3=3=3=3=3=3=3=3=3=3=3=3=3=3=3=3=3=3=3=3=
    > #include <stdio.h>
    >
    > main(void){
    > int i;
    > for(i = 0; i < 9; i++,printf("\n")){
    > int j = 1;
    > while(j <= 9){
    > printf("%d ",j);
    > if(j == i){
    > printf("+");
    > }
    > ++j;
    > }
    > }
    > return 0;
    > }


    #include <stdio.h>

    int main(void)
    {
    int i, j;

    for (i = 0; 9 > i; ++i) {
    for (j = 1; 9 >= j; ++j){
    printf("%d ",j);
    if (j == i){
    putchar('+');
    }
    }
    putchar('\n');
    }
    return 0;
    }

    > =4=4=4=4=4=4=4=4=4=4=4=4=4=4=4=4=4=4=4=4=4=4=4=4=
    > #include <stdio.h>
    >
    > main(void){
    > int i;
    > for(i = 0; i < 9; i++,printf("\n")){
    > int j = 1;
    > while(j <= 9){
    > if(j == i)
    > printf("(");
    > printf("%d ", j);
    > if(j == i)
    > printf("+");
    > if(j == i + 1 && i + 1 != 1)
    > printf(")");
    > ++j;
    > }
    > }
    > return 0;
    > }


    #include <stdio.h>

    int main(void)
    {
    int i, j;

    for (i = 0; 9 > i; i++) {
    for (j = 1; 9 >= j; ++j){
    if (j == i) {
    putchar('(');
    }
    printf("%d ", j);
    if (j == i) {
    putchar('+');
    }
    if (j == i + 1 && j != 1) {
    putchar(')');
    }
    }
    putchar('\n');
    }
    return 0;
    }

    --
    pete
     
    pete, Sep 17, 2007
    #3
  4. "pete" <> a écrit dans le message de news:
    ...
    > Junmin H. wrote:
    >>
    >> Hello, just wrote down 4 simple codes to print numbers with order
    >> for practice in C.
    >>
    >> Just would like to know if you guys have any suggestion about them.
    >> thanks
    >>
    >> Junmin
    >>
    >> =1=1=1=1=1=1=1=1=1=1=1=1=1=1=1=1=1=1=1=1=1=1=1=
    >> #include <stdio.h>
    >>
    >> main(void){
    >> int i = 0;
    >> while(i < 9){
    >> int j = 0;
    >> while(j < i){
    >> printf("- ");
    >> ++j;
    >> }
    >> int x = ++i;
    >> while(x <= 9){
    >> printf("%d ",x);
    >> ++x;
    >> }
    >> printf("\n");
    >> }
    >> return 0;
    >> }

    >
    > Explicitly declaring main as returning type int,
    > is the right way to write modern code.
    > However, declaring automatic objects anywhere
    > except at the begining of a compound statement,
    > is a little too modern for my old compiler.
    > When I first started learning C,
    > I didn't want to learn any other IO functions besides printf,
    > because I thought the IO library was complicated,
    > but some of the other functions like putchar, are worth learning.
    >
    > I like to rewrite other people's code:


    so do I ;-)

    > /* BEGIN new.c */
    >
    > #include <stdio.h>
    >
    > int main(void)
    > {
    > int i, j, x;
    >
    > for (i = 0; 9 > i; ++i) {
    > for (j = 0; i > j; ++j) {
    > printf("- ");
    > }
    > for (x = j + 1; 9 >= x; ++x) {
    > printf("%d ",x);
    > }
    > putchar('\n');
    > }
    > return 0;
    > }


    Your for loops are non-conventional. When writing code for best readability
    for all programmers, including newbie, it is essential to use classic proven
    idioms. writing your comparisons backwards from conventional use is
    surprising. You probably do this as a out of consistency with 0 == x being
    less error prone than x == 0, but even this idiom is largely irrelevant with
    modern compilers that can complain about suspicious assignments in
    conditional expressions.

    This version is more "classic"

    #include <stdio.h>

    int main(void)
    {
    int i, j;

    for (i = 0; i < 9; i++) {
    for (j = 0; j < i; j++) {
    printf("- ");
    }
    for (; j < 9; j++) {
    printf("%d ", j + 1);
    }
    putchar('\n');
    }
    return 0;
    }

    --
    Chqrlie.
     
    Charlie Gordon, Sep 17, 2007
    #4
  5. [comp.lang.c] Charlie Gordon <> wrote:

    > This version is more "classic"
    > (snip for loops)


    Well, since the for loops seem to be up for debate, I figure the fewer
    the better:

    #include <stdio.h>

    int main( void ) {
    int i;
    char *dashes = "- - - - - - - - ";
    char *nums = "1 2 3 4 5 6 7 8 9";

    for (i = 0; i < 9; i++) {
    printf( "%.*s%s\n", i*2, dashes, nums+i*2 );
    }
    return 0;
    }

    :) (One could arrange to build dashes and nums on the fly, of course.)

    --
    C. Benson Manica | I appreciate all corrections, polite or otherwise.
    cbmanica(at)gmail.com |
    ----------------------| I do not currently read any posts posted through
    sdf.lonestar.org | Google groups, due to rampant unchecked spam.
     
    Christopher Benson-Manica, Sep 17, 2007
    #5
  6. Junmin H.

    pete Guest

    Charlie Gordon wrote:
    >
    > "pete" <> a écrit dans le message de news:
    > ...
    > > Junmin H. wrote:
    > >>
    > >> Hello, just wrote down 4 simple codes to print numbers with order
    > >> for practice in C.
    > >>
    > >> Just would like to know if you guys have any suggestion about them.
    > >> thanks
    > >>
    > >> Junmin
    > >>
    > >> =1=1=1=1=1=1=1=1=1=1=1=1=1=1=1=1=1=1=1=1=1=1=1=
    > >> #include <stdio.h>
    > >>
    > >> main(void){
    > >> int i = 0;
    > >> while(i < 9){
    > >> int j = 0;
    > >> while(j < i){
    > >> printf("- ");
    > >> ++j;
    > >> }
    > >> int x = ++i;
    > >> while(x <= 9){
    > >> printf("%d ",x);
    > >> ++x;
    > >> }
    > >> printf("\n");
    > >> }
    > >> return 0;
    > >> }

    > >
    > > Explicitly declaring main as returning type int,
    > > is the right way to write modern code.
    > > However, declaring automatic objects anywhere
    > > except at the begining of a compound statement,
    > > is a little too modern for my old compiler.
    > > When I first started learning C,
    > > I didn't want to learn any other IO functions besides printf,
    > > because I thought the IO library was complicated,
    > > but some of the other functions like putchar, are worth learning.
    > >
    > > I like to rewrite other people's code:

    >
    > so do I ;-)
    >
    > > /* BEGIN new.c */
    > >
    > > #include <stdio.h>
    > >
    > > int main(void)
    > > {
    > > int i, j, x;
    > >
    > > for (i = 0; 9 > i; ++i) {
    > > for (j = 0; i > j; ++j) {
    > > printf("- ");
    > > }
    > > for (x = j + 1; 9 >= x; ++x) {
    > > printf("%d ",x);
    > > }
    > > putchar('\n');
    > > }
    > > return 0;
    > > }

    >
    > Your for loops are non-conventional.
    > When writing code for best readability
    > for all programmers, including newbie,
    > it is essential to use classic proven idioms.
    > writing your comparisons backwards from conventional use is
    > surprising.
    > You probably do this as a out of consistency with 0 == x being
    > less error prone than x == 0,


    I don't like to use the "less than" operator.
    It started a long time ago when I was implementing various
    sorting algorithms and I noticed that I only needed
    a "greater than" comparison to implement any of the algorithms
    that I was using. I'm aware that outside of that context,
    that it's now just a quirk in my writing style.

    > but even this idiom is largely irrelevant with
    > modern compilers that can complain about suspicious assignments in
    > conditional expressions.
    >
    > This version is more "classic"


    Yes, it is.

    > #include <stdio.h>
    >
    > int main(void)
    > {
    > int i, j;
    >
    > for (i = 0; i < 9; i++) {
    > for (j = 0; j < i; j++) {
    > printf("- ");
    > }




    > for (; j < 9; j++) {
    > printf("%d ", j + 1);
    > }


    while (j++ < 9) {
    printf("%d ", j);
    }



    > putchar('\n');
    > }
    > return 0;
    > }


    --
    pete
     
    pete, Sep 18, 2007
    #6
  7. "Christopher Benson-Manica" <> a écrit dans le
    message de news: fcm759$oer$...
    > [comp.lang.c] Charlie Gordon <> wrote:
    >
    >> This version is more "classic"
    >> (snip for loops)

    >
    > Well, since the for loops seem to be up for debate, I figure the fewer
    > the better:
    >
    > #include <stdio.h>
    >
    > int main( void ) {
    > int i;
    > char *dashes = "- - - - - - - - ";
    > char *nums = "1 2 3 4 5 6 7 8 9";
    >
    > for (i = 0; i < 9; i++) {
    > printf( "%.*s%s\n", i*2, dashes, nums+i*2 );
    > }
    > return 0;
    > }
    >
    > :) (One could arrange to build dashes and nums on the fly, of course.)


    This code is quite elegant.
    It does produce different output than the loops (missing trailing space
    before the new-line), but that is probably an improvement.
    I would prefer to use const char * variables to hold pointers to literal
    character arrays.

    --
    Chqrlie.
     
    Charlie Gordon, Sep 18, 2007
    #7
    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. Greg  --
    Replies:
    4
    Views:
    2,243
  2. Replies:
    2
    Views:
    2,935
    Malcolm
    Aug 20, 2005
  3. Replies:
    2
    Views:
    594
  4. Replies:
    4
    Views:
    559
  5. Anthony Kong
    Replies:
    4
    Views:
    208
    Chris Angelico
    Sep 26, 2012
Loading...

Share This Page