Jump to content
  • Advertisement
Sign in to follow this  
phil67rpg

triangle question

This topic is 1998 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am brushing up on my c++ by reading a book and doing exercises. My problem is that I am able to print out:
X
XX
XXX
XXXX
XXXXX

but I want to print out:
X
XXX
XXXXX
XXXXXXX

here is the code I am using
int main()
{
	for(int i=1; i<=5; i++)
	{
		for(int j=1; j <= i; j++)
		{
		cout << "X"; 
		}
		
		cout << endl;
	}
	cout << endl;
	system("pause");

	return 0; 
}
I know this is very simple but I need a little hint.
I have also googled this but I cant find a simple answer.

Share this post


Link to post
Share on other sites
Advertisement

You need to print out the correct number of spaces before you start printing out X's. Since it's an exercise I won't straight out give you the answer, but here's a hint: its got something to do with the row number you are printing and the total number of rows there are. Another hint: you are going to need to have 2 loops inside the "each row" loop.

 

Make sure that the number of rows to print out occurs exactly once in the code as well (have a local variable numrows inside main which you initialise with the number of rows you want to print).

Share this post


Link to post
Share on other sites

You don't actually need 2 loops inside the "each row" loop, you could do it with an if(some condition) /*print a space*/ else /*print an X*/. I think the 2 loops method is probably better though.

Share this post


Link to post
Share on other sites

At the moment you're incrementing by one each loop but the pattern adds two Xs each time.

 

One possible solution below the fold.

 

[spoiler]

int main()
{
    int times_to_loop = 10;
    int longest_line = times_to_loop * 2;

    for(int i = 0, j = 1; i < times_to_loop; i++, j += 2)
    {
        int padding = (longest_line - j) / 2;
        cout << string(padding, ' ') << string(j, 'x') << endl;
    }

    system("pause");
    return 0;
}

[/spoiler]

 

Edit #2: Corrected the example to meet the triangle requirement which I missed.

Edited by Indifferent

Share this post


Link to post
Share on other sites

wow this programming stuff sure is challenging, is there a certain way of thinking about programming

There are lots of ways smile.png

Personally i tend to break things down into as small pieces as possible.

the triangle should look like this: (- is a space)

----X
---XXX
--XXXXX
-XXXXXXX
XXXXXXXXX

so the first row (row 0) has 4 spaces and 1 X
second row has 3 spaces and 3 X
third row has 2 spaces and 5 X
fifth row(row 4) has 0 spaces and 9 X
etc.

We got a pattern here, what is the pattern ? (If you can describe the pattern it becomes trivial to write an application that prints it)

Share this post


Link to post
Share on other sites

Personally i tend to break things down into as small pieces as possible.

so do I, and yes I will ponder the pattern.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!