473,385 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to change/print pattern.

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
11 2566
DeMan
1,806 1GB
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
td0g03
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
1,806 1GB
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
td0g03
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
1,806 1GB
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
td0g03
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
1,806 1GB
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
td0g03
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
1,806 1GB
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
td0g03
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
1,806 1GB
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

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

Similar topics

5
by: Markus Elfring | last post by:
Hello, I try to use alternative delimiters for a regular expression. When will it be supported? www@mike:/home/www > /usr/local/bin/php -a Interactive mode enabled <?php...
8
by: David Horne | last post by:
I forgot to mention in my other posting that the program is suppose to figure out the minimum amount of different dollars and cents that would be required for a given amount.
1
by: Spike | last post by:
Hello! Im going to make a javascript for changing alot of images. But im not sure how to do it., where to start.. Ok, first.. this is the isue. I have 3 images(I call them 1a-3a). when u...
0
by: Sebastiao Pereira Filho | last post by:
When I bind a server control to a repeater it renders one copy of this control for each item in the datasource collection. By doing this it changes the name of the dynamically created controls, to...
41
by: Petr Jakes | last post by:
Hello, I am trying to study/understand OOP principles using Python. I have found following code http://tinyurl.com/a4zkn about FSM (finite state machine) on this list, which looks quite useful for...
1
by: Gary Wessle | last post by:
Hi import string import re accumulator = pattern = '(\S*)\s*(\S*)\s*(\S*)' for each text file in dir openfile and read into text
6
by: mirandacascade | last post by:
O/S: Win2K Vsn of Python: 2.4 Here is copy/paste from interactive window of pythonwin: >>> x = "Joe's desk" >>> y = 'Joe\x92s desk' >>> type(x) <type 'str'> >>> type(y)
1
by: Andrew Savige | last post by:
Python beginner here. For a string 'ABBBCC', I want to produce a list . That is, break the string into pieces based on change of character. What's the best way to do this in Python? Using...
6
by: zhangsonglovexiaoniuniu | last post by:
Dear all, I need you help. here the program: char a = 0x91; printf("%x",a);
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.