449,323 Members | 1,291 Online
Need help? Post your question and get tips & solutions from a community of 449,323 IT Pros & Developers. It's quick & easy.

# How to change/print pattern.

11 Replies

 100+ P: 1,806 I didn't really look at the code but basically the logic would be: (in non-specific pseudocode Expand|Select|Wrap|Line Numbers loop (i from 0 to  size)   loop (j from 0 to size)     if( i = j)       print size;     else       print symbol     endif   endloop   print newline; endloop   I think!!! Feb 22 '07 #2

 P: 64 I didn't really look at the code but basically the logic would be: (in non-specific pseudocode Expand|Select|Wrap|Line Numbers loop (i from 0 to  size)   loop (j from 0 to size)     if( i = j)       print size;     else       print symbol     endif   endloop   print newline; endloop   I think!!! Hmm, how about if you wanted to use for Feb 22 '07 #3

 100+ P: 1,806 my code is non language specific... so the loop statement could be replaced with something like Expand|Select|Wrap|Line Numbers for(int i = 0; i< size; i++) { doThings() }   same goes for the rest....... Feb 22 '07 #4

 P: 64 my code is non language specific... so the loop statement could be replaced with something like Expand|Select|Wrap|Line Numbers for(int i = 0; i< size; i++) { doThings() }   same goes for the rest....... How would I control it so if I only wanted to hardcore that there only can print four \$ only and no more no less. Feb 22 '07 #5

 100+ P: 1,806 I assunme you mean Expand|Select|Wrap|Line Numbers 5 \$ \$ \$ \$ \$ 5 \$ \$ \$ \$ \$ 5 \$ \$ \$ \$ \$ 5 \$ \$ \$ \$ \$ 5   set size to 5 and inlcude the nested for loops. If you want 4 \$ signs without the 5 down the centre, try two nested loops (at size 4) without the if statement (ie remove "if i=j") if you want just 4 in a row you can either print them literally: printf(" \$ \$ \$ \$"); or for a single loop from 1 to 4...... Feb 22 '07 #6

 P: 64 Expand|Select|Wrap|Line Numbers /*    ==================== printOne ====================     This function receives the size of pattern one and      prints it. For instance, if size is 5, output would      be:                 5 \$ \$ \$ \$                 \$ 5 \$ \$ \$                 \$ \$ 5 \$ \$                  \$ \$ \$ 5 \$                 \$ \$ \$ \$ 5            Pre   size        Post  pattern one printed    */ void  printOne  ( int size ) { //    Local Definitions      int i; //    Statements      printf("\n\n\t\t");     printf("%d",size);     for(int i = 1; i< size; i++)     {         printf(" \$");       }// first line         printf("\n\t\t\$ ");     printf("%d",size);     for(int i = 2; i< size; i++)     {         printf(" \$");       }// second line       printf("\n\t\t\$ \$ ");     printf("%d",size);     for(int i = 3; i< size; i++)     {         printf(" \$");       }// third line       printf("\n\t\t\$ \$ \$ ");     printf("%d",size);     for(int i = 4; i< size; i++)     {         printf(" \$");       }// fourth line       printf("\n\t\t\$ \$ \$ \$ ");     printf("%d",size);     for(int i = 5; i< size; i++)     {         printf(" \$");       }// fifth line       printf("\n\n\n");       return ; }// printOne     The result when I enter pattern 5, I get Expand|Select|Wrap|Line Numbers Please type the size of the pattern [2 - 9]: 5                     5 \$ \$ \$ \$                 \$ 5 \$ \$ \$                 \$ \$ 5 \$ \$                 \$ \$ \$ 5 \$                 \$ \$ \$ \$ 5       However, if I push pattern 3 or 4 I get a messed up pattern. Expand|Select|Wrap|Line Numbers                     3 \$ \$                 \$ 3 \$                 \$ \$ 3                 \$ \$ \$ 3                 \$ \$ \$ \$ 3                   4 \$ \$ \$                 \$ 4 \$ \$                 \$ \$ 4 \$                 \$ \$ \$ 4                 \$ \$ \$ \$ 4     I know its the way I coded it, but how would you do it? I'm thinking I would have to do something like this Expand|Select|Wrap|Line Numbers void  printOne  ( int size ) { //    Local Definitions      int i; //    Statements        printf("%d",size);     for(int i = 1; i< size; i++)     {         printf(" \$");         printf("\n");       }// first line   Which would print. 5 \$ \$ \$ \$ Is the way I'm thinking correct? Thanks in advance. Feb 22 '07 #7

 100+ P: 1,806 pyut two gfor loops inside each other: Expand|Select|Wrap|Line Numbers for(int i=0; i< size i++) {   for(int k=0; < size; k++)   {     //do your printing here, print size if i=k and \$ otherwise   } //print newline here }   Feb 22 '07 #8

 P: 64 [quote]=DeMan]pyut two gfor loops inside each other: Expand|Select|Wrap|Line Numbers for(int i=0; i< size i++) {   for(int k=0; < size; k++)   {     //do your printing here, print size if i=k and \$ otherwise   } //print newline here }   [/QUOTE Ahh, I see. Another question how would you put a number in there? 5 \$ \$ \$ \$ \$ 5 \$ \$ \$ \$ \$ 5 \$ \$ \$ \$ \$ 5 \$ \$ \$ \$ \$ 5 This is what I'm getting Expand|Select|Wrap|Line Numbers   5 \$5 \$5 \$5 \$ 5 \$5 \$5 \$5 \$ 5 \$5 \$5 \$5 \$ 5 \$5 \$5 \$5 \$   void  printOne  ( int size ) { //    Local Definitions      int i;     int k; //    Statements          for(int i=1; i<=4; i++)     {        for(int k=1; k< size; k++)        {                printf("%d",size);                printf(" \$");        }     printf("\n");     }     return ;   Feb 22 '07 #9

 100+ P: 1,806 As per the pseudocode originally posted, you need some sort of test.... We have different things tio print on different conditions... if i=j we want to print the number, otherwise we can print the \$ so Expand|Select|Wrap|Line Numbers if( i==k) //need to equals signs if we're testing {   printf("%d", Size); } else {   printf("\$"); }   Feb 22 '07 #10

 P: 64 How about if I wanted 5 \$ \$ \$ \$ \$ 5 \$ \$ \$ \$ \$ 5 \$ \$ \$ \$ \$ 5 \$ \$ \$ \$ \$ 5 I figured I would change the ++ to -- and make the >=, but that doesn't seem to work for me. Feb 22 '07 #11

 100+ P: 1,806 I don't really understand how that is different to what we are doing (although I notice in your code you have a hard coded 4 where you may have meant Size -> 1st for loop) Do you mean how to get the numbers to follow throyugh the other way? Feb 23 '07 #12