For in For Loop control (example pascal triangle)

Hi there!

I am just trying to get the "control" on for in for loops but i am having some problems.. The code that i wrote creates first a "0-matrix"and after that a triangle of "1" is set and finally the pascal triagle is "calculated".
Now I am trying to mirror the triangle but i am having problem with the boundary contidions of the loop, when calculating the triangle.
In my code i am doing my operations top/down and from the left to the right just using loops from 0 to x.
To improve the control of loops i am trying to do the operations (in special the pascal triangle) down/up and from the left to the right. Problem here are the boundary conditions. How do I set the inner loop boundary conditions for this case? (I am not having problems with rotating the 1-Matrix, just with the pascal triangle)

If someone can me explain or give a good example how do I control both loops counting down (decreasing) would be very greatful!
Thanks in advance!



Now:
1
11
121
1331

Mirror (* are blank spaces):
***1
**11
*121
1331

Mirror + Upside down (* are blank spaces):
***1
**13
*123
1111



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <stdlib.h>
using namespace std;

int main()
{
	int x;
	cin >> x;
	int tringle[x + 1][x + 1];
	int z, s;
	 // MATRIX CREATION
	for (z = 0; z < x + 1; ++z)
	{
		for (s = 0; s < x + 1; ++s)
		{
			tringle[z][s] = 0;
		}
	}
	
	// 1-Matrix
	for (z = 0 ; z < x +1 ; ++z)
	{
		for (s = 0 ; s <= z ; ++s)
		{
			tringle[z][s] = 1;
		}
	}
	
	 // PASCAL TRIANGLE 
	for (z = 2; z < x + 1 ; ++z)
    {
      for (s = 1; s < z; ++s)
        {
          tringle[z][s] = tringle[z - 1][s - 1] + tringle[z - 1][s];
        }
    }
    	
	//OUTPUT
	for (z = 0; z < x + 1; ++z)
	{
		for (s = 0; s < x + 1; ++s)
		{
			cout << tringle[z][s];
		}
	
		cout << endl;	
	} 
	
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
using namespace std;

//======================================================================

void outchar( int i )
{
    if ( i ) cout << i;
    else     cout << ' ';
}

//======================================================================

int main()
{
   const int x = 3;                         // <==== Can't input dimensions for this type of array at run-time
   int triangle[x + 1][x + 1] = { 0 };      // <==== Simplify initialisation

   int z, s;

   // 1-EDGES
   for ( z = 0; z <= x; ++z )
   {
      triangle[z][0] = 1;
      triangle[z][z] = 1;
   }

   // PASCAL TRIANGLE 
   for ( z = 2; z <= x; ++z )
   {
      for ( s = 1; s < z; ++s)
      {
         triangle[z][s] = triangle[z - 1][s - 1] + triangle[z - 1][s];
      }
   }
        
   //OUTPUTS
   cout << "Now:\n";
   for ( z = 0; z <= x; ++z )
   {
      for ( s = 0; s <= x; ++s ) outchar( triangle[z][s] );
      cout << endl;
   } 
   cout << endl;
        
   cout << "Mirror:\n";
   for ( z = 0; z <= x; ++z )
   {
      for ( s = x; s >= 0; --s ) outchar( triangle[z][s] );
      cout << endl;
   } 
   cout << endl;
        
   cout << "Mirror + Upside down (nope: just rotated):\n";
   for ( s = x; s >= 0; --s )
   {
      for ( z = 0; z <= x; ++z ) outchar( triangle[z][s] );
      cout << endl;
   } 
   cout << endl;
}


Now:
1   
11  
121 
1331

Mirror:
   1
  11
 121
1331

Mirror + Upside down (nope: just rotated):
   1
  13
 123
1111
thanks for the answer, very helpful and I understood it!
Now another question.. is there a way to avoid using a function that prints blank spaces? a way by using the code below and only changing the way of counting (like x-s) and the boundary conditions?

I am just having the feeling that i cant control loops (or really understand it) when using it on more dimensions....

1
2
3
4
5
6
7
for ( z = 2; z <= x; ++z )
   	{
      for ( s = 1; s < z; ++s)
      {
         triangle[z][s] = triangle[z - 1][s - 1] + triangle[z - 1][s];
      }
   	}



Last edited on
Well, something has to print a blank to put the output at the correct position. If you don't want another function then you would have to either count blanks in advance of the first output (yet another for loop), do something funny with the field width in the cout statement, or write the contents of the function at the point where it is called. I don't personally see the sense in any of those.
Last edited on
Thanks,

yeah after "playing" a little more with the code I understood what you mean!
and I just discovered that the best way to mirrow/rotate is to change the output (the way it is "printed") and not the way it is calculated.

Thanks again for the help and that you took time to explain it!
Topic archived. No new replies allowed.