Jump to content
Sign in to follow this  
Vasto

C++ Isosceles Triangle Question

Recommended Posts

For my C++ class, I need to make an isoscles triange, and I can't seem to get it to line up right, or I get them to enter an infinite loop. Any help would be appreciated, I've been trying to figure it out for hours (not an exageration)

/*
* Filename: isosceles.cpp
* Created by: XXXXXXXX
* Description: Prints an isosceles triangle with a base of n and a height of n
* If n=5, then the output should look like:
*	 X
*	XXX
*   XXXXX
*/

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
 int n=1;

  //Ask user input for "n"
  cout << "Enter the triangle's base:";
  cin >> n;

  // For Statement pertaining to the rows of the triangle
 for (int row = 1; row <= n; row++)
   {
		//Print blank spaces to the left of the x tomake the triangle look like a pyramid
			for (int space = (n+2)/2; space >= row; space--)
			{
					cout << "c";
			}
	 // For Statement pertaining to the columns of the triangle.
			for ( int col = 1; col <= row; col++)
			{

					cout << "x";
			}
	 cout << endl; //print newline to finish row.
   }
}
/*
//This part is for later when I need to determine if it is even or odd. I put it on the bottom just as a reminder.
int m = n%2
if ( m==0)
	{
	cout << "m is even"
	}
else
	{
	cout << "m is odd"
	}
*/

Share this post


Link to post
Share on other sites

Are even inputs allowed? It's not really stated in your post, but I would assume they're not. I'm not sure how you'd get the centering right otherwise.

Share this post


Link to post
Share on other sites

May not be the best way to solve the problem, but I came up with two solutions.

 

/*
* Filename: isosceles.cpp
* Created by: XXXXXXXX
* Description: Prints an isosceles triangle with a base of n and a height of n
* If n=5, then the output should look like:
*	 X
*	XXX
*   XXXXX
*/

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
 int n=1;
int spaces = 0;
int stars = 0;
int row = 0;

  //Ask user input for "n"
  cout << "Enter the triangle's base:";
  cin >> n;

/*
* Bitwise test to see if a number is even or odd. 
* If bit 0 (decimal 1) is set, then the number must be odd.
*				128 ....  8 4 2 1
* e.g 3 in binary 0 0 0 0  0 0 1 1
*	 4 in binary 0 0 0 0  0 1 0 0
*/
if (n & 1)
{
cout << "Odd" << endl;
} else {
cout << "Even" << endl;
}

// triple for loop
// row = 3,4,5
for (row = ceil((float)n / 2); row <= n; row++)
{
	// spaces = 5 - 3,4,5
	spaces = n - row;
	// stars = 3,4,5 - spaces
	stars = row - spaces;

	//Print blank spaces to the left of the x tomake the triangle look like a pyramid
	for (int space = 0; space < spaces; space++)
	{
		cout << " ";
	}   

	// For Statement pertaining to the columns of the triangle.
	for ( int col = 0; col < stars; col++)
	{
		cout << "x";
	}

	cout << endl; //print newline to finish row.
}


// double for loop version
// interestingly, the row number corresponds to the total amount of characters on that line (spaces + x's)
// we can use this to our advantage.
for (row = ceil((float)n / 2); row <= n; row++)
{
	// spaces = n - row
	// e.g for n = 5 (_ = space)
	// row = 3	_ _*		  (spaces = 5 - 3 = 2)
	// row = 4	_ ***		 (spaces = 5 - 4 = 1)
	// row = 5	 *****		(spaces = 5 - 5 = 0)
	spaces = n - row;

	// for each character on the line
	for ( int col = 0; col < row; col++)
	{
		// if current col is smaller than spaces, print a space
		// when col goes over spaces, start printing x's till the end of the loop
		(col < spaces) ? cout << " " : cout << "x";
	}

	cout << endl; //print newline to finish row.
}


system("pause");
}
/*
//This part is for later when I need to determine if it is even or odd. I put it on the bottom just as a reminder.
int m = n%2
if ( m==0)
	{
	cout << "m is even"
	}
else
	{
	cout << "m is odd"
	}
*/

 

Comments are poor. It's late / early here (2AM). You may find it useful to run through each of them on paper or something. You can also check odd / even with a simple bitwise test.

Share this post


Link to post
Share on other sites

Here's what I came up with for the for loop section:

 

for(int row = 0; row <= n/2, row++)

{

int xNum = row * 2 - 1;

for(int space = (n+2)/2; space > row; space--)

{

cout<<" ";

}

for(int col = 0; col <= xNums + 1; col++)

{

cout<<"X";

}

cout<<endl;

}

 

Pardon for no comments, and if this doesn't even work right. I wrote it in Python and translated it the best I could. But rock on for learning programming!

Share this post


Link to post
Share on other sites

give me 5 mins to get dev cpp installed, and i'll see what i can do.

 

edit looks like markie came to the rescue. i'm gonna do this anyways just to challenge me...it's been 3 years since i've done any c++ coding :)

Edited by airman

Share this post


Link to post
Share on other sites

Thanks for the help everyone! I was able to get it and make it cout even triangles (by making two x's on the top)

Share this post


Link to post
Share on other sites
/*
* Filename: isosceles.cpp
* Created by: XXXXXXXX
* Description: Prints an isosceles triangle with a base of n and a height of n
* If n=5, then the output should look like:
*	 X
*	XXX
*   XXXXX
*/

how does the height equal 5 in that example?

 

shouldn't the height be 2n - 1? oh lol, and n = 3... bah, it's 5am, base != height anyway lol

Share this post


Link to post
Share on other sites

Oh, good point. I guess the desription was wrong. I'm glad everyone understood what I was still trying to do though. Programming really is not my cup of tea.

Share this post


Link to post
Share on other sites

You should point out that the question is wrong and that you solved for what you assumed was being asked. You might get a bonus point or a golden star or something haha

Share this post


Link to post
Share on other sites

I was going to point out what hardnrg said last night, but I didn't for fear of misunderstanding and appearing stupid :lol: (Yeah, I don't know any C++ :P)

Share this post


Link to post
Share on other sites
I was going to point out what hardnrg said last night, but I didn't for fear of misunderstanding and appearing stupid :lol: (Yeah, I don't know any C++ :P)

Between not understanding how the height was n (5) in that example and not knowing what to do with even n's, I figured I'd wait for answers before I spent time on the code :) But by then, markie had already done it :P

Share this post


Link to post
Share on other sites
Oh, good point. I guess the desription was wrong. I'm glad everyone understood what I was still trying to do though. Programming really is not my cup of tea.

I actually just copied the header from another program that I made (for right trangles) and modified it incorrectly. The question in the assignment was actually correct. lol

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×