# Thread: Nested Loops is making me loopy

1. ## 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. 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. 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. @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. Semicolon after printf("D")? If I get this one wrong, I'm not posting any more garbage...lol

6. Originally Posted by Rubik
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. > 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.

8. Originally Posted by Rubik
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.

Popular pages Recent additions