Thread: Nested Loops is making me loopy

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    23

    Nested Loops is making me loopy

    So I'm taking an OpenSource C class and I literally cannot think my way through a problem. I need to get a number from a user (example: 4), and then use that number to create
    xx
    xxx
    xxxx
    xxxxx
    with the number as the base. Here's what I know so far: I need to create a nested loop (two for statements).

    Here's what I have so far:

    Code:
    #include <cs50.h> 
    #include <stdio.h>  
    
    int main(void) 
    {
         printf("Insert a non-negative integer no greater than 23: ");
         
        int x = GetInt();
         while (x > 23 || x < 0)
        {
            printf("Insert a non-negative integer less than 24!: ");
             x = GetInt();
        }
        return 0;
     }
    
    
    // here's the nested loop part
        int row;
        for (row = 1; row == (x - 1); row++);
        {
            int c;
            for (c = 1; c == (x - 1); c++)
                printf(" ");
            int z;
            for (z =2; z == (row + 1); row++)
                printf("#");
        }  
      return 0;
    }
    I've been trying for hours and I have no idea how to tackle and defeat this nested loop. Any ideas? Thank you.

  2. #2
    Registered User
    Join Date
    Feb 2013
    Posts
    8
    I really hesitate to post an answer since I know next to nothing about coding, but...isn't GetInt() a function call? I don't see the function prototyped above main() or called anywhere below main. How do you get the number from the user? I was expecting a scanf() statement. I hope I'm not saying dumb things...lol

  3. #3
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Your main problem is boolean test expression in your for() loops. The for loop will loop only as long as the expression evaluates to true (non-zero). So, if row doesn't equal x - 1, the first loop ends.

    Another problem is that you update row in your outer loop AND the second inner loop. You probably meant to increment z, and not row, since the logic fails completely otherwise.

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    23
    @C-Noob: I think I can use GetInt() because I have a pre-programmed library in my linux system.

    @Tclausex: Thank you for answering! You're absolutely right in that my second inner loop cannot include row. However, I don't understand what is wrong with my boolean test expression in my for() loop; the expression in my first inner loop will evaluate to true because row will eventually reach x -1 (since row increases by 1 every time).


    I'm trying to break down the nested loop into simplified steps and I have:
    Code:
    #include <cs50.h>
    #include <stdio.h>
    
    int main(void)
    {
        printf("Insert an integer less than 24: ");
        int x = GetInt();
    
        // to get the spaces right, where D is equivalent to a space
        int c;
        for (c = (x-1); c == 0; c--)
            printf("D")
        printf("\n");
    
        return 0;
    }
    I don't understand why when I run the simplified code, D does not print.

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    8
    Semicolon after printf("D")? If I get this one wrong, I'm not posting any more garbage...lol

  6. #6
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Quote Originally Posted by Rubik View Post
    the expression in my first inner loop will evaluate to true because row will eventually reach x -1 (since row increases by 1 every time).
    That is not the case. row increases at the bottom of the loop - but that depends on the loop executing, which requires the test expression to be true for each iteration. The loop stops (or never begins) once the expression is false.
    Look at the for loop in its equivalent while() loop form...
    Code:
    //Given the loop    for(  row = 1 ; row == (x - 1)  ; row++ )
    
    row = 1;
    while( row == (x - 1) )
    {
      ...
      row++;
    }
    You should design your loops so that they enter the loop and continue to loop as long as the required condition is met. If its more natural to think about what condition will cause a particular loop to end rather than start/continue, that's fine, just negate the expression (or apply DeMorgan's).

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I don't understand why when I run the simplified code, D does not print.
    Perhaps because it won't compile.

    Are you really copy/pasting directly from your IDE.
    If you're not pasting the code you're ACTUALLY running, then you're wasting everyone's time.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Rubik View Post
    Code:
    // here's the nested loop part
        int row;
        for (row = 1; row == (x - 1); row++);
        {
            int c;
            for (c = 1; c == (x - 1); c++)
                printf(" ");
            int z;
            for (z =2; z == (row + 1); row++)
                printf("#");
        }  
      return 0;
    }
    I've been trying for hours and I have no idea how to tackle and defeat this nested loop. Any ideas? Thank you.
    Yeah, right. I believe that the easter bunny lays eggs too. It strikes me you're just tapping in code with the hope that a miracle will happen. Miracles are rare. Reading compiler diagnostics would have been a realistic start to you solving your own problem.

    Firstly, your code won't compile, since the fragment I quoted is not in a function. If it can't be compiled, it can't be executed.

    Even if you fix that problem, the outer loop will only be executed (once and once only) if x has a value of 2. The first inner loop will then run once and once only (again because it only works if x is 2) and print a space, that you won't be able to see on most screens. The second inner loop (printing hashes) won't do anything, since row has a value of 2.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loops
    By brillpsycho in forum C Programming
    Replies: 11
    Last Post: 10-02-2012, 06:00 PM
  2. Nested loops
    By zeondik in forum C# Programming
    Replies: 2
    Last Post: 10-26-2008, 06:58 AM
  3. Help nested for loops
    By dals2002 in forum C Programming
    Replies: 14
    Last Post: 03-14-2008, 01:18 AM
  4. Nested For Loops
    By smitsky in forum C++ Programming
    Replies: 2
    Last Post: 11-28-2004, 01:58 PM
  5. nested loops
    By briand. in forum C Programming
    Replies: 6
    Last Post: 10-01-2002, 05:15 PM