Menu

Help with my Code ?? Check it please

2010-12-07
2012-09-26
  • Kyel John David

    Kyel John David - 2010-12-07

    I need to have this output:

    ---GGGGGGGGGGGGGGGGGG
    ---------GGGGGGGGGGGG
    ---------------GGGGGG
    ---------------------
    ---------------BBBBBB
    ---------BBBBBBBBBBBB
    ---BBBBBBBBBBBBBBBBBB
    (note they are supposed to be all in line with each other but it got messed up
    when I posted this)

    and this is my code

    include <stdio.h>

    include <conio.h>

    main()
    {
    int i, j, Eritrea;

    printf("Input the Size of the Flag: ");
    scanf("%d",&Eritrea);

    for (i = 0; i < Eritrea / 2; ++i) {
    for (j = 0; j < i; ++j)
    printf("-");
    for (; j < Eritrea; ++j)
    printf("G");
    printf("\n");
    }
    for (j = 0; j < Eritrea; ++j)
    printf("-");
    printf("\n");
    for (i = 0; i < Eritrea / 2; ++i) {
    for (j = 0; j < Eritrea / 2 - i - 1; ++j)
    printf("-");
    for (;j<Eritrea; ++j)
    printf("B");
    printf("\n");
    }
    getch();
    }

    will anyone ghelp me out with this?

     
  • Steve A

    Steve A - 2010-12-07

    I have made a few corrections to your code:

    #include <stdio.h>
    #include <conio.h>
    
    int main()
    {
        int i, j, Eritrea;
        char ch;
    
        printf("Input the Size of the Flag: ");
        scanf("%d",&Eritrea);
    
        for(i = 0; i < Eritrea / 2; ++i)
        {
            for(j = 0; j < i; ++j)
            {
                printf("-");
            }
    
            for(; j < Eritrea; ++j)
            {
                printf("G");
            }
            printf("\n");
        }
    
        for(j = 0; j < Eritrea; ++j)
        {
            printf("-");
        }
        printf("\n");
    
        for(i = 0; i < Eritrea / 2; ++i)
        {
            for(j = 0; j < Eritrea / 2 - i - 1; ++j)
            {
                printf("-");
            }
    
            for (;j<Eritrea; ++j)
            {
                printf("B");
            }
            printf("\n");
        }
    
        ch = getch();
    
        return 0;
    }
    

    1) Here:
    printf("Input the Size of the Flag: ");

    what is the input parameter ?

    2) Here:
    for(i = 0; i < Eritrea / 2; ++i)
    {
    for(j = 0; j < i; ++j)

    i equals 0
    j equals 0
    (...; while j < i;...)

    the second for(;;) never gets executed, on the first loop, because i and j
    both equal 0.

     
  • Steve A

    Steve A - 2010-12-07

    This forum is crap!!!
    Code gets smushed so you can't read it.

    Try it again!

    include <stdio.h>

    include <conio.h>

    int main()
    {
    int i, j, Eritrea;
    char ch;

    printf("Input the Size of the Flag: ");
    scanf("%d",&Eritrea);

    for(i = 0; i < Eritrea / 2; ++i)
    {
    for(j = 0; j < i; ++j)
    {
    printf("-");
    }

    for(; j < Eritrea; ++j)
    {
    printf("G");
    }
    printf("\n");
    }

    for(j = 0; j < Eritrea; ++j)
    {
    printf("-");
    }
    printf("\n");

    for(i = 0; i < Eritrea / 2; ++i)
    {
    for(j = 0; j < Eritrea / 2 - i - 1; ++j)
    {
    printf("-");
    }

    for (;j<Eritrea; ++j)
    {
    printf("B");
    }
    printf("\n");
    }

    ch = getch();

    return 0;
    }

     
  • Kyel John David

    Kyel John David - 2010-12-08

    uhmm excuse me but there is supposed to be and "-" before the "G" how do I fix
    that?

     
  • Kyel John David

    Kyel John David - 2010-12-08

    3 is the input parameter

     
  • Steve A

    Steve A - 2010-12-08

    uhmm excuse me but there is supposed to be and "-" before the "G" how do I
    fix that?

    Yes, I explained that in #2:

    2) Here:
    for(i = 0; i < Eritrea / 2; ++i)
    {
    for(j = 0; j < i; ++j)

    i equals 0
    j equals 0
    (...; while j < i;...)

    the second for(;;) never gets executed, on the first loop, because i and j
    both equal 0.

    ...what this means is that there is a flaw in your logic at this point.
    The "-" never gets printed because i=0 and j=0,
    while your statement says (to the effect):
    "while j (is less than) i, do this..."

    j is equal to i, therefore, the loop is skipped.

    To correct the situation, you need to give i an initial value.

    Steve

     
  • Kyel John David

    Kyel John David - 2010-12-08

    but wait it is supposed to be 3 " - " can you suggest an intiial value or any
    will do?

     
  • Kyel John David

    Kyel John David - 2010-12-08

    Edit I already added an initial value but still the output is still different
    what should I add? ; it still does not give me the ouput I this output when I
    input 7

    ---GGGGGGGGGGGGGGGGGG
    ---------GGGGGGGGGGGG
    ---------------GGGGGG
    ---------------------
    ---------------BBBBBB
    ---------BBBBBBBBBBBB
    ---BBBBBBBBBBBBBBBBBB

     
  • Steve A

    Steve A - 2010-12-08

    The following code seems to work (as long as you use odd input values
    only):

    include <stdio.h>

    include <conio.h>

    int main()
    {
    int i, j, Eritrea;
    int x, y, z;
    char ch;

    printf("Input the Size of the Flag: ");
    scanf("%d",&Eritrea);

    y = Eritrea * 3;
    x = y - 3;

    for(i = 0; i < Eritrea / 2; i++)
    {
    z = y - x;

    for(j = 0; j < z; j++)
    {
    printf("-");
    }

    for(; j < y; j++)
    {
    printf("G");
    }
    printf("\n");
    x -= 6;

    }

    for(j = 0; j < y; j++)
    {
    printf("-");
    }
    printf("\n");

    x = 6;

    for(i = 0; i < Eritrea / 2; i++)
    {
    z = y - x;

    for(j = 0; j < z; j++)
    {
    printf("-");
    }

    for(; j < y; j++)
    {
    printf("B");
    }
    printf("\n");
    x += 6;
    }

    ch = getch();

    return 0;
    }

    I'm certain there is a better way of doing this.
    Steve

     
  • Kyel John David

    Kyel John David - 2010-12-10

    for(; j < y; j++)

    that one is that supposed to be an error/ there is no j=1? or it is okay like
    that/

     
  • Steve A

    Steve A - 2010-12-10

    Yes, that is correct.
    At that point in the code, we want j to procede with it's present value
    and continue counting upwards from there.

    Steve

     
  • Kyel John David

    Kyel John David - 2010-12-10

    whoah I did not know that, I learned something new , at first I thought it was
    a typo I made then it compiled whoah

     
  • Anonymous

    Anonymous - 2011-03-12

    do you want to upgrade quickly in maple story?why not Buy Maplestory
    Mesos
    to enlarge your enquipment?our website
    sellCheap Maplestory Mesos and provide all
    kinds of service.get going!you will get more discount when you Buy Maplestory
    Mesos
    from us.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.