Different orientation on this triangle

Click For Summary

Discussion Overview

The discussion revolves around modifying a C++ program to produce a specific output pattern of asterisks in a descending triangle format. Participants explore different approaches to achieve this output, focusing on loop structures and potential pitfalls in coding.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant shares their initial code that produces an ascending triangle instead of the desired descending triangle.
  • Another participant suggests changing the for loop to count down instead of up.
  • A subsequent reply indicates that counting down has been attempted but resulted in an infinite loop, prompting a request for the problematic code.
  • Further discussion suggests two methods to achieve the desired output: counting down in the while loop or calculating the number of asterisks to print based on the current line number.
  • A participant proposes creating a function to handle the printing of asterisks, which could simplify the main logic.
  • Another participant emphasizes the need to adjust loop comparisons when changing the loop direction and mentions the importance of using signed numbers to avoid infinite loops.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach, with multiple competing views on how to modify the loop structure and handle the output. The discussion remains unresolved regarding the specific implementation details.

Contextual Notes

There are unresolved issues related to the infinite loop encountered when attempting to count down, as well as potential misunderstandings about loop conditions and variable types.

Demon117
Messages
162
Reaction score
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?
 
Technology news on Phys.org


Have the for loop count down instead of up?
 


DavidSnider said:
Have the for loop count down instead of up?

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


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

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


matumich26 said:
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?
 


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);
}
 


DavidSnider said:
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;
}
 


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.

matumich26 said:
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;
}
 


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.
 

Similar threads

Replies
12
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
3
Views
2K