Menu

Looping Question Kindly Check please

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

    Kyel John David - 2010-12-11

    I should have have an Input like this when I enter the number 4


    BBBBBBBBBBBB
    YYYYYYYYYYYY
    GGGGGGGGGGGG

    and I should have an Output like this when I input 8

    ------------------------

    BBBBBBBBBBBBBBBBBBBBBBBB
    BBBBBBBBBBBBBBBBBBBBBBBB
    YYYYYYYYYYYYYYYYYYYYYYYY
    YYYYYYYYYYYYYYYYYYYYYYYY
    GGGGGGGGGGGGGGGGGGGGGGGG
    GGGGGGGGGGGGGGGGGGGGGGGG

    here is my code kindlly check what is wrong if you could kindly edit it

    include<stdio.h>

    include<conio.h>

    main()
    {
    int ctr,input, ctr2;//declaration of the variabkes
    printf("Enter a number");
    scanf("%d",&input);

    for(ctr=0;ctr<input*3;ctr++)
    {
    for(ctr = 0; ctr < (input * 3); ctr++)
    printf("-");
    printf("\n");
    for(ctr = 0; ctr < (input * 3); ctr++)
    printf("-");
    printf("\n");
    for(ctr = 0; ctr < (input * 3); ctr++)
    printf("B");
    printf("\n");
    for(ctr = 0; ctr < (input * 3); ctr++)
    printf("B");
    printf("\n");
    for(ctr = 0; ctr < (input * 3); ctr++)
    printf("Y");
    printf("\n");
    for(ctr = 0; ctr < (input * 3); ctr++)
    printf("Y");
    printf("\n");
    for(ctr = 0; ctr < (input * 3); ctr++)
    printf("G");
    printf("\n");
    for(ctr = 0; ctr < (input * 3); ctr++)
    printf("G");
    printf("\n");
    getch();
    }
    }

    thanks in advance

     
  • Steve A

    Steve A - 2010-12-11

    Okay, first things first:
    1) function main should always look like either this: int main()
    or this: int main(int argc, char *argv)

    and never like this: main()

    there are reasons why. I recommend you get a good tutorial on C programming.

    2) for reasons of clarity, (to others reading your code), even though the
    compiler does not care how code looks,
    always add newlines and extra spaces where you can, so that statements are
    blocked or grouped together.
    i.e.:
    for(bla; bla; bla) do_something(); / only a single statement allowed, using
    this method
    /
    something_else();
    or:
    for(bla; bla; bla)
    {
    do_something(); / multiple statements to be executed /
    ........();
    }
    something_else();

    for(bla; bla; bla)
    {
    do_something2();
    ............();
    }
    something_else2();
    etc...

    Make your code as easy to read (by others) as possible and add comments
    everywhere possible.

    Steve

     
  • Steve A

    Steve A - 2010-12-11

    Here:

    for(ctr = 0; ctr < (input * 3); ctr++)
    {
    for(ctr = 0; ctr < (input * 3); ctr++)
    {
    printf("-");
    }
    printf("\n");
    etc......,

    variable ctr is used to control both the outer and inner "FOR" loop.
    After the first cycle through the loop, variable ctr has changed its value
    numerous times inside each "FOR" loop.
    By the time it reaches the end of the first cycle, ctr has reached its
    maximum value and the program is done.

     
  • Steve A

    Steve A - 2010-12-11

    This code will accept input as: 4,8,12,etc....

    include <stdio.h>

    include <conio.h>

    int main()
    {
    int ctr, input, ctr2; //declaration of the variabkes
    int x; // need another integer variable
    char ch; // getch() needs a character variable

    printf("Enter a number ");
    scanf("%d",&input);

    if(input > 4) // here we need to determine how many
    { // loops for each stripe are needed
    ctr = (input / 4);
    }
    else
    {
    ctr = 1; // default is one (1) stripe
    }

    for(x = 0; x < ctr; x++) // loop for each stripe
    {
    for(ctr2 = 0; ctr2 < (input * 3); ctr2++)
    {
    printf("-");
    }
    printf("\n");
    }

    for(x = 0; x < ctr; x++) // loop for each stripe
    {
    for(ctr2 = 0; ctr2 < (input * 3); ctr2++)
    {
    printf("B");
    }
    printf("\n");
    }

    for(x = 0; x < ctr; x++) // loop for each stripe
    {
    for(ctr2 = 0; ctr2 < (input * 3); ctr2++)
    {
    printf("Y");
    }
    printf("\n");
    }

    for(x = 0; x < ctr; x++) // loop for each stripe
    {
    for(ctr2 = 0; ctr2 < (input * 3); ctr2++)
    {
    printf("G");
    }
    printf("\n");
    }
    ch = getch(); // getch() returns a character value

    return 0; // always end main() by returning a zero
    }

    Steve

     

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.