Nested Loop Problem. Help me to figure out how to do it

Modify the program in step 2 so that if the
x-coordinate (the number of *'s you print on a line)
for any line falls within the range 20 ≤ x ≤ 30 then
only print a line with exactly 20 *’s rather than the
calculated value of x. This will make a sharp vertical
cliff in the triangle for some number of lines (maybe
none if theta is small) and then resume along the
original diagonal line. Compile and test your
program.

Can anyone help me to figure out how to do this part.
I am supposed to get this:

*
***
*****
******
********
**********
************
*************
***************
*****************
*******************
********************
********************
********************
********************
********************
********************
*******************************
********************************
**********************************
************************************
**************************************
***************************************
*****************************************
*******************************************
*********************************************
**********************************************
************************************************
**************************************************
but insted I am getting this:
*
***
*****
******
********
**********
************
*************
***************
*****************
*******************
********************
********************
********************
********************
********************
********************
**********************
***********************
*************************
***************************
*****************************
******************************
********************************
**********************************
************************************
*************************************
***************************************
*****************************************
******************************************


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
  #include <iostream>
#include <string>
#include <cmath>

#define PI 3.14159265

using namespace std;

int main()
{
   char star = '*';
   int rows = 31;
   int angle_theta = 0;
   int length = 1;
   
   cout <<"Enter angle between 15 - 75: ";
   cin >> angle_theta;
   cout << "Entered theta " << angle_theta << " degree" << endl;

  for(int i = 1; i < rows; i++)
   {
       //cout << "number of i " << i << endl;
       //length = i * floor(tan(angle_theta*PI/180.0));
       length = floor(i * tan(angle_theta*PI/180.0));
       //cout << "length " << length;
       for(int j = 0; j < length; j++)
       {
           if( j >= 0 && j <= 19)
           cout << star;
           else if( j >= 29 )
           cout << star;
           else if ( j >= 19 && j <= 29)
           cout << "";
           
       }
       cout << endl;
       //length++;
   }
   return 0;
}
closed account (2UD8vCM9)
I'm not sure if this is what you want, but try this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <string>
#include <cmath>

#define PI 3.14159265

using namespace std;

int main()
{
	char star = '*';
	int rows = 31;
	int angle_theta = 0;
	int length = 1;

	cout << "Enter angle between 15 - 75: ";
	cin >> angle_theta;
	cout << "Entered theta " << angle_theta << " degree" << endl;

	for (int i = 1; i < rows; i++)
	{
		//cout << "number of i " << i << endl;
		//length = i * floor(tan(angle_theta*PI/180.0));
		length = floor(i * tan(angle_theta*PI / 180.0));
		//cout << "length " << length;
		for (int j = 0; j < length; j++)
		{
			if (length >= 0 && length <= 19)
				cout << star;
			else if (length > 29)
				cout << star;
			else if (length >= 19 && length <= 29)
				if (j<20)
					cout << star;

		}
		cout << endl;
		//length++;
	}
	system("pause");
	return 0;
}


I believe the issue was you should've been checking what your length was, not your J, and if your length was between 20-30, then only print X's for when your counting up to 20 and not after that.
Can you explain me from line 32 to line 34??
closed account (2UD8vCM9)
Line 32 - 34 is saying the following

If the # of stars to be printed is between 20 and 30 and
If we have printed less than 20 stars,
print a star

In a nutshell, that's the logic since J is the counter for how many stars have been printed, and length is the counter for the total # of stars to be drawn on the line.

Does that clear it up?
Topic archived. No new replies allowed.