1. ## Nested loop frustration

Hi guys, I'm having trouble with the following code: first problem is, i'm really not very good at mathematics and the equation to represent the spaces eludes me.
What am i doing wrong please. I've visited this link where salem was helping another individual and it clarified a few things in terms of the structure but i'm still a little mystified at the output i get. Compile it and you see the weird structure.
thank you.

Code:
```/* pyramid.c output:  *
***
*****
*******
*********

*/

#include <stdio.h>
int main(void){

int r,c,s, ht=5;

for (r = 1; r <=ht; r++){ //number of rows
for (s = 1; s <=(ht-(r/2)); s++) // determine spaces
printf(" ");

for (c = 1; c <=r; c++)//determine num of charaters to place
printf("%c", '*');

printf("\n");
}
getchar();
return 0;
}```

2. Do you notice a pattern in the output? Try looking for places where a difference in even/odd would result in an off-by-one error in printing.

3. Originally posted by Prelude
Do you notice a pattern in the output? Try looking for places where a difference in even/odd would result in an off-by-one error in printing.
Thanks for your reply prelude. the pattern in the output seems to be that the clms is shifted to the right by 1 space. however i was doing some more search on the board here and there was a quote
Using an ASCII display you can't get an even numbered pyramid to center properly. In this case I would make sure that the number is always odd by either requiring an odd number to be input, or making it odd by adding or subtracting one if it's even.
So is that true? if it is it would make sense why salem incremented his loop b y 2. so i attempted to do that.
Code:
`for (c = 0; c<=r; c+=2)`
however it did not have the desired output. I'm gonna search some more on the board too and see if maybe i can get it explained in a different light, maybe my brainfart will clear up. Unless you have another explanation for me.

4. Number of leading spaces - 5,4,3,2,1
Number of stars - 1,3,5,7,9

nspace = ht - r
nstars = 2*r-1

Code:
```   for (s = 1; s <=ht-r; s++) // determine spaces
printf(" ");

for (c = 1; c <=r*2-1; c++)//determine num of charaters to place
printf("%c", '*');```

5. You guys are geniuses!!! Thank you so much. on a cursory glance, i don't necessarily understand how you came to that logic salem. but I'm gonna look over this code you just posted and the one before and have it saturate in my brain. Programming is about problem solving and its not just enough to have the solution given to you. I need to understand it for myslef. Thank you guys very much. This is a prelude to another excercise my book is giving me, but perhaps i won't need to come back on the board for it. Thanks.

BTW: prelude. Like your name so much i went out and bought one. you wouldn't happen to have one would you?

Thanks again guys. Now i can go spend some time with my girl while this solution works itself out and saturate in my head.

6. Yo Dude(Caroundwh5). Check you PM.

7. > i don't necessarily understand how you came to that logic salem
Draw the pattern on graph paper and start counting.
That's how you discover the relationship between spaces, stars and the row number.

Then calculate as I did here

Or do something like
for ( space = 5, star = 1 ; space > 0 ; space -= 1, star += 2 )

8. Thanks for the advice. I really need to improve my problem solving skills. and that was helpful. Just feel so inept sometimes you know?

9. Draw the pattern on graph paper and start counting.
That's how you discover the relationship between spaces, stars and the row number.

Then calculate as I did here

Or do something like
for ( space = 5, star = 1 ; space > 0 ; space -= 1, star += 2 )
That was very helpful. The problem was I wasn't thinking of exactly how the computer displays numbers, I just saw the slanting structure and i was just like "thats hard". But your idea of a graph helps a lot in visualizing what needs to be done. I added this to better help me understand
Code:
``` /* determine spaces equation */

for (s = 1; s<=ht-r; s++)
printf("-");// shows equation for number of spaces needed```
This really helped me understand better the relationship between the spaces and stars.

As to the algorithm to determine where to put them. I noticed you had two of them and i understand you can have different equations that add up to the same thing. Thanks a lot.

One last question: What exactly are the practical applications of nested loops. Aside from prettyprinting, what can i do with them in the "real world" ?

10. >Aside from prettyprinting, what can i do with them in the "real world" ?
Many things. The first that comes to mind is processing multidimensional arrays in a natural manner.

11. Originally posted by Prelude
>Aside from prettyprinting, what can i do with them in the "real world" ?
Many things. The first that comes to mind is processing multidimensional arrays in a natural manner.
I haven't delved into multidemensional arrays yet ( they are called matrixes right? ) but i've done an introduction to arrays. Could you show me an example maybe plz?

12. >Could you show me an example maybe plz?
The only example I have handy is Dr. Conway's game of life that I wrote this morning to answer a question on another board. Unfortunately it's in C++. Sorry.
Code:
```#include <bitset>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <vector>

using namespace std;

class life {
static const int height = 24;
static const int width = 76;
public:
life()
: board ( vector< bitset<width> > ( height ) )
{
srand ( static_cast<unsigned> ( time ( 0 ) ) );
for ( int x ( 1 ); x < height - 1; x++ ) {
for ( int y ( 1 ); y < width - 1; y++ ) {
if ( (double)rand() / RAND_MAX < .5 )
board[x].set ( y );
}
}
};
void display()
{
for ( int x ( 0 ); x < height; x++ ) {
for ( int y ( 0 ); y < width; y++ )
cout<< ( board[x].test ( y ) ? '*' : ' ' );
cout<<'\n';
}
}
bool next()
{
vector< bitset<width> > scratch_board ( height );

for ( int x ( 1 ); x < height - 1; x++ ) {
for ( int y ( 1 ); y < width - 1; y++ ) {
const int top ( board[x - 1][y - 1] + board[x - 1][y] + board[x - 1][y + 1] );
const int center ( board[x][y - 1] + board[x][y + 1] );
const int bottom ( board[x + 1][y - 1] + board[x + 1][y] + board[x + 1][y + 1] );
const int neighbors ( top + center + bottom );
if ( neighbors == 3 || ( neighbors == 2 && board[x].test ( y ) ) )
scratch_board[x][y] = 1;
else
scratch_board[x][y] = 0;
}
}
// Notify if oscillating or static universe
if ( board == scratch_board )
return false;
board = scratch_board;

return true;
}
private:
vector< bitset<width> > board;
};

#include <windows.h>

void pause_program ( int n )
{
Sleep ( n );
}

void clear_screen()
{
system ( "cls" );
}

int main()
{
life game;
int gen;

cout<<"Enter the number of generations: ";
if ( cin>> gen ) {
while ( gen-- > 0 ) {
clear_screen();
game.display();
if ( !game.next() )
break;
pause_program ( 500 );
}
}
}```

13. thanks, i can follow along. if not necessarily understand some of the c++ functions you introduced. I see where the dependant nested loops are, although the code compiled with errors the first being
C:\DEV-C_~1\Include\G__~1\stl_alloc.h: In instantiation of `bitset<76,long unsigned int>':
C:\DEV-C_~1\Include\G__~1\stl_alloc.h:228: instantiated from `simple_alloc<bitset<76,long unsigned int>,__default_alloc_template<false,0> >::allocate(unsigned int)'
C:\DEV-C_~1\Include\G__~1\stl_vector.h:93: instantiated from `_Vector_alloc_base<bitset<76,long unsigned int>,allocator<bitset<76,long unsigned int> >,>::_M_allocate(unsigned int)'
C:\DEV-C_~1\Include\G__~1\stl_vector.h:110: instantiated from `_Vector_base<bitset<76,long unsigned int>,allocator<bitset<76,long unsigned int> > >::_Vector_base(unsigned int, const allocator<bitset<76,long unsigned int> > &)'
C:\DEV-C_~1\Include\G__~1\stl_vector.h:230: instantiated from `vector<bitset<76,long unsigned int>,allocator<bitset<76,long unsigned int> > >::vector(unsigned int)'
c:\docume~1\owner\desktop\untitl~1.cpp:19: instantiated from here
C:\DEV-C_~1\Include\G__~1\stl_alloc.h:228: `CHAR_BIT' was not declared in this scope
C:\DEV-C_~1\Include\G__~1\stl_alloc.h:228: `CHAR_BIT' was not declared in this scope
C:\DEV-C_~1\Include\G__~1\stl_alloc.h:228: template argument 1 is invalid
C:\DEV-C_~1\Include\G__~1\bitset:541: `CHAR_BIT' was not declared in this scope
C:\DEV-C_~1\Include\G__~1\bitset:541: `CHAR_BIT' was not declared in this scope
C:\DEV-C_~1\Include\G__~1\bitset:541: template argument 1 is invalid
C:\DEV-C_~1\Include\G__~1\bitset:542: `CHAR_BIT' was not declared in this scope
C:\DEV-C_~1\Include\G__~1\bitset:542: `CHAR_BIT' was not declared in this scope
C:\DEV-C_~1\Include\G__~1\bitset:542: template argument 1 is invalid
C:\DEV-C_~1\Include\G__~1\bitset:543: `CHAR_BIT' was not declared in this scope
C:\DEV-C_~1\Include\G__~1\bitset:543: `CHAR_BIT' was not declared in this scope
C:\DEV-C_~1\Include\G__~1\bitset:543: template argument 1 is invalid
C:\DEV-C_~1\Include\G__~1\bitset:544: `CHAR_BIT' was not declared in this scope
C:\DEV-C_~1\Include\G__~1\bitset:544: `CHAR_BIT' was not declared in this scope
C:\DEV-C_~1\Include\G__~1\bitset:544: template argument 1 is invalid
C:\DEV-C_~1\Include\G__~1\bitset:545: `CHAR_BIT' was not declared in this scope
C:\DEV-C_~1\Include\G__~1\bitset:545: `CHAR_BIT' was not declared in this scope
C:\DEV-C_~1\Include\G__~1\bitset:545: template argument 1 is invalid
C:\DEV-C_~1\Include\G__~1\bitset:546: `CHAR_BIT' was not declared in this scope
C:\DEV-C_~1\Include\G__~1\bitset:546: `CHAR_BIT' was not declared in this scope
C:\DEV-C_~1\Include\G__~1\bitset:546: template argument 1 is invalid
C:\DEV-C_~1\Include\G__~1\bitset:547: `CHAR_BIT' was not declared in this scope
C:\DEV-C_~1\Include\G__~1\bitset:547: `CHAR_BIT' was not declared in this scope
C:\DEV-C_~1\Include\G__~1\bitset:547: template argument 1 is invalid
C:\DEV-C_~1\Include\G__~1\bitset:548: `CHAR_BIT' was not declared in this scope
C:\DEV-C_~1\Include\G__~1\bitset:548: `CHAR_BIT' was not declared in this scope
C:\DEV-C_~1\Include\G__~1\bitset:548: template argument 1 is invalid
C:\DEV-C_~1\Include\G__~1\bitset:549: `CHAR_BIT' was not declared in this scope
C:\DEV-C_~1\Include\G__~1\bitset:549: `CHAR_BIT' was not declared in this scope
C:\DEV-C_~1\Include\G__~1\bitset:549: template argument 1 is invalid
C:\DEV-C_~1\Include\G__~1\bitset:550: `CHAR_BIT' was not declared in this scope
C:\DEV-C_~1\Include\G__~1\bitset:550: `CHAR_BIT' was not declared in this scope
C:\DEV-C_~1\Include\G__~1\bitset:550: template argument 1 is invalid
C:\DEV-C_~1\Include\G__~1\bitset:551: `CHAR_BIT' was not declared in this scope
C:\DEV-C_~1\Include\G__~1\bitset:551: `CHAR_BIT' was not declared in this scope
C:\DEV-C_~1\Include\G__~1\bitset:551: template argument 1 is invalid
C:\DEV-C_~1\Include\G__~1\bitset:552: `CHAR_BIT' was not declared in this scope
C:\DEV-C_~1\Include\G__~1\bitset:552: `CHAR_BIT' was not declared in this scope
C:\DEV-C_~1\Include\G__~1\bitset:552: template argument 1 is invalid
C:\DEV-C_~1\Include\G__~1\bitset:553: `CHAR_BIT' was not declared in this scope
C:\DEV-C_~1\Include\G__~1\bitset:553: `CHAR_BIT' was not declared in this scope
C:\DEV-C_~1\Include\G__~1\bitset:553: template argument 1 is invalid
C:\DEV-C_~1\Include\G__~1\bitset:554: `CHAR_BIT' was not declared in this scope
C:\DEV-C_~1\Include\G__~1\bitset:554: `CHAR_BIT' was not declared in this scope
C:\DEV-C_~1\Include\G__~1\bitset:554: template argument 1 is invalid
C:\DEV-C_~1\Include\G__~1\bitset:555: `CHAR_BIT' was not declared in this scope
C:\DEV-C_~1\Include\G__~1\bitset:555: `CHAR_BIT' was not declared in this scope
C:\DEV-C_~1\Include\G__~1\bitset:555: template argument 1 is invalid
C:\DEV-C_~1\Include\G__~1\bitset:556: `CHAR_BIT' was not declared in this scope
C:\DEV-C_~1\Include\G__~1\bitset:556: `CHAR_BIT' was not declared in this scope
C:\DEV-C_~1\Include\G__~1\bitset:556: template argument 1 is invalid
C:\DEV-C_~1\Include\G__~1\bitset:557: `CHAR_BIT' was not declared in this scope
C:\DEV-C_~1\Include\G__~1\bitset:557: `CHAR_BIT' was not declared in this scope
C:\DEV-C_~1\Include\G__~1\bitset:557: template argument 1 is invalid
C:\DEV-C_~1\Include\G__~1\bitset:558: `CHAR_BIT' was not declared in this scope
C:\DEV-C_~1\Include\G__~1\bitset:558: `CHAR_BIT' was not declared in this scope
C:\DEV-C_~1\Include\G__~1\bitset:558: template argument 1 is invalid
C:\DEV-C_~1\Include\G__~1\bitset:559: `CHAR_BIT' was not declared in this scope
C:\DEV-C_~1\Include\G__~1\bitset:559: `CHAR_BIT' was not declared in this scope
C:\DEV-C_~1\Include\G__~1\bitset:559: template argument 1 is invalid
C:\DEV-C_~1\Include\G__~1\bitset:560: `CHAR_BIT' was not declared in this scope
C:\DEV-C_~1\Include\G__~1\bitset:560: `CHAR_BIT' was not declared in this scope
C:\DEV-C_~1\Include\G__~1\bitset:560: template argument 1 is invalid
C:\DEV-C_~1\Include\G__~1\bitset:537: `CHAR_BIT' was not declared in this scope
C:\DEV-C_~1\Include\G__~1\bitset:537: `CHAR_BIT' was not declared in this scope
C:\DEV-C_~1\Include\G__~1\bitset:537: template argument 1 is invalid
C:\DEV-C_~1\Include\G__~1\bitset:815: confused by earlier errors, bailing out

14. Upgrade. It compiles cleanly on my version of Dev-C++ (4.9.8.0).

15. Originally posted by Prelude
Upgrade. It compiles cleanly on my version of Dev-C++ (4.9.8.0).
Oh thanks. I've been having problems with 4.9 and up. But thanks for all your help on this topic. both you and Salem.