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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Nest loops

  • 08-11-2010 3:15pm
    #1
    Registered Users, Registered Users 2 Posts: 1,595 ✭✭✭


    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, Registered Users 2 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, Registered Users 2 Posts: 68,317 ✭✭✭✭seamus


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


  • Registered Users, Registered Users 2 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, Registered Users 2 Posts: 1,595 ✭✭✭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, Registered Users 2 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