Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Nest loops

Options
  • 08-11-2010 4:15pm
    #1
    Registered Users Posts: 1,586 ✭✭✭


    Hi all,

    Can some one help me with a very noob issue im having , I need to print a 2 dimentional array using a nested loop , that shoud look like >>

    FFFFFF
    FFFFFF
    FFFFFF
    FFFFFF
    FFFFFF
    FFFFFF
    FFFFFF
    FFFFFF
    FFFFFF
    FFFFFF

    Ive compile the following >

    for (r=1; r<10; r++)
    {
    printf("F\n");
    for (s=1; s<6; s++)
    {
    printf("F");
    but the output is appearing as

    F
    FFFFFF
    FFFFFF
    FFFFFF
    FFFFFF
    FFFFFF
    FFFFFF
    FFFFFF
    FFFFFF
    FFFFF

    i understand why its appearing like that but cant figure out how to resolve.

    Thanks guys


Comments

  • Registered Users Posts: 108 ✭✭Jaych1000


    Just at a quick glance, could you not put the print with the line-break after the nested For loop?
    EDIT: You also have to do something else but I'll leave that up to you to figure out :P


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    Why is there a printf("F\n") on Line 3? :)


  • Registered Users Posts: 15,065 ✭✭✭✭Malice


    [PHP]for (r=1; r<10; r++)
    {
    printf("F\n");
    for (s=1; s<6; s++)
    {
    printf("F");[/PHP]

    Yeah, as Seamus said, what is the first printf() call doing? I would also start the looping from zero unless you've a good reason to start at 1 as otherwise you might start running into array indexing issues.


  • Users Awaiting Email Confirmation Posts: 351 ✭✭ron_darrell


    for (r=1; r<10; r++)
    {
    printf("F\n");
    for (s=1; s<6; s++)
    {
    printf("F");
    

    Repeat 9 times
    Print "F" and a new line
    Print "F" 5 times
    End repeat

    What's the result?

    F
    FFFFFF
    FFFFFF
    FFFFFF
    FFFFFF
    FFFFFF
    FFFFFF
    FFFFFF
    FFFFFF
    FFFFFF
    FFFFF

    Instead look at what you want to do

    Repeat 9 times
    Print "F" 6 times
    Print a new line
    End repeat

    This will look like this:
    for (r=0; r<9; r++)
    {
    for (s=0; s<6; s++)
    {
    printf("F");
    }
    printf("\n");
    }
    

    Always when coding; rather than just start coding; look at what you're trying to achieve. Break it down into small steps. Then code it.

    Hope that helps.
    Regards,
    RD


  • Registered Users Posts: 1,586 ✭✭✭Gaz


    Thanks guys .... I actually made some progress and had it similar to Rons solution, just had the braces in the wrong place. :rolleyes:

    Thanks again


  • Advertisement
  • Registered Users Posts: 2,781 ✭✭✭amen


    you could it with just one for loop if you used a modulo with 6 and use that to print the \n


Advertisement