Click here to Skip to main content
15,441,629 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The below code print a pattern...and i am not understand the comment line..Can u please tell me

C++
#include <stdio.h>
 
int main()
{
   int row, c, n, temp;
 
   printf("Enter the number of rows in pyramid of stars you wish to see ");
   scanf("%d",&n);
 
   temp = n;
 
   for ( row = 1 ; row <= n ; row++ )
   {
      for ( c = 1 ; c < temp ; c++ )
         printf(" ");
 
      temp--;
 
      for ( c = 1 ; c <= 2*row - 1 ; c++ )// what is mean by this line?? please explain......
         printf("*");
 
      printf("\n");
   }
 
   return 0;
}
Posted
Updated 28-Dec-13 20:40pm
v4
Comments
[no name] 29-Dec-13 2:44am    
What do you want explained? Do you understand what the other for loops are doing?

1 solution

It's a loop: it has three parts:
C++
for ( A ; B ; C)
Where:
A is the initial set up. In your case is sets "c" to 1 before it enters the loop.
B is the test that is performed each time the code is about to go round the loop. If it is true, then the code is executed. If not, the loop ends. In your case it checks that "c" is is still less than or equal to twice the rows minus one.
C is the step change - it defines what happens at the end of the loop, before the test is repeated to ensure that the loop ends when it is supposed to. In your case, it just adds one to "c"

So if rows was 2, the code in the loop would be executed 3 times. (2 * 2 - 1 == 4 - 1 == 3, so when c exceeds 3 the loop will stop.)
 
Share this answer
 
Comments
Richard MacCutchan 29-Dec-13 5:37am    
Must be the start of a new school year. :)
OriginalGriff 29-Dec-13 6:00am    
Or he's very late with his homework! :laugh:
H.Brydon 29-Dec-13 11:18am    
...or the place I used to work hired some new programmers. :-)
OriginalGriff 29-Dec-13 11:25am    
I'm glad it was "used to work" :laugh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900