Creating a pyramid using C++

In summary, the code prints an equilateral triangle, but the triangle does not look right. The first change makes the triangle have 45, 90, and 45 degree angles, but the triangle still does not look right. The second change makes the triangle have y=7*sqrt(3), but the triangle still does not look right. The third change makes the triangle have double y=-10 to y=10, but still the triangle does not look right.
  • #1
jacy
76
0
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:
Technology news on Phys.org
  • #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
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
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

  • 45triangle-circle-60triangle.gif
    45triangle-circle-60triangle.gif
    8.4 KB · Views: 753
Last edited:
  • #5
Thanks for the help
 
  • #6
Your Welcome. :smile:
 
  • #7
Please don't do people's homework for them. :grumpy:
 
  • #8
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

}
 

1. How do I create a pyramid using C++?

To create a pyramid using C++, you will need to use a combination of for loops and if statements to print out the desired pattern. You will also need to use variables to keep track of the number of stars or spaces to print on each line.

2. What is the basic structure of a pyramid in C++?

The basic structure of a pyramid in C++ involves using nested for loops to print out the desired number of rows and columns. The outer loop controls the number of rows, while the inner loop controls the number of columns. You will also need to use if statements to print out the correct number of stars or spaces on each line.

3. How do I control the size of the pyramid in C++?

You can control the size of the pyramid in C++ by using variables to keep track of the number of rows and columns to print. You can also use user input to allow for different sizes of the pyramid to be created.

4. Can I create a pyramid with different shapes using C++?

Yes, you can create a pyramid with different shapes using C++. You will need to use different patterns and modify the for loops and if statements accordingly to achieve the desired shape.

5. Is there a library or function in C++ that can help me create a pyramid?

No, there is no specific library or function in C++ that is designed specifically for creating a pyramid. However, there are built-in functions and libraries that can help with printing out patterns and controlling the output of the program.

Similar threads

  • Programming and Computer Science
Replies
15
Views
1K
Replies
10
Views
904
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
2
Replies
66
Views
4K
  • Programming and Computer Science
Replies
1
Views
965
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
Back
Top