Different orientation on this triangle

In summary, you need to change the loop from inf(Set) to sup(Set), change the comparisons you make from less than to greater than, and make sure you are using signed numbers.
  • #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?
 
Technology news on Phys.org
  • #2


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


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

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


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?
 
  • #5


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?
 
  • #6


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


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;
}
 
  • #8


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;
}
 
  • #9


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.
 

Related to Different orientation on this triangle

What is the definition of different orientation on this triangle?

Different orientation on this triangle refers to the different ways in which a triangle can be positioned or rotated in space. This can be described in terms of the direction of its sides, angles, or vertices.

How many different orientations can a triangle have?

A triangle can have an infinite number of orientations, as it can be rotated or flipped in any direction in space.

How do you determine the orientation of a triangle?

The orientation of a triangle can be determined by looking at the direction of its sides, angles, or vertices. One common method is to use the right-hand rule, where the fingers of the right hand are curled in the direction of the sides and the thumb points in the direction of the normal vector.

Can two triangles have the same orientation?

No, two triangles cannot have the same orientation unless they are identical. Even if they have the same shape and size, if they are positioned or rotated differently in space, they will have different orientations.

Why is understanding different orientations important in geometry?

Understanding different orientations is important in geometry because it helps us to accurately describe and visualize the position and relationship of objects in space. It also allows us to solve problems and make calculations involving angles, areas, and other geometric properties.

Similar threads

  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
13
Views
1K
  • Programming and Computer Science
Replies
3
Views
827
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
4
Views
891
  • Programming and Computer Science
Replies
13
Views
2K
Back
Top