4 Replies - 26630 Views - Last Post: 21 February 2008 - 07:07 PM Rate Topic: -----

#1 nancys  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 20-February 08

nested for loop to print triangle to console posted code problem

Post icon  Posted 20 February 2008 - 07:07 PM

Hi everybody, my problem is trying to nest one for loop within another one.
I am trying to print a traditional three sided triangle, and I have the
left side of the triangle done with decreasing spaces and asterisks.
now i need to embed the secod loop within the first to be able to
print the empty spaces within and the second set of asterisks comprising the right side of the triangle. so I need to embed or nest that loop in the first loop.
the sequence of empty spaces for the middle and the second set of
asterisks is first row aempty space asterisk
second row three empty spaces asterisk
third row five empty spaces asterisk
here's my code . . .

#include <iostream>

using namespace std;

int main () {

	int x = 0, n = 0, y = 0, limit = 0, z = 0, j = 0, numberOfRows = 0, line = 9, spaces = 0;
	;

	cout << "Enter the base width of triangle to draw. \n";
	cout << "Odd integers only between 3 and 41 <other values will quit>:  \n";



	do {	
			cin >> x;
			limit = x/2;
			numberOfRows = x/2 + 1;
			

			for (y = 0; y <= limit; y++) 
				{
				cout << " ";
				}
				cout << "*\n";

				for (n = 0; n < numberOfRows - 2; n++)
				{	
					
					limit = limit - 1;
					for (z = limit; 0 < z; z--)	
					{	
						cout << " "; 
						}								
							cout << "*"; 
									
						spaces = line + 2;
							for (j = 1; j < spaces; j++)
								{
								cout << " ";
								}
							cout << "*" << endl; 
					}
				
					
					
				

		 } while (x % 2 == 0 || x < 1 || x > 41);

   cout << "You entered an invalid number." << endl;





 return (0);
}
Mod Edit: added code tags: :code:

Is This A Good Question/Topic? 0
  • +

Replies To: nested for loop to print triangle to console posted code problem

#2 VernonDozier  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 06-January 08

Re: nested for loop to print triangle to console posted code problem

Posted 21 February 2008 - 02:48 AM

Your printout is along the lines of this:
		   *			*
		 *			*
	   *			*
	 *			*
   *			*
*			 *


In each line the number of spaces between asterisks is the same. You want it to vary. You want the number of spaces between asterisks to to increase by two for every line as you progress from top to bottom. I'm guessing that is what you are trying to do here before the second inner for loop inside the outer for loop:
spaces = line + 2;
 for (j = 1; j < spaces; j++)
{
   cout << " ";
}
cout << "*" << endl;


However, line is defined at the top of your program as 9. It never changes. Thus spaces = 9 + 2 = 11, and stays that way. So spaces is effectively a constant. Since spaces is your loop control variable, you are always outputting the same number displaying the same number of spaces between asterisks, so you get parallel sides, which you don't want for a triangle. You need to vary spaces. You could change this line:
spaces = line + 2;


to
spaces = spaces + 2;


That will cause it to vary as you desire. You would have to initialize spaces previously, of course.
Was This Post Helpful? 0
  • +
  • -

#3 vinodj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 12-February 08

Re: nested for loop to print triangle to console posted code problem

Posted 21 February 2008 - 04:53 AM

I didn't understand ur code. But I am including my code. If u want u can.


#include <iostream>

using namespace std;

int main () {

	int x ,i, n , j;
	

	cout << "Enter the base width of triangle to draw. \n";
	cin>>x;
	for(j=1;j<=x;j++)
	{
		if(j==x)
		{
			for(i=1;i<=2*x-1;i++)
				if(i%2!=0)
			  printf("*");
				else 
					printf(" ");
				printf("\n");
			break;
		}
		for(n=1;n<=x-j;n++)
			printf(" ");
		for(i=1;i<=2*j-1;i++)
		if(i==1 || i==2*j-1)
			printf("*");
		else
			printf(" ");
		printf("\n");
	}
  
return (0);
}

Was This Post Helpful? 0
  • +
  • -

#4 nsivrts  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 16-October 07

Re: nested for loop to print triangle to console posted code problem

Posted 21 February 2008 - 06:58 PM

View PostVernonDozier, on 21 Feb, 2008 - 02:48 AM, said:

Your printout is along the lines of this:
		   *			*
		 *			*
	   *			*
	 *			*
   *			*
*			 *


In each line the number of spaces between asterisks is the same. You want it to vary. You want the number of spaces between asterisks to to increase by two for every line as you progress from top to bottom. I'm guessing that is what you are trying to do here before the second inner for loop inside the outer for loop:
spaces = line + 2;
 for (j = 1; j < spaces; j++)
{
   cout << " ";
}
cout << "*" << endl;


However, line is defined at the top of your program as 9. It never changes. Thus spaces = 9 + 2 = 11, and stays that way. So spaces is effectively a constant. Since spaces is your loop control variable, you are always outputting the same number displaying the same number of spaces between asterisks, so you get parallel sides, which you don't want for a triangle. You need to vary spaces. You could change this line:
spaces = line + 2;


to
spaces = spaces + 2;


That will cause it to vary as you desire. You would have to initialize spaces previously, of course.


Thank you very much for your helpful insights. That's what I get for working on this late at night on no sleep. I meant to declare lines as 0 and OR spaces, don't want to use both. Too many variables. but that's why I am so glad to have another pair of eyes . . . .

nancy s.
Was This Post Helpful? 0
  • +
  • -

#5 nsivrts  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 16-October 07

Re: nested for loop to print triangle to console posted code problem

Posted 21 February 2008 - 07:07 PM

View Postvinodj, on 21 Feb, 2008 - 04:53 AM, said:

I didn't understand ur code. But I am including my code. If u want u can.


#include <iostream>

using namespace std;

int main () {

int x ,i, n , j ;


cout << "Enter the base width of triangle to draw. \n";
cin>>x;
for(j=1;j<=x;j++)
{
if(j==x)
{
for(i=1;i<=2*x-1;i++)
if(i%2!=0)
printf("*");
else
printf(" ");
printf("\n");
break;
}
for(n=1;n<=x-j;n++)
printf(" ");
for(i=1;i<=2*j-1;i++)
if(i==1 || i==2*j-1)
printf("*");
else
printf(" ");
printf("\n");
}

return (0);
}

Thank you for including your code example. I really think it's helpful to see other people's successful code examples. I don't want to cut it and paste it or anything, I want to write my own, but I can walk through yours, which works, with values and figure
out what's going in a successful program. I think I am making this harder than it's supposed to be.

I just need to walk through the code with a couple of values and figure out how to successfully nest for loops.
Instructor said we can't use if - else statements, just loops.
Can I just put in a for loop for the initial empty spaces, then drop out of the loop, print asterisk, another loop for more empty spaces, then the last asterisk?
thanks for all the help

nancy s.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1