Thread: How to Draw a square within a square

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    4

    How to Draw a square within a square

    i know how to draw a square hollow or filled

    but i need to draw an alternating pattern

    ex
    iPatternHeight = 1
    X

    iPatternHeight = 3
    OOO
    OXO
    OOO

    iPatternHeight = 5
    XXXXX
    XOOOX
    XOXOX
    XOOOX
    XXXXX

    iPatternHeight = 7
    OOOOOOO
    OXXXXXO
    OXOOOXO
    OXOXOXO
    OXOOOXO
    OXXXXXO
    OOOOOOO

    anybody can help?

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I would say quit focusing exactly on the squareness of the shape. I will use the size 7 square, since I think it will clearly illustrate my thinking -- but it applies equally to the size 5 and size 3 ones. The 1 input is a special case, regardless.

    OOOOOOO

    OXXXXXO

    OXOOOXO

    OXOXOXO

    What do you notice about these lines? For each line, there is a specific number of X's and O's to draw. First it was all O's. Notice that as the X's are filled in, the line composed of the same characters on the inside is getting shorter by two. So one way to program it is to keep track of what row you are on, alternate characters to the start of the inner line, and then fill the inner line, and then alternate characters until you are done with the whole row. When you get down to the inner line length of 1, you know to alternate like OXOXOXO.

    The pattern below is the same as that pattern, done in reverse.

    OXOOOXO

    OXXXXXO

    OOOOOOO

    Once you can do it for this length, extend the concept for longer lines.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. draw an empty square of asterisks
    By rob90 in forum C Programming
    Replies: 3
    Last Post: 11-01-2009, 06:03 PM
  2. line-square and circle-square intersections
    By tomast in forum Game Programming
    Replies: 1
    Last Post: 11-16-2008, 08:12 AM
  3. Red Square? :(
    By Kai_Legends in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 11-14-2004, 09:13 PM
  4. for loops using square roots, square, Cube
    By lotf in forum C Programming
    Replies: 3
    Last Post: 03-21-2004, 04:29 AM
  5. Square
    By steerpike100 in forum C++ Programming
    Replies: 4
    Last Post: 04-07-2002, 12:09 PM