Public Group

triangle question

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

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 on other sites
I want to print out X
XXX
XXXXX
XXXXXXX
XXXXXXXXX

Share on other sites
I want to print out a triangle but for some reason this post wants to left justify.

Share on other sites

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 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 on other sites
I think I have left out a for loop for the spaces to be printed out.

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 on other sites
wow this programming stuff sure is challenging, is there a certain way of thinking about programming

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

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 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.

1. 1
2. 2
Rutin
20
3. 3
khawk
18
4. 4
A4L
14
5. 5

• 12
• 16
• 26
• 10
• 44
• Forum Statistics

• Total Topics
633763
• Total Posts
3013728
×