Thursday, July 30, 2009

How to make a '*' triangle using C...?

Hi Guys I need to make a '*' triangle in C that should look like:





*


***


*****


and so on until the last row is 15....I have to use a nested for loop...But I just cant think straight on how to make this work....

How to make a '*' triangle using C...?
You've put in enough work to be told how to do this.





Your inner for loop needs to use the value of the outer for loop to make proper-sized rows.





int totalstars = 15;


int i;


int j;





for(i = 1; i %26lt;= totalstars; i++) {


for(j = 1; j %26lt;= i; j++) {


printf("*");


}


printf("\n");


}
Reply:Homework question? Here is a general guideline:





Outer loop will be used to keep track of the line, say i


Inner loop will be used to print each * in each line, and each line has the same number of * as the line number, so loop from 0 from i





Each time you've finished printing out a line of *****'s you need a newline, so you would do printf("\n");











*applause* This kid is good :)
Reply:replace row in j loop by i
Reply:try this


change the value of row to set the height of the tree





for(i = 1; i %26lt;= row; i++)


{





for (j = 1; j %26lt;=(1 +(( i-1)*2)) ; j++)


{ printf("*");}


printf("\n");


}
Reply:HI,





Don't get too flustered...its actually quite simple when you visualise what's going on....John, the previous replier above just answered it for you....I can see the entire code in my head and it's 4 lines of codes....





Just visualize it and you will see it....its defintiely easier than you think
Reply:Have patience. The analysis and manipulation of code until it does what you want it to is the essence of programming, much like hours of practice with a violin is the essence of learning to play it. Keep at it and you'll get it eventually.


No comments:

Post a Comment

Post a Comment