Different orientation on this triangle

  • Thread starter Demon117
  • Start date
  • #1
Demon117
165
1
I am trying to get a different orientation on this triangle; specifically I want this output:

**********
*********
********
*******
******
*****
****
***
**
*

Here is the code that I have

Code:
#include <iostream>
using namespace std;

int main () 
{
	int lines=10;
	int start = 0;
	int dots;
	while (start < lines) {
		// We start at 0, then gradually increment
		for (dots = 0; dots <= start; dots++)
			cout << "*";
		cout << endl;
		start++;// Increment
	}
    system("pause");
	return 0;
}

and it generates this output:

*
**
***
****
*****
******
*******
********
*********
**********

What do I need to change?
 

Answers and Replies

  • #2
DavidSnider
Gold Member
510
146


Have the for loop count down instead of up?
 
  • #3
Demon117
165
1


Have the for loop count down instead of up?

Nope, this has been tried already and it produces an infinite loop.
 
  • #4
DavidSnider
Gold Member
510
146


Nope, this has been tried already and it produces an infinite loop.

Then I think you went wrong somewhere. Can you post code again?
 
  • #5
Grep
298
2


Nope, this has been tried already and it produces an infinite loop.
If it goes into an infinite loop, you're doing it wrong.

There's two ways to do it that I can think of. Either count through the while loop as you do now, and work out how many asterisks you want. Or count the other way in your while loop. I'd do the former, probably.

Assuming you're printing, say, 7 lines. You start out and the counter is 0. How many asterisks to print? 7 right? Next line, counter is 1. So you print 7-1=6 asterisks. In general, it's (lines - counter) right?
 
  • #6
DavidSnider
Gold Member
510
146


for extra credit you might want to build a function like:

void printStars(int numStars) {
...
}

then in main:
for(int i=0;i<lines;i++) {
printStars(lines - i);
}
 
  • #7
Demon117
165
1


Then I think you went wrong somewhere. Can you post code again?

Here:

Code:
#include <iostream>
using namespace std;

int main () 
{
	int lines=10;
	int start = 0;
	int dots;
	while (start < lines) {
		// We start at 0, then gradually increment
		for (dots = 0; dots <= start; dots++)
			cout << "*";
		cout << endl;
		start++;// Increment
	}
    system("pause");
	return 0;
}
 
  • #8
DavidSnider
Gold Member
510
146


I meant post the code where you counted down and got the infinite loop problem :smile:

But I'd try the method I suggested earlier with an external function to print each line first.

Here:

Code:
#include <iostream>
using namespace std;

int main () 
{
	int lines=10;
	int start = 0;
	int dots;
	while (start < lines) {
		// We start at 0, then gradually increment
		for (dots = 0; dots <= start; dots++)
			cout << "*";
		cout << endl;
		start++;// Increment
	}
    system("pause");
	return 0;
}
 
  • #9
chiro
Science Advisor
4,815
134


Like other people have said change the loop.

When you change the loop to go from sup(Set) to inf(Set) you also need to change the comparisons you make from less than to greater than.

Also make sure you are using signed numbers. You can get in an infinite loop if you use these incorrectly.
 

Suggested for: Different orientation on this triangle

Replies
2
Views
268
Replies
8
Views
1K
  • Last Post
3
Replies
89
Views
3K
  • Last Post
Replies
19
Views
599
  • Last Post
2
Replies
54
Views
981
  • Last Post
Replies
19
Views
326
  • Last Post
Replies
7
Views
406
Replies
17
Views
271
Replies
1
Views
110
  • Last Post
Replies
2
Views
559
Top