473,378 Members | 1,377 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Need Help with writing a Triangle program

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;

Nov 19 '06 #1
12 2782

as*****@hotmail.com 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
Alf P. Steinbach wrote:
>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.)

--
Bob R
POVrookie
Nov 19 '06 #2

BobR wrote:
as*****@hotmail.com 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
Alf P. Steinbach wrote:
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.)

--
Bob R
POVrookie
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
* * *

* *

*


* * * *
* * *
* *
*

* * * *
* * *
* *
*
* * * *
* * *
* *
*

Nov 19 '06 #3
On 18 Nov 2006 19:14:16 -0800 in comp.lang.c++, as*****@hotmail.com
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.)

Nov 19 '06 #4

as*****@hotmail.com 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.

--
Bob R
POVrookie
Nov 19 '06 #5

David Harmon wrote:
On 18 Nov 2006 19:14:16 -0800 in comp.lang.c++, as*****@hotmail.com
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.)
It also didn't work (n - row)

SAM

Nov 19 '06 #6
On 19 Nov 2006 11:05:56 -0800 in comp.lang.c++, as*****@hotmail.com
wrote,
>
David Harmon wrote:
>On 18 Nov 2006 19:14:16 -0800 in comp.lang.c++, as*****@hotmail.com
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.)

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

Nov 19 '06 #7

David Harmon wrote:
On 19 Nov 2006 11:05:56 -0800 in comp.lang.c++, as*****@hotmail.com
wrote,

David Harmon wrote:
On 18 Nov 2006 19:14:16 -0800 in comp.lang.c++, as*****@hotmail.com
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.)
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".
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.

Nov 19 '06 #8
On 19 Nov 2006 12:13:37 -0800 in comp.lang.c++, as*****@hotmail.com
wrote,
>David Harmon wrote:
>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?
>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?

Nov 19 '06 #9

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::ostringstream 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::ostringstream 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).

--
Bob R
POVrookie
Nov 19 '06 #10
On 18 Nov 2006 19:14:16 -0800 in comp.lang.c++, as*****@hotmail.com
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);
}

Nov 28 '06 #11

David Harmon wrote:
On 18 Nov 2006 19:14:16 -0800 in comp.lang.c++, as*****@hotmail.com
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;
}

Dec 5 '06 #12
On 4 Dec 2006 16:53:55 -0800 in comp.lang.c++, as*****@hotmail.com
wrote,
>But, you forgot to add the prompt.
I left it out because it was uninteresting
(and you had that part fine.)

Dec 5 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Starting_Anew | last post by:
Hi i just started a programing class a few weeks ago. and i need help drawing lines...I know this may seem very simple to everyone here but i am struggling. Ok I am writing a program to create a...
22
by: Rajshekhar | last post by:
Hi , i am writing a simple prgm to read a .txt file then store the contents into the array... program as follows: -------------------------- #include<stdio.h> int main() { FILE *fp1;
4
by: orel | last post by:
<o_r_l_25@yahoo.frwrote, As the message says, to help the compiler with template parsing, I add 'typename' as you said, it now understands the expression, but but i have now those complier...
1
by: Dave128 | last post by:
I am trying to write a program for an assignment that uses a For loop to produce an output that looks like this: * ********** ********** * ** ...
4
by: asif929 | last post by:
I have another program to write, i will appreciate if somebody can help......prompts the user to enter positive integer, and then prints out four triangles For Example: If we enter 4 it should...
0
by: tyson verdez | last post by:
imma beginner and my iinstructur doesnt do hands on wit us we learn out of a lecture but it doesnt help some1 please help me 1. (The MyTriangle class) Write a class/program named MyTriangle. ...
1
by: albertjun | last post by:
i am new to php and need some help. i have 2 pages one is html and other is .php when i run the program it just spits out the code. i am stumped here is the html file <html> <Head>...
6
by: jackj | last post by:
Hi, I am first time C++ student and doing the usual tasks. This one is to create a triangle based on user input of how large (how many rows) and what symbol to use. I have managed to create a...
9
by: pereges | last post by:
Hello I need some ideas for designing a recursive function for my ray tracing program. The idea behind ray tracing is to follow the electromagnetic rays from the source, as they hit the...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.