Creating a pyramid using C++

  • C/++/#
  • Thread starter jacy
  • Start date
  • #1
76
0

Main Question or Discussion Point

Hello,
I am trying to print an equilateral triangle, made up of * (stars), of height 8. Can someone please give me some hint. I am not getting the desired output. Thank you. Here is my code.

Code:
#include<iostream.h>
#include<iomanip.h>
void main()
{
  int n=8;


	 for (int row=1; row<=8; row++)
	 {
		  for (int col=0; col<row; col++)
		  {

				cout<<setw(n=n-1)<< '*';

		  }
		  cout << endl;  // end the line.
	 }

}
 
Last edited:

Answers and Replies

  • #2
1,356
2
ew c++ io commands, can't remmeber what the setw command does so it may be best for you to post the errored output you got
...and just printing stars you won't get a real equilateral triangle so i'm guessing your looking for 8 stars each edge.

also are you filling the triangle?

and you can use n-- rather then n=n-1...doesn'treallly matter but n-- looks nicer.

my best guess though is your forgetting to print spaces.
 
  • #3
jtbell
Mentor
15,581
3,559
jacy said:
Hello,
I am trying to print an equilateral triangle, made up of * (stars), of height 8.
Please show us exactly what this triangle is supposed to look like, by literally typing it out inside a (code)(/code) block. (square brackets instead of parentheses, of course).
 
  • #4
972
0
Code:
#include<iostream.h>

main()
{
  int n=8;

	 cout<<"45,90,45 degree triangle"<<endl;
	 for (int y=1; y<=8; y++)
	 {
		for(int space=8-y; space>0; space--)
		{
			cout<<" ";
 		}	
		for (int x=0; x<y; x++)
		{
			cout<<"**";
		}		 
		cout << endl;  // end the line.
	}

	cout<<endl<<"circle"<<endl;
	for (double y=-10; y<=10; y++)
	{
		for (double x=-10; x<=10; x++)
		{
			if(x*x+y*y<=100)
				cout<<"*";
			else
				cout<<" ";
		}		
		cout << endl;  // end the line.
	}

	cout<<endl<<"Equilateral Triangle"<<endl;
	for (double y=7*sqrt(3); y>=0; y--)
	{
		for (double x=-7; x<=7; x++)
		{
		if(x==0)
			cout<<"*";
		else if(x<0&&y/(x+7)<=sqrt(3))
			cout<<"*";
		else if(x>0&&y/(7-x)<=sqrt(3))
			cout<<"*";
		else
			cout<<" ";
		}		
		cout << endl;  // end the line.
	}
	system("PAUSE");
}
 

Attachments

Last edited:
  • #5
76
0
Thanks for the help
 
  • #6
972
0
Your Welcome. :smile:
 
  • #7
Hurkyl
Staff Emeritus
Science Advisor
Gold Member
14,916
19
Please don't do people's homework for them. :grumpy:
 
  • #8
1
0
something wrong

when i tried to run the programm it had some problems.this is how i change it but it works

#include<iostream.h>
#include <cmath>//the first change
int main()
{
int n=8;


cout<<"45,90,45 degree triangle"<<endl;
for (int y=1; y<=8; y++)
{
for(int space=8-y; space>0; space--)
{
cout<<" ";
}
for (int x=0; x<y; x++)
{
cout<<"**";
}
cout << endl; // end the line.
}

cout<<endl<<"circle"<<endl;
for ( y=-10; y<=10; y++)//second change
{
for (double x=-10; x<=10; x++)
{
if(x*x+y*y<=100)
cout<<"*";
else
cout<<" ";
}
cout << endl; // end the line.
}

cout<<endl<<"Equilateral Triangle"<<endl;
for (y=7*sqrt(3); y>=0; y--)//third change
{
for (double x=-7; x<=7; x++)
{
if(x==0)
cout<<"*";
else if(x<0&&y/(x+7)<=sqrt(3))
cout<<"*";
else if(x>0&&y/(7-x)<=sqrt(3))
cout<<"*";
else
cout<<" ";
}
cout << endl; // end the line.
}

return 0;//fourth change

}
 

Related Threads on Creating a pyramid using C++

Replies
6
Views
3K
  • Last Post
Replies
3
Views
465
  • Last Post
Replies
5
Views
3K
  • Last Post
Replies
4
Views
3K
Replies
5
Views
4K
Replies
2
Views
4K
Top