473,395 Members | 1,791 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,395 software developers and data experts.

Need help with Triangles program

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

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

I have also tried to create this program but i couldn't be able to
correct the formating of "*".
UW PICO(tm) 4.10
File: triangle.cpp

#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;
} // function main

Nov 18 '06 #1
4 2760
* as*****@hotmail.com:
#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;
}
Try correcting the indenting to see the real structure of this code.

To help with that, /always/ put {} braces around a loop body.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 18 '06 #2
Hello Asif. Actually i mentally trace your program, but its not
flexible. What i mean by flexible is that supposed you enter 5 at the
beginning, then i think it will not display 5 triangle. I have written
a program below. That one is flexible and it will display you the
number of triangle according to the integer that you entered at the
beginning Do run it and tell me if it works.

#include<iostream.h>
void main()
{
int n, a;
float b, c;

cout<<"Enter the number of triangle you wanna display: ";
cin>>n;

for(int i=0;i<n;i++)
{
int j, k, m;
a=i/4;
b=float(i)/4;
c=b-a;

if(c==0)
{
for(j=0;j<4;j++)
{
for(k=0;k<=j;k++)
{
cout<<"* ";
}
cout<<endl;

}
cout<<endl<<endl;
}

else if(c==0.25)
{
int l=4;
for(j=0;j<4;j++)
{

for(k=0;k<l;k++)
{
cout<<"* ";
}
cout<<endl;
l--;
}
cout<<endl<<endl;
}

else if(c==0.5)
{
int l=4;
for(j=0;j<4;j++)
{
for(m=0;m<j;m++)
{
cout<<" ";
}

for(k=0;k<l;k++)
{
cout<<"* ";
}
cout<<endl;
l--;

}
cout<<endl<<endl;
}

else if(c==0.75)
{
int l=3;
for(j=0;j<4;j++)
{
for(m=0;m<l;m++)
{
cout<<" ";
}

for(k=0;k<j+1;k++)
{
cout<<"* ";
}
l--;
cout<<endl;

}
cout<<endl<<endl;
}

}
}

Well i didnt get time to comment the program. But try to see the
importance of variable that i have used. Actually Exams are going on
for me, so later i'll try to tackle the chess game problem. Anw cya.
Hope the program run.A+


On Nov 18, 10:08 am, "Alf P. Steinbach" <a...@start.nowrote:
* asif...@hotmail.com:


#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;
}Try correcting the indenting to see the real structure of this code.

To help with that, /always/ put {} braces around a loop body.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?- Hide quoted text -- Show quoted text -
Nov 18 '06 #3
First of all thanks for your help and response. This program runs fine.
But it's not what i am trying to do. My program is suppose to print
only four triangles and wach triangle will have N rows ranging from 1
to N. If somebody enter SIX it prints six rows in each triangle.

Thanks for your program. Thumbs up :)

as********@gmail.com wrote:
Hello Asif. Actually i mentally trace your program, but its not
flexible. What i mean by flexible is that supposed you enter 5 at the
beginning, then i think it will not display 5 triangle. I have written
a program below. That one is flexible and it will display you the
number of triangle according to the integer that you entered at the
beginning Do run it and tell me if it works.

#include<iostream.h>
void main()
{
int n, a;
float b, c;

cout<<"Enter the number of triangle you wanna display: ";
cin>>n;

for(int i=0;i<n;i++)
{
int j, k, m;
a=i/4;
b=float(i)/4;
c=b-a;

if(c==0)
{
for(j=0;j<4;j++)
{
for(k=0;k<=j;k++)
{
cout<<"* ";
}
cout<<endl;

}
cout<<endl<<endl;
}

else if(c==0.25)
{
int l=4;
for(j=0;j<4;j++)
{

for(k=0;k<l;k++)
{
cout<<"* ";
}
cout<<endl;
l--;
}
cout<<endl<<endl;
}

else if(c==0.5)
{
int l=4;
for(j=0;j<4;j++)
{
for(m=0;m<j;m++)
{
cout<<" ";
}

for(k=0;k<l;k++)
{
cout<<"* ";
}
cout<<endl;
l--;

}
cout<<endl<<endl;
}

else if(c==0.75)
{
int l=3;
for(j=0;j<4;j++)
{
for(m=0;m<l;m++)
{
cout<<" ";
}

for(k=0;k<j+1;k++)
{
cout<<"* ";
}
l--;
cout<<endl;

}
cout<<endl<<endl;
}

}
}

Well i didnt get time to comment the program. But try to see the
importance of variable that i have used. Actually Exams are going on
for me, so later i'll try to tackle the chess game problem. Anw cya.
Hope the program run.A+


On Nov 18, 10:08 am, "Alf P. Steinbach" <a...@start.nowrote:
* asif...@hotmail.com:


#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;
}Try correcting the indenting to see the real structure of this code.
To help with that, /always/ put {} braces around a loop body.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?- Hide quoted text -- Show quoted text -
Nov 18 '06 #4

as*****@hotmail.com wrote in message ...
>First of all thanks for your help and response. This program runs fine.
WHAT program?!? (there were two in your post, and your response was below
neither!)
>But it's not what i am trying to do.
Yours or his?!?
My program is suppose to print
only four triangles and wach triangle will have N rows ranging from 1
to N. If somebody enter SIX it prints six rows in each triangle.
Thanks for your program. Thumbs up :)
Do NOT Top-Post! Remove parts of old post you are not refering to!

<http://www.parashift.com/c++-faq-lite/how-to-post.html>

What do you think the following meant?!?
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


Nov 18 '06 #5

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

Similar topics

4
by: kazack | last post by:
I posted a similiar question in this newsgroup already and got an answer which I already knew but didn't get the answer I was looking for so I am reposting the code and question differently in the...
42
by: Frank Buss | last post by:
I've setup a challenge, mainly for C++, Java and Lisp, but every other language is welcome: http://www.frank-buss.de/challenge/index.html There is nothing to win, but I hope there will be some...
3
by: satish | last post by:
frns i need a codef for solving common point for three circles pls any one help me please
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: * ********** ********** * ** ...
6
by: asif929 | last post by:
I have been trying to create this chess program from many hours and still couldn't figure out how to complete it. After consume all these efforts i come here for the first time for help. i would...
12
by: asif929 | last post by:
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...
6
by: pereges | last post by:
I hae compiled and executed codes of vector.c, reader.c , test.c seperately and they can be execute in proper manner and give correct outputs but when i tried to integrate them all into a single...
1
by: pereges | last post by:
in my program i have a piece of code like this - int left_count, right_count; left_count = right_count = 0; for all triangles { a = condition that x coordinates of all vertices are <=...
87
by: pereges | last post by:
I have a C program which I created on Windows machine. I have compiled and executed the program on windows machine and it gives me the consistent output every time i run it. for eg. input a = 2,...
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:
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...
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...
0
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,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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,...

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.