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
#include <iostream>
#include <stdlib.h>
usingnamespace 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;
}
}
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];
}
}
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.
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!