Nested For Loops

Apr 8, 2009 at 11:46pm
Hi i have the following question to answer:

Write a C++ program with a nested loop structure that displays the following pyramid in "*".

here 's' is just one space. (spaces before *'s wont show with this formatting).

sssssss******
ssssss********
sssss**********
ssss************
sss**************
ss****************
s******************
********************

How can i change my code to suit the question? any ideas appreciated.

So far i have written the following code:


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
#include <iostream>
using namespace std;

int main ()

{
     int space;
     int rows;
     int star;


    for (rows = 0; rows <9; rows ++)
        {
        
        for (space = 0; space < 8; space ++)
        {
         cout << "*";

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


Basically for the first row, there are 7 spaces before the 6 *'s and 7 spaces after.
second row there are 6 spaces before 8 *'s and 6 spaces after.
this needs to continue until we hit 20 *'s and ideas.

im quite lost :(

Thanks in advance.

EDIT: Added details
Last edited on Apr 8, 2009 at 11:59pm
Apr 9, 2009 at 12:05am
Break it into parts

1) Seperate each row by drawing each row in the "outer" loop

2) The drawing consists of three seperate parts (drawing X spaces, drawing Y *'s, then drawing X spaces again). Each of these three parts can be done in a loop inside the "outer" loop for the row.

3) Once you complete the drawing for the row, adjust some variables so that the next row is drawn differently.

Here's some pseudocode:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int spaces = 7;  // top row has 7 spaces
int stars = 6;   // top row has 6 stars
int row;         // row loop counter
int i;           // inner loop counter

for( ... each row... )
{

  for( ... number of spaces... )
     draw_a_space

  for( ... number of stars ... )
     draw_a_star

  for( ... number of spaces... )  // but is this really necessary?
     draw_a_space

  // prepare counters for next row:
  spaces -= ???
  stars += ???
  cout << endl;
}
Apr 9, 2009 at 12:11am
Ok, ill give this format a go. ill add in the algorithm for this solution see how it works.

I'm having a problem with grabbing the logic of the program really.
Apr 9, 2009 at 2:54am
Ok, so how can we use 'i' to count the amount of rows that have occured.


for eg.

can we use:

i = 1 //---> 1 for the first row?
Last edited on Apr 9, 2009 at 2:55am
Apr 9, 2009 at 3:06am
In my example... 'i' was to be the loop counter for the inner loops, whereas 'row' was to be the loop counter for the outer loop (ie: 'row' counts the rows -- 'i' counts the individial characters within the row).

ie: to loop 5 times:

1
2
for(i = 0; i < 5; ++i)
  do_something;


That code executes 'do_something' 5 times. Basic loop stuff.

The thing with nested loops is you can't use the same loop counter for both loops, because the inner loops will "overwrite" the counter for the outer loop. Therefore when nesting loops you generally use seperate loop counters for each layer of loop.
Last edited on Apr 9, 2009 at 3:10am
Apr 11, 2009 at 1:08am
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
#include <iostream>
using namespace std;
int main()
{
 int z=0;
 int i, j;
 int spaces;
 int g=10;
 
 for (i=1; i<12; i++)
 {
     for (spaces=g; spaces>0; spaces--)
     {
         cout << " ";
     }
       for (j=1; j<z; j++)
       {
           cout << "*";
       }
 cout << endl;
  z+=2;
  g--;
 }
 system("pause");
}

That should work perfectly!
Apr 11, 2009 at 1:17am
Yup, did work! , Like you said Perfectly :)
Are u sure you did start Learning C++ yesterday?
Last edited on Apr 11, 2009 at 1:18am
Apr 11, 2009 at 5:42am
Lol, yes, I'm sure :) It's really pretty easy for me, I'm enjoying it alot.
Apr 11, 2009 at 7:47am
Well I'm glad you are :) Perhaps you could help me if you want, to gain experience, with my Practice, like student partners or something... Well If you want... So we can study together...
Pm me then...
Last edited on Apr 11, 2009 at 7:48am
Apr 21, 2009 at 3:08pm
hey guys i rly need some help doing this loop:


2 4 6 8
3 6 9 12
4 8 12 16
5 10 15 20


can anybody help me??
Apr 21, 2009 at 3:13pm
1 2 4 8
16 32 64 128
Apr 22, 2009 at 12:25am
You looking for something like this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;
int main()
{
 
int i, j = 1;
for (i=1; i<9; i++)
{
 cout << j;
 j = j*2;
}   
    
 system("pause");   
 return 0;
}

or this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;
int main()
{
 
int i, j = 1, y;
for (i=1; i<3; i++)
{
    for (y=1; y<5; y++)
    {
     cout << j;
     j = j*2;
    }
    cout << endl;
}   
    
 system("pause");   
 return 0;
}

Hope this helps :D
~Mashed Pwntato
Apr 22, 2009 at 6:10pm
Thanks Man Ill try it out and tell u
Apr 22, 2009 at 6:21pm
the diamond shape thing

*
***
*****
*******
*********
*******
*****
***
*
damn hard i tried everything it wouldnt get me this shape??
Apr 22, 2009 at 6:30pm







*
***
*****
*******
*********
*******
*****
***
*
Apr 23, 2009 at 4:06am
to do a diamond shape you could just do it that same way, but toss in an if or two so that once you pass that line in the middle it stops adding and starts subtracting.
Topic archived. No new replies allowed.