1 Replies - 1718 Views - Last Post: 11 November 2010 - 08:02 AM Rate Topic: -----

#1 hectc7   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 22-December 09

nested for loops

Posted 11 November 2010 - 07:44 AM

sooo.... we have gotten to the point in my c++ class where they want us to begin displaying 2 dimensional characters in a console application.
******
******
******

We started by doing them with nested for loops and now with nested while loops....
The code for the latter 2 dimensional shape is
#include <iostream>
using namespace std;
int main ()
[code]
for (int row= ;row < 3 ;  row ++)
{
   for( int column = 1; column < 6; column ++)
  {
	cout<< "*" ;

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

 }



However when something like this is needed to be displayed:
*
**
***
****
***** 
#include <iostream>
using namespace std;
int main ()

for (int row= ;row < 6 ;  row ++)
{
   for( int column = 1; column < row; column ++)
  {
	cout<< " * " ;

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

 }



I get the whole loop inside of the loop is responsible for producing the * a certain amount of times. AND the outside loop is responsible for how many lines of that * the inner loop will produce.... HOWEVER ...why is it that column<row is needed ? whats the logic? I know that in a for loop the 3 steps are 1. INITIALIZE THE VARIABLE 2. SET CONDITION 3. COUNTER CONTROL

but why is the condition column < row ????

If I didn't lose anyone with my explanation and someone can answer ....THANKS!!!!!

Is This A Good Question/Topic? 0
  • +

Replies To: nested for loops

#2 Kilorn   User is offline

  • XNArchitect
  • member icon



Reputation: 1361
  • View blog
  • Posts: 3,534
  • Joined: 03-May 10

Re: nested for loops

Posted 11 November 2010 - 08:02 AM

Well, in that specific example, you want the output to be as many *'s as the current row.
* - row 1
** - row 2
*** - row 3
**** - row 4
etc.

This is the reasoning behind that particular condition.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1