Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Nest loops

  • 08-11-2010 04:15PM
    #1
    Registered Users, Registered Users 2 Posts: 1,602 ✭✭✭


    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,173 ✭✭✭✭seamus


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


  • Registered Users, Registered Users 2 Posts: 15,079 ✭✭✭✭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,602 ✭✭✭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