#include <iostream>
usingnamespace 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.
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.
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;
}
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.
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...
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.