C Programming Q?

This is a discussion on C Programming Q? within the C Programming forums, part of the General Programming Boards category; I need to write a program which prints a square (made out of asterisks) of a certain size, something like ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    2

    C Programming Q?

    I need to write a program which prints a square (made out of asterisks) of a certain size, something like printing squares of size 1 to 10.

    "open'' squares, like this:

    ****
    * *
    * *
    ****
    instead of filled squares. as open boxes of 1 & 2 are not possible...im supposed to start from 3.

    Please help. Thanks in advance.
    PS- ive just started learning C...id prefer simple approach and codes. the length is not a problem.

  2. #2
    Registered User
    Join Date
    Nov 2009
    Posts
    2
    FYI the box is suposed to be with the asterixs n not with these - ,
    *****
    *----*
    *----*
    *----*
    *****

  3. #3
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Post the code for what u r waitin dude

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Well, given a number n in the range 1,10, the dimensions are (obviously) n*n. The first line you print has n "*"s, the last line you print has n "*"s, and every line in between has (n-2) " "s (spaces) surrounded by 2 "*s". There are (n-2) lines in between the first and last lines (lines with only "*"s). For example, n =3:
    Code:
    ***  // 3 "*"s
    * *  // 3-2 = 1 lines with 3-2=1 spaces, surrounded by 1 "*" on either side
    ***  // 3 "*"s
    Hopefully the solution is obvious. Give it an attempt, and post your code along with any errors/questions, when any arise.

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Well given a N size square. The first and last rows have N asterisks, with the rows in between having one asterisk, followed by N-2 spaces, followed by an asterisk.

    There really isn't much to it.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Creepy! :P

  7. #7
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Try to write some code or pusdo code dude than your learning will be faster

    Code:
    input
     loop 0 to input - 1 
     if 0 or input - 1
        loop 0 to input - 1 print *
        else
        print *
             loop 1 to input - 2 print ' '
        print *

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by RockyMarrone View Post
    Try to write some code or pusdo code dude than your learning will be faster

    Code:
    input
     loop 0 to input - 1 
     if 0 or input - 1
        loop 0 to input - 1 print *
        else
        print *
             loop 1 to input - 2 print ' '
        print *
    I wouldn't do it that way, just loop from N+1 to N-1 and avoid the "if-else" rubbish.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21