473,747 Members | 2,762 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2814

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::istringstr eam icin("R \n");
cout << "Enter a character:>";
int n;
icin >n;
cout << n <<std::endl;
// out: Enter a character:>1067 3684

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

std::istringstr eam 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::istringstr eam icin("R \n");
cout << "Enter a character:>";
int n;
icin >n;
cout << n <<std::endl;
// out: Enter a character:>1067 3684

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

std::istringstr eam 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::istringstr eam 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::istringstr eam icin("4 \n");
std::istringstr eam 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::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
Nov 19 '06 #10

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

Similar topics

10
6501
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 right triangle and i cant seem to draw a horizontal line..i want to draw it from the bottom of the vertical line. I am writing an applet. Please help..
22
1865
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
2229
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 errors : src\core\objectfactory.cpp:41: instantiated from here src\include\objectfactory.h:125: error: no matching function for call to `std::pair<const int, Shape*>::pair(const int&, Shape**(*&)())' \c++\3.4.2\bits\stl_pair.h:69: note:...
1
1224
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
2771
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 shows * * * * * * * * * *
0
1193
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. Within the class, define two methods as follows. /** Returns true if the sum of ANY two sides is greater than the third side. Otherwise, returns false. */ public static boolean isValid(double side1, double side2, double side3)
1
1126
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> <title>Triangle</title> </head>
6
13365
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 triangle that aligns to the left of the screen, but it loops and disappears immediately. So, my problem is getting it to stay on the screen and then ask the user if they want to create another one AND to make it print the first symbol in the center of...
9
2635
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 object.The object is triangulated. The rays can undergo multiple reflections, diffractions etc of the same object i.e. a ray hits a surface of the object, undergoes reflection resulting in a reflected ray which can again hit a surface, corner or edge...
0
8979
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8818
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9223
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8233
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4588
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4860
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3296
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2771
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2203
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.