How to Create a Pyramid in C++?

  • Context: C/C++ 
  • Thread starter Thread starter jacy
  • Start date Start date
  • Tags Tags
    C++ Pyramid
Click For Summary

Discussion Overview

The discussion revolves around creating an equilateral triangle using stars in C++. Participants are seeking assistance with code implementation and output formatting, with a focus on achieving the desired visual representation of the triangle.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant shares their initial code and requests hints for correcting the output of an equilateral triangle of height 8.
  • Another participant questions the use of the `setw` command and suggests that the output may not represent a true equilateral triangle, proposing that the participant might be looking for a specific number of stars on each edge.
  • A different participant requests a visual representation of the expected triangle output to better understand the issue.
  • One participant provides an alternative code snippet that includes a triangle and a circle, but it contains several errors and inconsistencies, such as using `count` instead of `cout` and missing type declarations.
  • Another participant expresses gratitude for the help received.
  • A later reply cautions against doing homework for others, indicating a concern about academic integrity.
  • One participant mentions experiencing problems with their code and shares their modified version, highlighting changes made to fix the issues, although it still contains errors.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach to create the triangle, and multiple competing views and code snippets are presented throughout the discussion.

Contextual Notes

There are unresolved issues regarding the correctness of the code snippets provided, including syntax errors and the use of undefined variables. The discussion also lacks clarity on the exact visual requirements for the triangle.

Who May Find This Useful

Individuals interested in C++ programming, particularly those learning about output formatting and graphical representations using text characters.

jacy
Messages
76
Reaction score
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
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.
 
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).
 
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: 830
Last edited:
Thanks for the help
 
Your Welcome. :smile:
 
Please don't do people's homework for them.
 
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;


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

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

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

return 0;//fourth change

}
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 33 ·
2
Replies
33
Views
7K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K