Unable to understand the logic behind nested for loop- now with 3 loops

Jul 4, 2010 at 4:30am
Hi..
I still do not understand the logic behind the nested loop..

I was trying to come up with a program that would display this.

5
5 4
5 4 3
5 4 3 2
5 4 3 2 1

But I just could not get it.. i gave up and just found the solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{
	//int n;
	float sum=0,term=1;

	for (int i=5; i != 0; i--)
	{
		for(int j=5; j>=i; j--)
		{
			cout << j << '\t';
		}
	cout << endl;
	}
return 0;
}


the thing is..I still don't understand..I can't seem to get it..

can someone explain..I need to understand the logic..for loop is so fundamental.in C++..
please
Last edited on Jul 8, 2010 at 5:26am
Jul 4, 2010 at 8:13am
Do you understand what a single loop does? Well a nested loop does the inner loop as many times as the outer loop, loops.

In your example i takes on the value 5. Then the inner loop makes the variable j go from 5 to i(which changes with each outer loop).

So j will loop multiple times. First when i = 5, next when i = 4, next when i = 3 etc...
Jul 4, 2010 at 4:34pm
Kind of..

something like this..

when i = 5 @ the outer loop
the inner loop is executed..

so ..the value j is initialised to 5 and 5 is also more than or equals to i which at the outer loop is now 5..

so it prints 5..and because after j--...it becomes 4..it is ignored...it goes to the outer loop...
and now i becomes 4...

again..the inner loop is activated..and now..it prints 5 ..and then..after j--...4 is also less than or equal to i..it prints 4


but.. i think..it depends on the question type..I really have to look out for nested problem questions..exercises makes it perfect..thanks again
Jul 4, 2010 at 5:00pm
If you'd like another nested loop problem, perhaps try this one:

Let's say we have a square matrix M of dimension 3.

[2][1][3]
[6][1][4]
[7][2][1]

The values of each element in the array are arbitrary.

Task: Write a function that will...
* take a pointer parameter m of type int, where m points to a 2D integer array (which represents our square matrix like M)
* calculate the transpose of m (hint: can be done with nested loops)
* persist the result in the location pointed to by m (that is, after the function is finished executing, m will point to the transposed matrix, not the original matrix)
Jul 5, 2010 at 4:38pm
hi sammy

thanks..but the thing is..I have not covered functions and array yet..just about too...will get back to this question..once i am ok with array and function.
Jul 8, 2010 at 5:25am
hi again..

I think I am fine with 2 level of nested loop for now...
can someone guide me on how to do..3 level of nested loops

I have got this exercise..that I saw in a book, can someone just describe the logic

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 1  for ( int i = 1; i <= 5; i++ )
 2  {
 3     for ( int j = 1; j <= 3; j++ )
 4     {
 5        for ( int k = 1; k <= 4 ; k++ )
 6           cout << '*';
 7
 8        cout << endl;
 9     } // end inner for
10
11     cout << endl;
12  } // end outer for

 

I know how the last loop, k works..I know it should print ****..but how does it loop to the outer loop, whichone goes first
Last edited on Jul 8, 2010 at 5:26am
Jul 8, 2010 at 9:24am
It works exactly the same as with two loops.

The each inner loop completes once for every iteration of its outer loop.
Aug 21, 2010 at 3:34pm
thanx guys that code given above by kalp1200 worked me out practically from my difficulty thanx dude!!(kalp1200);
Topic archived. No new replies allowed.