Thread: Printing Patterns

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    7

    Printing Patterns

    I've tried many ways to do this, but can't seem to find out how to make patters such as these in a C program. I've tried most things I've learned about C so far, but still no cigar. Last time I came here I got outstanding help from you guys, so thanks very much for that! If any more information is needed then feel free to ask for it. I have the rest of the program but it is quite long now so I thought I'd simplify it a bit.

    Here's the basics of my program:
    1. User see's options of patterns.
    2. User chooses an option.
    3. User chooses a size (more details on this below).
    4. The correct pattern & size is printed.
    Size can be [ 2 - 9 ]

    Here are what the functions should look like after printing.
    Pattern One:
    if size is 5:

    1 * * * *
    5 2 * * *
    5 5 3 * *
    5 5 5 4 *
    5 5 5 5 5

    if the size is 6:

    1 * * * * *
    6 2 * * * *
    6 6 3 * * *
    6 6 6 4 * *
    6 6 6 6 5 *
    6 6 6 6 6 6


    Pattern Two:
    (random numbers within the range 0 to size, inclusive)
    if size is 5:
    *********
    * 0 4 3 *
    * 1 2 5 *
    * 3 0 1 *
    *********
    if size is 6:
    ***********
    * 3 6 2 0 *
    * 5 3 0 2 *
    * 4 5 3 1 *
    * 1 2 0 3 *
    ***********


    Pattern Three:
    The printed pattern does not include the - 's. I put those in because it is all aligned to the right, and this forum does not allow me to use spaces like that. However I just saw the Align Right button.... oh well you know what I was getting at.
    if size is 5:
    and the first number is 0:
    --- 0 1 2 3 4
    ----- 5 6 7 8
    ------- 9 0 1
    --------- 2 3
    ----------- 4


    f size is 5:
    --- 5 6 7 8 9
    ----- 0 1 2 3
    ------- 4 5 6
    --------- 7 8
    ----------- 9
    and the first number is 5:
    The first number in the pattern is a random integer between 0 and 9 inclusive. All numbers in the pattern are integers within the same range. Notice that they are consecutive integers as you read the pattern one row at a time, left to right, and when you get to ten, the number changes back to 0.


    Thanks for any help, this one has got me stumped.
    Last edited by ferniture; 11-17-2008 at 06:20 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Let's start easy. Do you know how to print a grid of (say) random numbers? Once you have that, then you can look at putting conditionals (if statements) inside that structure to print specific things based on where you are (you'll need to be able to print spaces in certain cases; fixed numbers in certain cases).

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    7
    I know how to print random numbers, but as a grid I'm not sure. I get the feeling that it takes more than simply making a lot of printf lines printing spaces, *'s and integers.

    Also, would it be possible to do this with loops of any sort?

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> I've tried many ways to do this, but can't seem to find out how to make patters such as these in a C program.

    Rather than simply saying that you have tried and failed, post the actual code you are struggling with.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by ferniture View Post
    Also, would it be possible to do this with loops of any sort?
    Only in the sense that it would be impossible to do it any other way.

  6. #6
    Registered User
    Join Date
    Jul 2008
    Posts
    7
    Quote Originally Posted by Sebastiani View Post
    >> I've tried many ways to do this, but can't seem to find out how to make patters such as these in a C program.

    Rather than simply saying that you have tried and failed, post the actual code you are struggling with.
    The code I used was simply me making multiple efforts to change the value of "size" to be able to print it differently, whether it be adding or subtracting 1 from it and using another holding int to keep the original number. Problem is I couldn't get the pattern to work right with it. I'm getting a few ideas as I type this though, so I'll post my results in a bit. But still any help is appreciated, and thanks for the replies so far.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Consider the line. It has a total width. It needs a variable computer for that. It has a width (in char's of course), for the number of letters (char's), that it will need to print as a *, it also needs a variable for the number of times it will print anything else.

    Total width of line = width of stars to print + width of anything else.

    If I print one more star, the width to print out anything else must decrement by 1.

    That will definitely get you thinking in the right direction.

  8. #8
    Registered User
    Join Date
    Jul 2008
    Posts
    7
    How can I set the width of a line? I tried using Google, but came up with nothing. I found a page that said something about "maxoutput" but didn't go into any real detail other than saying that it exists.

    I'm sorry, but I didn't follow your post very well. Could you go into a little more detail?

    What I did for the first pattern is the code below, which is very "primitive" so to speak.
    Code:
    printf( "Pattern One\n\n" );
        if (size == 2)
        {
          printf("1 *\n");
          printf("2 2\n");
          }
          else if (size == 3)
        {
          printf("1 * *\n");
          printf("3 2 *\n");
          printf("3 3 3\n");
          }
          else if (size == 4)
        {
          printf("1 * * *\n");
          printf("4 2 * *\n");
          printf("4 4 3 *\n");
          printf("4 4 4 4\n");
          }
          else if (size == 5)
          {
               printf("1 * * * *\n");
               printf("5 2 * * *\n");
               printf("5 5 3 * *\n");
               printf("5 5 5 4 *\n");          
               printf("5 2 5 5 5\n");          
               }
               else if (size == 6)
               {
                    printf("1 * * * * *\n");
                    printf("6 2 * * * *\n");     
                    printf("6 6 3 * * *\n");     
                    printf("6 6 6 4 * *\n");
                    printf("6 6 6 6 5 *\n");
                    printf("6 6 6 6 6 6\n");
                    }
                    else if (size == 7)
                    {
                         printf("1 * * * * * *\n");
                         printf("7 2 * * * * *\n");    
                         printf("7 7 3 * * * *\n");
                         printf("7 7 7 4 * * *\n");
                         printf("7 7 7 7 5 * *\n");
                         printf("7 7 7 7 7 6 *\n");
                         printf("7 7 7 7 7 7 7\n");
                         }
                         else if (size == 8)
                         {
                              printf("1 * * * * * * *\n");
                              printf("8 2 * * * * * *\n");
                              printf("8 8 3 * * * * *\n");
                              printf("8 8 8 4 * * * *\n");
                              printf("8 8 8 8 5 * * *\n");
                              printf("8 8 8 8 8 6 * *\n");
                              printf("8 8 8 8 8 8 7 *\n");
                              printf("8 8 8 8 8 8 8 8\n");
                              }
                              else if (size == 9)
                              {
                                   printf("1 * * * * * * * *\n");
                                   printf("9 2 * * * * * * *\n");
                                   printf("9 9 3 * * * * * *\n");
                                   printf("9 9 9 4 * * * * *\n");
                                   printf("9 9 9 9 5 * * * *\n");
                                   printf("9 9 9 9 9 6 * * *\n");
                                   printf("9 9 9 9 9 9 7 * *\n");
                                   printf("9 9 9 9 9 9 9 8 *\n");
                                   printf("9 9 9 9 9 9 9 9 9\n");
                                   }

  9. #9
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    That is not what they want you to do. If I was marking it, you'd probably get a 1 out of 10 (for effort).

    To control the width of a line, just print however many characters you want, then a "\n".
    Last edited by zacs7; 11-17-2008 at 08:58 PM.

  10. #10
    Registered User
    Join Date
    Jul 2008
    Posts
    7
    Quote Originally Posted by zacs7 View Post
    That is not what they want you to do. If I was marking it, you'd probably get a 1 out of 10 (for effort).
    I'm aware of this, which is why I made the thread here. Please let me know if you have an idea of how to do it another way.

    Quote Originally Posted by zacs7 View Post
    To control the width of a line, just print however many characters you want, then a "\n".
    I thought Adak was talking about some way to set the max characters on a line, and if there was more characters than the set max then it would only show the characters up until the max.

    Ex:
    max = 10 //however it is actually set is beyond me
    printf( "012345678910" );

    output: 0123456789

    I may have read his post wrong though.
    Last edited by ferniture; 11-17-2008 at 09:12 PM.

  11. #11
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Okay...

    Code:
    int   widthOfAnythingElse = 0,
          widthOfLine = 0,
          stars = 0;
    
    /* I want a line width of 10 in total, with 3 stars */
    stars = 3;
    widthOfLine = 10;
    widthOfAnythingElse = widthOfLine - stars;
    
    for(i = 0; i < widthOfAnythingElse; i++)
    {
       putchar(' ');
    }
    
    for(i = 0; i < stars; i++)
    {
       putchar('*');
    }
    
    putchar('\n');
    Of course there are many ways to do it

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Take what you have above, and REPRESENT it through the use of a loop.


    Code:
    /* Printing some lines,  Adak
    if (size == 6)
      {
          printf("1 * * * * *\n");
          printf("6 2 * * * *\n");     
          printf("6 6 3 * * *\n");     
          printf("6 6 6 4 * *\n");
          printf("6 6 6 6 5 *\n");
          printf("6 6 6 6 6 6\n");
      }
    */
    
    #include <stdio.h>
    
    int main(void)   {
       int i, j, k, size, totalsize, number, gar;
       totalsize = 6;
    
       printf("\n");
    
       //Outer for loop represents and controls the total number of sets to be printed
      
       for(k = 1; k <= totalsize; k++)  {
    
          size = k;
          printf("\n");
    
          //This for loop represents the total number of lines to be printed, in each
          //set size, and the width of that line, in char's.
    
          for(i = 1; i <= size; i++)  { 
         
             for(j = size-1; j > size - i; --j)  //controls printing set size number 
                printf("&#37;d ", size);
                                             
             printf("%d ", i);                   //prints the last digit of each line
    
             for(j = 1; j <= size - i; j++)     //controls the asterisks
                printf("* ");
    
             printf("\n");
          }
       }
       printf("\n\n                                 Press Enter to Quit ");
       gar = getchar(); gar++;
       return 0;
    }
    Last edited by Adak; 11-17-2008 at 10:03 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C# Printing Problem
    By silverlight001 in forum C# Programming
    Replies: 0
    Last Post: 03-23-2009, 01:13 AM
  2. generic printing preferences dialog box
    By stanlvw in forum Windows Programming
    Replies: 8
    Last Post: 06-27-2008, 02:20 AM
  3. printing data to a file
    By coralreef in forum C Programming
    Replies: 3
    Last Post: 11-02-2006, 08:10 PM
  4. need help relating printing?
    By omarlodhi in forum Linux Programming
    Replies: 0
    Last Post: 03-03-2006, 04:46 AM
  5. Cprog tutorial: Design Patterns
    By maes in forum C++ Programming
    Replies: 7
    Last Post: 10-11-2004, 01:41 AM