Thread: Help with making simple patters from loops

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    6

    Help with making simple patters from loops

    I've been trying to solve this problem all day. I can't figure out what I am supposed to do. I really hope you can help me and soon. Thank you for any contribution you can make.

    Problem:
    With a prompted height and width, I need to have a checkerboard of the given height and width with squares of X's and spaces that are 2x2. If either of the inputs are odd then the far right or bottom part of the board should be cut off.
    Ex: 9 9 input should output:


    XX XX XXX XX X XX XX XX XX XX XX XXX XX X XX XX XX XX XX XX X

    This is my code so far:
    Code:
    #include <stdio.h>
    
    
    
    
       int main () {
          int height, width, row, col;
    
    
          printf("Enter a height and a width: ");
          scanf("%d%d", &height, &width);
    
    
          for (row = 0; row < height/4; row++) {
             for (col = 0; col < width/4; col++) {
                printf("XX");
                printf("  ");
    
    
             }
          printf("\n");
    
    
             for (col = 0; col < width/4; col++) {
                printf("XX");
                printf("  ");
    
    
             }
          printf("\n");
    
    
             for (col = 0; col < width/4; col++) {
                printf("  ");
                printf("XX");
             }
          printf("\n");
    
    
             for (col = 0; col < width/4; col++) {
                printf("  ");
                printf("XX");
             }
          printf("\n");
          }
    
    
          if (height % 3 == 1); {
             for (col = 0; col < width / 4; col++) {
                printf("XX");
                printf("  ");
             }
                printf("\n");
          }
    
    
    
    
       }
    I need it to be under 400 characters. Thank you!!

  2. #2
    Registered User
    Join Date
    Jan 2012
    Posts
    6
    Help with making simple patters from loops-screen-shot-2012-01-28-1-03-50-am-jpg
    This is the example from above, but properly formatted.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How do you expect to get that right most column of only a single x or space, if you are only ever trying to print two of them at a time?


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    6
    Quote Originally Posted by quzah View Post
    How do you expect to get that right most column of only a single x or space, if you are only ever trying to print two of them at a time?


    Quzah.
    I fixed that part, but for some reason my program keeps printing an extra line of the patten at the bottom on even height inputs. Also, I really need to know how to shorten this. Thank you!

    Code:
    #include <stdio.h>
    
    
    
    
       int main () {
          int height, width, row, col;
    
    
          printf("Enter a height and a width: ");
          scanf("%d%d", &height, &width);
    
    
          for (row = 0; row < height/4; row++) {
             for (col = 0; col < width/4; col++) {
                printf("XX  ");
             }
             if (height % 2 == 1)
                printf("X");
    
    
          printf("\n");
    
    
             for (col = 0; col < width/4; col++) {
                printf("XX  ");
             }
             if (height % 2 == 1)
                printf("X");
    
    
          printf("\n");
    
    
             for (col = 0; col < width/4; col++) {
                printf("  XX");
             }
          printf("\n");
    
    
             for (col = 0; col < width/4; col++) {
                printf("  XX");
             }
          printf("\n");
          }
    
    
          if (height % 2 == 1); {
             for (col = 0; col < width / 4; col++) {
                printf("XX  ");
             }
             if (height % 2 == 1)
                printf("X");
           printf("\n");
          }
    
    
    
    
       }

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    6

    I fixed it, but I still need to shorten it.

    The code works fine, but I need to condense it by at least 50 characters. Thank you!

    Code:
    #include <stdio.h>
    
    
    
    
       int main () {
          int height, width, row, col;
    
    
          printf("Enter a height and a width: ");
          scanf("%d%d", &height, &width);
    
    
          for (row = 0; row < height/4; row++) {
             for (col = 0; col < width/4; col++)
                printf("XX  ");
    
    
             if (width % 2 == 1)
                printf("X");
    
    
          printf("\n");
    
    
             for (col = 0; col < width/4; col++)
                printf("XX  ");
    
    
             if (width % 2 == 1)
                printf("X");
    
    
          printf("\n");
    
    
             for (col = 0; col < width/4; col++)
                printf("  XX");
    
    
             if (width % 2 == 1)
                printf(" ");
          printf("\n");
    
    
             for (col = 0; col < width/4; col++)
                printf("  XX");
    
    
             if (width % 2 == 1)
                printf(" ");
          printf("\n");
          }
    
    
          if (height % 2 == 1) {
             for (col = 0; col < width / 4; col++) {
                printf("XX  ");
             }
          if (height % 2 == 1)
                printf("X");
           printf("\n");
          }
    
    
    
    
       }

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Code:
    cat yoursource.c | sed 's/height/h/g' | sed 's/widht/w/g' | sed 's/row/r/g' | sed 's/col/c/r' > your_new_source.c
    / joking, if this is some odd requirement the above will probably not fly.

  7. #7
    Registered User
    Join Date
    Jan 2012
    Posts
    6
    Quote Originally Posted by Subsonics View Post
    Code:
    cat yoursource.c | sed 's/height/h/g' | sed 's/widht/w/g' | sed 's/row/r/g' | sed 's/col/c/r' > your_new_source.c
    / joking, if this is some odd requirement the above will probably not fly.
    I don't understand what this means...

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    It's a joke, but it will shorten your source by at least 50 characters. It's a unix pipeline, where each sed performs search and replace. So for example 'height' will be replaced with 'h' and so on. All your variable names will become single characters, which should shorten your source by more than 50 characters.

    But what are you looking for? And why 50 characters specifically?

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    There's a very short way:
    Code:
    get width and height
    for (i = 0; i < height; i++)
        for (j = 0; j < width; j++)
            if (some condition)
                putchar('X')
            else
                putchar(' ');
        putchar('\n')
    Or, if you're familiar with the ternary operator, drop the if/else and just do:
    Code:
    putchar(some condition ? 'X' : ' ');
    The trick is finding out what that "some condition" is. Hint, everything happens in doubles, two identical rows one after the other, or two identical columns one after the other. Thus, you need to take your row and column counters (i and j), and scale them down by two. There's an alternating pattern, both by row and column, so you need to know if your scaled down row/column counter is odd or even. There's some division and modulus in there somewhere.

    I think your 400 character limit doesn't include white space or new lines. My version, excluding white spaces, is 187 characters. With white space and new lines it's 201.

  10. #10
    Registered User
    Join Date
    Jan 2012
    Posts
    6
    I need it to be under 400. Its so that we better learn the concept of nested loops.

  11. #11
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    The above should work, but you need to alternate 'x' and ' ' as well. An option to do that is to hold them in two char variables and swap them every second row.

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Subsonics View Post
    The above should work, but you need to alternate 'x' and ' ' as well. An option to do that is to hold them in two char variables and swap them every second row.
    Nope, no need to alternate 'X' and ' '. Check your PM for the condition, don't wanna give away the answer!

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Ugly, in 243.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question on while loops
    By danieldcc in forum C Programming
    Replies: 4
    Last Post: 07-22-2011, 09:29 AM
  2. Design patters/discussion
    By jeffcobb in forum General Discussions
    Replies: 24
    Last Post: 02-11-2010, 10:36 PM
  3. For Loops (Making Stars)
    By GCNDoug in forum C++ Programming
    Replies: 5
    Last Post: 10-04-2007, 11:34 PM
  4. Simple C Program [for loops?]
    By mas0 in forum C Programming
    Replies: 6
    Last Post: 11-18-2006, 05:53 PM
  5. Replies: 2
    Last Post: 01-13-2003, 01:28 PM