By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,678 Members | 1,920 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,678 IT Pros & Developers. It's quick & easy.

How to change/print pattern.

P: 64
Hello, I just have a few questions. The first one be how would you print a pattern. I could use the if else, but I remember my teacher talking about something like

Expand|Select|Wrap|Line Numbers
  1. for(i=1;i<=size;i)
  2.    printf($);
  3.  


Code below will take the size of what I input and change the size of the pattern by adding more or less $

So if the user type number 6 it would print

Expand|Select|Wrap|Line Numbers
  1.                 6 $ $ $ $ $
  2.                 $ 6 $ $ $ $
  3.                 $ $ 6 $ $ $
  4.                 $ $ $ 6 $ $
  5.                 $ $ $ $ 6 $    
  6.  

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. #define QUIT 5
  4.  
  5. //    Prototype Definitions 
  6. void  printMenu (void);
  7. int   getOption (void);
  8. int   getSize   (void);
  9. void  printPattern( int size, int option );
  10. void  printOne  ( int size );
  11. void  printTwo  ( int size );
  12. void  printThree( int size );
  13. void  printFour ( int size );
  14.  
  15. int main (void)
  16. {
  17. //    Local Definitions 
  18.     int option;
  19.     int size;
  20.  
  21. //    Statements 
  22.     printf("\nBeginning of Program.\n");
  23.  
  24.     option = getOption();
  25.     while (option != QUIT)
  26.     {
  27.         size = getSize();
  28.         printPattern( size, option );
  29.         option = getOption();
  30.     } 
  31.  
  32.     printf("\nEnd of Program.\n");
  33.  
  34.     return 0;
  35. }// main 
  36.  
  37. /*    ==================== printMenu ====================
  38.     This function prints a menu 
  39.        Pre   nothing 
  40.        Post  nothing 
  41. */
  42. void printMenu (void)
  43.  
  44.     printf("\n*********************");
  45.     printf("\n*     M E N U       *");
  46.     printf("\n*                   *");
  47.     printf("\n*  1. Pattern One   *");
  48.     printf("\n*  2. Pattern Two   *");
  49.     printf("\n*  3. Pattern Three *");
  50.     printf("\n*  4. Pattern Four  *");
  51.     printf("\n*  5. Quit          *");
  52.     printf("\n*                   *");
  53.     printf("\n*********************");
  54.  
  55.     return;
  56. }
  57. /*    ==================== getOption ====================
  58.     This function shows a menu and reads the user option.
  59.        Pre   nothing 
  60.        Post  returns a valid option 
  61. */
  62. int getOption (void)
  63. //    Local Definitions 
  64.     int option;
  65.  
  66. //    Statements 
  67.     printMenu();
  68.     printf("\n\n\nPlease type your choice ");
  69.     printf("and press the return key : ");
  70.     scanf("%d", &option);
  71.     while (option < 1 || option > 5)
  72.     {
  73.         printf("\nInvalid option. Please re-enter.\n\n");
  74.         printMenu();
  75.          printf("\n\n\nPlease type your choice ");
  76.         printf("and press the return key : ");
  77.         scanf("%d", &option);
  78.     }
  79.  
  80.     return option;
  81. }// getOption 
  82.  
  83.  
  84. /*    ==================== getSize ====================
  85.     This function gets the size of the pattern 
  86.        Pre   nothing 
  87.        Post  returns a valid size
  88. */
  89. int getSize (void)
  90. //    Local Definitions 
  91.     int size;
  92.  
  93. //    Statements 
  94.     printf("\n\n\nPlease type the size of the pattern [2 - 9]: ");
  95.     scanf("%d", &size);
  96.     while (size < 2 || size > 9)
  97.     {
  98.           printf("\nInvalid size. Please re-enter.\n");
  99.           scanf("%d", &size);
  100.     }
  101.  
  102.     return size;
  103. }// getSize 
  104.  
  105. /*    ==================== printPattern ====================
  106.     This function receives a pattern's size and the 
  107.     user's option and prints the corresponding pattern.
  108.        Pre   size
  109.              option
  110.        Post  pattern printed 
  111. */
  112. void printPattern( int size, int option )
  113. {
  114. //    Local Definitions 
  115.  
  116. //    Statements 
  117.     switch (option)
  118.     {
  119.         case 1: printOne(size);
  120.                 break;
  121.     case 2: printTwo(size);
  122.                 break;
  123.     case 3: printThree(size);
  124.                 break;
  125.     case 4: printFour(size);
  126.                    break;
  127.     }
  128.  
  129.     return;
  130. }// printPattern
  131.  
  132. /*    ==================== printOne ====================
  133.     This function receives the size of pattern one and 
  134.     prints it. For instance, if size is 5, output would 
  135.     be:
  136.                 5 $ $ $ $
  137.                 $ 5 $ $ $
  138.                 $ $ 5 $ $ 
  139.                 $ $ $ 5 $
  140.                 $ $ $ $ 5    
  141.        Pre   size
  142.        Post  pattern one printed 
  143.  
  144. */
  145. void  printOne  ( int size )
  146. {
  147. //    Local Definitions 
  148.  
  149. //    Statements 
  150.     for(i=!;i<=n;i
  151.     printf("%d",size);
  152.  
  153.     return ;
  154. }// printOne
  155.  
  156. /*    ==================== printTwo ====================
  157.     This function receives the size of pattern two and 
  158.     prints it. For instance, if size is 3, output would 
  159.     be:
  160.  
  161.  
  162.  
(Took most of the code out)

Sorry if this is confusing. Not sure how else I can explain it.
Feb 22 '07 #1
Share this Question
Share on Google+
11 Replies


DeMan
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
  1. loop (i from 0 to  size)
  2.   loop (j from 0 to size)
  3.     if( i = j)
  4.       print size;
  5.     else
  6.       print symbol
  7.     endif
  8.   endloop
  9.   print newline;
  10. endloop
  11.  
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
  1. loop (i from 0 to  size)
  2.   loop (j from 0 to size)
  3.     if( i = j)
  4.       print size;
  5.     else
  6.       print symbol
  7.     endif
  8.   endloop
  9.   print newline;
  10. endloop
  11.  
I think!!!
Hmm, how about if you wanted to use for
Feb 22 '07 #3

DeMan
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
  1. for(int i = 0; i< size; i++)
  2. {
  3. doThings()
  4. }
  5.  
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
  1. for(int i = 0; i< size; i++)
  2. {
  3. doThings()
  4. }
  5.  
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

DeMan
100+
P: 1,806
I assunme you mean
Expand|Select|Wrap|Line Numbers
  1. 5 $ $ $ $
  2. $ 5 $ $ $
  3. $ $ 5 $ $
  4. $ $ $ 5 $
  5. $ $ $ $ 5
  6.  
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
  1. /*    ==================== printOne ====================
  2.     This function receives the size of pattern one and 
  3.     prints it. For instance, if size is 5, output would 
  4.     be:
  5.                 5 $ $ $ $
  6.                 $ 5 $ $ $
  7.                 $ $ 5 $ $ 
  8.                 $ $ $ 5 $
  9.                 $ $ $ $ 5    
  10.        Pre   size
  11.        Post  pattern one printed 
  12.  
  13. */
  14. void  printOne  ( int size )
  15. {
  16. //    Local Definitions 
  17.     int i;
  18. //    Statements 
  19.     printf("\n\n\t\t");
  20.     printf("%d",size);
  21.     for(int i = 1; i< size; i++)
  22.     {
  23.         printf(" $");
  24.  
  25.     }// first line
  26.  
  27.  
  28.     printf("\n\t\t$ ");
  29.     printf("%d",size);
  30.     for(int i = 2; i< size; i++)
  31.     {
  32.         printf(" $");
  33.  
  34.     }// second line
  35.  
  36.     printf("\n\t\t$ $ ");
  37.     printf("%d",size);
  38.     for(int i = 3; i< size; i++)
  39.     {
  40.         printf(" $");
  41.  
  42.     }// third line
  43.  
  44.     printf("\n\t\t$ $ $ ");
  45.     printf("%d",size);
  46.     for(int i = 4; i< size; i++)
  47.     {
  48.         printf(" $");
  49.  
  50.     }// fourth line
  51.  
  52.     printf("\n\t\t$ $ $ $ ");
  53.     printf("%d",size);
  54.     for(int i = 5; i< size; i++)
  55.     {
  56.         printf(" $");
  57.  
  58.     }// fifth line
  59.  
  60.     printf("\n\n\n");
  61.  
  62.     return ;
  63. }// printOne
  64.  
  65.  
The result when I enter pattern 5, I get

Expand|Select|Wrap|Line Numbers
  1. Please type the size of the pattern [2 - 9]: 5
  2.  
  3.  
  4.                 5 $ $ $ $
  5.                 $ 5 $ $ $
  6.                 $ $ 5 $ $
  7.                 $ $ $ 5 $
  8.                 $ $ $ $ 5
  9.  
  10.  
  11.  
However, if I push pattern 3 or 4 I get a messed up pattern.

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.                 3 $ $
  4.                 $ 3 $
  5.                 $ $ 3
  6.                 $ $ $ 3
  7.                 $ $ $ $ 3
  8.  
  9.                 4 $ $ $
  10.                 $ 4 $ $
  11.                 $ $ 4 $
  12.                 $ $ $ 4
  13.                 $ $ $ $ 4
  14.  
  15.  
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
  1. void  printOne  ( int size )
  2. {
  3. //    Local Definitions 
  4.     int i;
  5. //    Statements 
  6.  
  7.     printf("%d",size);
  8.     for(int i = 1; i< size; i++)
  9.     {
  10.         printf(" $");
  11.         printf("\n");
  12.  
  13.     }// first line
  14.  
Which would print.

5
$
$
$
$

Is the way I'm thinking correct? Thanks in advance.
Feb 22 '07 #7

DeMan
100+
P: 1,806
pyut two gfor loops inside each other:

Expand|Select|Wrap|Line Numbers
  1. for(int i=0; i< size i++)
  2. {
  3.   for(int k=0; < size; k++)
  4.   {
  5.     //do your printing here, print size if i=k and $ otherwise
  6.   }
  7. //print newline here
  8. }
  9.  
Feb 22 '07 #8

P: 64
[quote]=DeMan]pyut two gfor loops inside each other:

Expand|Select|Wrap|Line Numbers
  1. for(int i=0; i< size i++)
  2. {
  3.   for(int k=0; < size; k++)
  4.   {
  5.     //do your printing here, print size if i=k and $ otherwise
  6.   }
  7. //print newline here
  8. }
  9.  
[/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
  1.  
  2. 5 $5 $5 $5 $
  3. 5 $5 $5 $5 $
  4. 5 $5 $5 $5 $
  5. 5 $5 $5 $5 $
  6.  
  7. void  printOne  ( int size )
  8. {
  9. //    Local Definitions 
  10.     int i;
  11.     int k;
  12. //    Statements 
  13.  
  14.  
  15.     for(int i=1; i<=4; i++)
  16.     {
  17.        for(int k=1; k< size; k++)
  18.        {
  19.                printf("%d",size);
  20.                printf(" $");
  21.        }
  22.     printf("\n");
  23.     }
  24.     return ;
  25.  
Feb 22 '07 #9

DeMan
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
  1. if( i==k) //need to equals signs if we're testing
  2. {
  3.   printf("%d", Size);
  4. }
  5. else
  6. {
  7.   printf("$");
  8. }
  9.  
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

DeMan
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

Post your reply

Sign in to post your reply or Sign up for a free account.