as*****@hotmail .com wrote in message ...

>

David Harmon wrote:
>On 19 Nov 2006 11:05:56 -0800 in comp.lang.c++, wrote,

The whole benefit from this exercise comes from thinking about the

code, not from the marvelous result that you see when it "works".

off course you make sense, that was the reason i tried. As you asked

above that where is the majic number 3 come from....Answer to that

number alighn the triangles in triangle shape. But, as soon as i input

the number, triangles go out of shape.

You need to learn to analyze your loops. Try the following

#include <sstream// and your others

// insert in main() (replace first for() loop set)

// ----------

std::ostringstr eam sos;

// leave the 'sos' touching the left margin for now.

// it will make commenting or removeing them easier

// when you are done with testing.

for( int row = 0; row < n; row++ ){

sos<<"for A row="<<row<<" (3-row)="<<(3-row)<<"\n";

for( int column = 0; column <= row; column++ )

sos<<" for B row="<<row<<" column="<<colum n<<"\n";

cout << ' ';

for( int column = 0; column < (3 - row); column++ )

sos<<" for C row="<<row<<" column="<<colum n

<<" (3-row)="<<(3-row)<<"\n";

cout << " * ";

cout << " "<< endl;

cout << endl;

}

sos<<"Done"<<"\ n";

cout<<sos.str() <<endl;

// ----------

// n == 5

for A row=0 (3-row)=3

for B row=0 column=0

for C row=0 column=0 (3-row)=3

for C row=0 column=1 (3-row)=3

for C row=0 column=2 (3-row)=3

for A row=1 (3-row)=2

for B row=1 column=0

for B row=1 column=1

for C row=1 column=0 (3-row)=2

for C row=1 column=1 (3-row)=2

for A row=2 (3-row)=1

for B row=2 column=0

for B row=2 column=1

for B row=2 column=2

for C row=2 column=0 (3-row)=1

for A row=3 (3-row)=0 // now for C skips

for B row=3 column=0

for B row=3 column=1

for B row=3 column=2

for B row=3 column=3

for A row=4 (3-row)=-1 // <<---- note how it goes negative!

for B row=4 column=0

for B row=4 column=1

for B row=4 column=2

for B row=4 column=3

for B row=4 column=4

Done

Now, fix the loops:

// insert in main() (replace first for() loop set)

// ----------

std::ostringstr eam sos;

for( int row = 0; row <= n; ++row ){

sos<<"for A row="<<row<<" (n-1-row)="<<(n-1-row)<<"\n";

for( int column = 0; column <= row; ++column ){

sos<<" for B row="<<row<<" column="<<colum n<<"\n";

cout<<" ";

}

for( int column = 0; column <= (n-1-row); ++column ){

sos<<" for C row="<<row<<" column="<<colum n

<<" (n-1-row)="<<(n-1-row)<<"\n";

cout<<" * ";

}

//cout<<" "<<endl;

cout<<" "<<"<<---end line"<<endl; // add for test

//cout << endl;

}

sos<<"Done"<<"\ n";

cout<<sos.str() <<endl;

// ----------

Enter a single number:>5

* * * * * <<---end line

* * * * <<---end line

* * * <<---end line

* * <<---end line

* <<---end line

<<---end line

for A row=0 (n-1-row)=4

for B row=0 column=0

for C row=0 column=0 (n-1-row)=4

for C row=0 column=1 (n-1-row)=4

for C row=0 column=2 (n-1-row)=4

for C row=0 column=3 (n-1-row)=4

for C row=0 column=4 (n-1-row)=4

for A row=1 (n-1-row)=3

for B row=1 column=0

for B row=1 column=1

for C row=1 column=0 (n-1-row)=3

for C row=1 column=1 (n-1-row)=3

for C row=1 column=2 (n-1-row)=3

for C row=1 column=3 (n-1-row)=3

for A row=2 (n-1-row)=2

for B row=2 column=0

for B row=2 column=1

for B row=2 column=2

for C row=2 column=0 (n-1-row)=2

for C row=2 column=1 (n-1-row)=2

for C row=2 column=2 (n-1-row)=2

for A row=3 (n-1-row)=1

for B row=3 column=0

for B row=3 column=1

for B row=3 column=2

for B row=3 column=3

for C row=3 column=0 (n-1-row)=1

for C row=3 column=1 (n-1-row)=1

for A row=4 (n-1-row)=0

for B row=4 column=0

for B row=4 column=1

for B row=4 column=2

for B row=4 column=3

for B row=4 column=4

for C row=4 column=0 (n-1-row)=0

for A row=5 (n-1-row)=-1 // still goes negative, but after C==1

for B row=5 column=0

for B row=5 column=1

for B row=5 column=2

for B row=5 column=3

for B row=5 column=4

for B row=5 column=5

Done

Study the changes and the output until you see how the loops work.

Learn to use tools ( like my ostringstream sos;) to help you see where

problems crop up.

Another trick is: You can't often see spaces sent to screen, so, use an

unused character to see the output.

// cout<<" ";

cout<<".";

// cout<<" "<<endl;

cout<<"....."<< endl;

After testing, just change them back.

Now, you try:

change (n-1-row) to (n-row), and see what happens.

change <= to < in the C for() loop, and see what happens (in combination

with above change).

--

Bob R

POVrookie