Saturday, May 22, 2010

C Programming Language: Printing Patterns With For Loops?

We are asked to do the following:





Write a program that prints the following patterns separately one below the other. Use for loops to generate the patterns. All asterisks ( * ) should by a single printf statement of the form printf ( “ * “ ); (this causes the asterisks to print side by side ).Hint : The last two patterns require that each line begin with an appropriate number of blanks.





A picture of the four patterns’ we are supposed to print can be found here: http://altunbay.isikun.edu.tr/cse105/fal...





I’ve used the following code – which works correctly – to print the patterns for A, B, and D, but I am having trouble with C. Here is my code, between the dotted lines:








--------------------------------------...








#include "stdio.h"


#define N 10


main()


{


int i,j;


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


{


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


printf("*");


printf("\n");





}


printf("\n");


for(i=N; i%26gt;=1; i--)


{


for(j=i; j%26gt;=1; j--)


printf("*");


printf("\n");


}





printf("\n");


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


{


for(j=i; j%26lt;N; j++)


printf(" ");


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


printf("*");


printf("\n");


}





}





--------------------------------------...





To anyone who answers, please show which for loop conditions can be used to print pattern C.

C Programming Language: Printing Patterns With For Loops?
Since this is a homework problem, I'm not going to give you the answer. I will try to guide you to the answer without giving it away. After all, I won't be helping you on your test and this is an important concept with nested loops.





The interesting part of this is you solved D. This means you also know how to generate C. Because, C is just a variation of D.





What did you have to do different with A and B? Wouldn't you have to do something similar to get C?





It seems you print a lot of asterisks first in C. you'd probably want to do something the opposite of D?





The answer is right in front of you and you know how to do it. You also have plenty of time to solve it before it is due.





Shadow Wolf

flowering plum

No comments:

Post a Comment