Need help with Triangles program

A

asif929

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
 
A

Alf P. Steinbach

* (e-mail address removed):
#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

ashish1711

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+
 
A

asif929

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 :)
 
B

BobR

(e-mail address removed) 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?!?
 

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,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top