Need Help with writing a Triangle program

A

asif929

I am trying to write a program which creates four triangles. The
program begins with prompting a user " Enter the size of triangles",
number from 1 to N is the size of four triangles For Example if we
enter 4, the size of four triangles are 4 rows.

In addition, I am also writing a program which i started and failed in
giving the right size of traingles. I will appreciate if somebody can
help.


*
* *
* * *
* * * *


* * * *
* * *
* *
*


* * * *
* * *
* *
*


*
* *
* * *
* * * *


I have also tried to create this program but i couldn't be able to
correct the formating of "*".






#include <iostream>


using namespace std;


int main()
{
cout << "Enter a character:>";
int n;
cin >> n;


for ( int row = 0; row < n; row++ )
{
for ( int column = 0; column <= row; column++ )
cout << ' ';
for ( int column = 0; column < (3 - row); column++ )
cout << " * ";
cout << " "<< endl;
cout << endl;
}


for ( int row = 0; row < n; row++ )
{
for ( int column = 0; column <= (3 - row); column++ )
cout << " * ";
cout << " ";
cout << endl;
}
cout<<endl;
for ( int row = 0; row < n; row++ )
{
for ( int column = 0; column <= row; column++ )
cout << " * ";
cout << " ";
cout << endl;
}
cout<<endl;
for ( int row = 0; row < n; row++ )
{


for ( int column = 0; column < row; column++ )
cout << ' ';
for ( int column = 0; column <= (3 - row);
column++ )
cout << " * ";
cout << endl;


}


return 0;
 
B

BobR

(e-mail address removed) wrote in message ...
I am trying to write a program which creates four triangles. The
program begins with prompting a user " Enter the size of triangles",
number from 1 to N is the size of four triangles For Example if we
enter 4, the size of four triangles are 4 rows.

In addition, I am also writing a program which i started and failed in
giving the right size of traingles. I will appreciate if somebody can
help.

Why did you start another thread when you already had one going?
I have also tried to create this program but i couldn't be able to
correct the formating of "*".

#include <iostream>
using namespace std;

int main(){
cout << "Enter a character:>";
int n;
cin >> n;

/*
std::istringstream icin("R \n");
cout << "Enter a character:>";
int n;
icin >> n;
cout << n <<std::endl;
// out: Enter a character:>10673684

You asked for a 'character'! The 'cin' to an 'int' will fail, you get
undefined behavior!
*/

std::istringstream icin("4 \n");
cout << "Enter a single number:>";
int n(0); // INITIALIZE!!!
icin >> n;
if( not n ){ return EXIT_FAILURE;} // ALWAYS check your input.
// or: if( n < 1 || n > 1000 ){ return EXIT_FAILURE;}
cout << n <<std::endl;
// out: Enter a single number:>4
for( int row = 0; row < n; ++row ){
for( int column = 0; column <= row; ++column ){
cout << ' '; }
for( int column = 0; column < (3 - row); ++column ){
cout << " * "; }
cout << " "<< endl << endl;
} // for(row)


for( int row = 0; row < n; row++ ){
for ( int column = 0; column <= (3 - row); column++ ){
cout << " * "; }
cout << " " << endl;
} // for(row)
cout<<endl;
for( int row = 0; row < n; row++ ){
for ( int column = 0; column <= row; column++ ){
cout << " * "; }
cout << " " << endl;
} // for(row)
cout<<endl;
for( int row = 0; row < n; row++ ){
for( int column = 0; column < row; column++ ){
cout << ' '; }
for( int column = 0; column <= (3 - row); column++ ){
cout << " * "; }
cout << endl;
} // for(row)
return 0;

YOU NEED TO PUT AN CLOSING BRACE HERE!!!

} // main() end

Try correcting the indenting to see the real structure of this code.

I did that for you above.
To help with that, /always/ put {} braces around a loop body.

I did that for you above.

Now you tell us what it is supposed to do, and what it is doing that is not
what you want.
(....and be sure you do not top-post! Please.)
 
A

asif929

BobR said:
(e-mail address removed) wrote in message ...

Why did you start another thread when you already had one going?


/*
std::istringstream icin("R \n");
cout << "Enter a character:>";
int n;
icin >> n;
cout << n <<std::endl;
// out: Enter a character:>10673684

You asked for a 'character'! The 'cin' to an 'int' will fail, you get
undefined behavior!
*/

std::istringstream icin("4 \n");
cout << "Enter a single number:>";
int n(0); // INITIALIZE!!!
icin >> n;
if( not n ){ return EXIT_FAILURE;} // ALWAYS check your input.
// or: if( n < 1 || n > 1000 ){ return EXIT_FAILURE;}
cout << n <<std::endl;
// out: Enter a single number:>4


YOU NEED TO PUT AN CLOSING BRACE HERE!!!

} // main() end



I did that for you above.


I did that for you above.

Now you tell us what it is supposed to do, and what it is doing that is not
what you want.
(....and be sure you do not top-post! Please.)

Hi again! first of all the program i wrote it in the begining of the
post it compiles an run. while i pasted the program in the board i
missed the closing main bracket " }". The problem i had with the
program was its not changing the size of the triangles except the first
triangle, and the angles of the last three triangles are not right or
out of shape.

The program you modified i tried and i compiled and ran it. It gives me
the same problem. Now the first triangle is no more look like a
triangle. The example below shows how the output looks like now:

Enter a size of triangles in integer :> 5


* * *

* *

*




* * * *
* * *
* *
*



* * * *
* * *
* *
*


* * * *
* * *
* *
*
 
D

David Harmon

On 18 Nov 2006 19:14:16 -0800 in comp.lang.c++, (e-mail address removed)
wrote,
for ( int column = 0; column < (3 - row); column++ )

Where did that magic number 3 come from?
It will be right for one value of n, wrong for all others.
Should be (n - row) or something similar (you choose how similar.)
 
A

asif929

David said:
On 18 Nov 2006 19:14:16 -0800 in comp.lang.c++, (e-mail address removed)
wrote,

Where did that magic number 3 come from?
It will be right for one value of n, wrong for all others.
Should be (n - row) or something similar (you choose how similar.)

It also didn't work (n - row)

SAM
 
B

BobR

(e-mail address removed) wrote in message ...
Hi again! first of all the program i wrote it in the begining of the
post it compiles an run. while i pasted the program in the board i
missed the closing main bracket " }". The problem i had with the
program was its not changing the size of the triangles except the first
triangle, and the angles of the last three triangles are not right or
out of shape.

The program you modified i tried and i compiled and ran it. It gives me
the same problem. Now the first triangle is no more look like a
triangle. The example below shows how the output looks like now:

I simulated an input. Did you remove (or comment out) the 'istringstream'? :
/*
std::istringstream icin("4 \n");
cout << "Enter a single number:>";
int n(0); // INITIALIZE!!!
icin >> n;
*/
If not, it will always input a '4' to int 'n'. You want:

cout << "Enter a single number:>";
int n(0);
cin >> n;

Or, if you want to use the 'istringstream during testing, just change the
number in it:

// std::istringstream icin("4 \n");
std::istringstream icin("5 \n");

Then, once your program is correct, go back to 'keyboard input'.
Enter a size of triangles in integer :> 5

* * *

* *

*

check your first loop. If n == 5:

for( int row = 0; row < n; ++row ){
// loops 4 times
}

for( int row = 0; row <= n; ++row ){ // add the '<='
// loops 5 times
}

Inside your first for() loop the second included for() loop was:

for( int column = 0; column < (3 - row); ++column ){
cout << " * ";
}

On the final iteration (assume n==5, so, row == 5, (using the '<=')), you
will have (3 - 5)( == -2). Is that what you want? Or, do you think ( n - 1 -
row ) would make it more flexible?

In your program comment out ( /* */ ) all but the first for() loop set, then
get that working. Then work on the second set, etc..

It's common practice to:
1 - get a basic program up and running.
2 - add *one* thing.
3 - test that.
4 - add another thing.
5 - test that.
repeat 4,5, etc.
 
D

David Harmon

On 18 Nov 2006 19:14:16 -0800 in comp.lang.c++, (e-mail address removed)
wrote,

Where did that magic number 3 come from?
It will be right for one value of n, wrong for all others.
Should be (n - row) or something similar (you choose how similar.)

It also didn't work (n - row) [/QUOTE]

But, more importantly, do you agree with my reasoning.
I hinted already that you would have to think about it
to get it exactly right.

What part didn't work? The fact that some places you output three
chars and other places one char, so your columns don't line up?

The whole benefit from this exercise comes from thinking about the
code, not from the marvelous result that you see when it "works".
 
A

asif929

David said:
It also didn't work (n - row)

But, more importantly, do you agree with my reasoning.
I hinted already that you would have to think about it
to get it exactly right.

What part didn't work? The fact that some places you output three
chars and other places one char, so your columns don't line up?

The whole benefit from this exercise comes from thinking about the
code, not from the marvelous result that you see when it "works".[/QUOTE]

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.
 
D

David Harmon

What part didn't work? The fact that some places you output three
chars and other places one char, so your columns don't line up?
[/QUOTE]
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.

Well, I mentioned why your columns don't line up.
Width of ' ' vs. " * "

But as soon as that is settled, I am more interested in the difference
between n and 3, where n==4. What would the magic number be if n==5?
 
B

BobR

(e-mail address removed) wrote in message ...
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::eek:stringstream 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="<<column<<"\n";
cout << ' ';
for( int column = 0; column < (3 - row); column++ )
sos<<" for C row="<<row<<" column="<<column
<<" (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::eek:stringstream 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="<<column<<"\n";
cout<<" ";
}
for( int column = 0; column <= (n-1-row); ++column ){
sos<<" for C row="<<row<<" column="<<column
<<" (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).
 
D

David Harmon

On 18 Nov 2006 19:14:16 -0800 in comp.lang.c++, (e-mail address removed)
wrote,
*
* *
* * *
* * * *

I guess the assignment is turned in by now, but you forgot
to show us the final code! Here's mine:


#include <iostream>
static const char *q[] = {" ", "* "};

void tri(bool flip, bool mirror, int size)
{
for (int row = 0; row < size; row++) {
for (int col = 0; col < size; col++)
std::cout << q[(flip ? row : size-row-1)
<= (mirror ? col : size-col-1)];
std::cout << "\n";
}
std::cout << "\n";
}

int main()
{
int n = 6;
tri(0, 0, n);
tri(1, 0, n);
tri(1, 1, n);
tri(0, 1, n);
}
 
A

asif929

David said:
On 18 Nov 2006 19:14:16 -0800 in comp.lang.c++, (e-mail address removed)
wrote,
*
* *
* * *
* * * *

I guess the assignment is turned in by now, but you forgot
to show us the final code! Here's mine:


#include <iostream>
static const char *q[] = {" ", "* "};

void tri(bool flip, bool mirror, int size)
{
for (int row = 0; row < size; row++) {
for (int col = 0; col < size; col++)
std::cout << q[(flip ? row : size-row-1)
<= (mirror ? col : size-col-1)];
std::cout << "\n";
}
std::cout << "\n";
}

int main()
{
int n = 6;
tri(0, 0, n);
tri(1, 0, n);
tri(1, 1, n);
tri(0, 1, n);
}


Hello again!
I am sorry for not sending you the correct version of the program
earlier. David your program works good and very interesting to see the
coding in funcations. But, you forgot to add the prompt.

Thanks to everybody for the help.

#include <iostream>

using namespace std;

int main()
{
int size, lineNum, charNum;
cout << "Enter a positive integer: " ;
cin >> size;

for (lineNum = 1; lineNum <= size; lineNum++)
{
for (charNum = 1; charNum <= lineNum; charNum++)
cout << "* ";
cout << endl;
}
cout << endl;

for (lineNum = size; lineNum >= 1; lineNum--)
{
for (charNum = lineNum; charNum >= 1; charNum--)
cout << "* ";
cout << endl;
}
cout << endl;

for (lineNum = size; lineNum >= 1; lineNum--)
{
for (charNum = 1; charNum <= size - lineNum; charNum++)
cout << " ";
for ( ; charNum <= size; charNum++)
cout << "* ";
cout << endl;
}
cout << endl;

for (lineNum = 1; lineNum <= size; lineNum++)
{
for (charNum = 1; charNum <= size - lineNum; charNum++)
cout << " ";
for ( ; charNum <= size; charNum++)
cout << "* ";
cout << endl;
}
cout << endl;
}
 
D

David Harmon

On 4 Dec 2006 16:53:55 -0800 in comp.lang.c++, (e-mail address removed)
wrote,
But, you forgot to add the prompt.

I left it out because it was uninteresting
(and you had that part fine.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top