Thread: Square star pattern

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    2

    Square star pattern

    The program will ask the number for length and width then print it into a square star pattern.
    Ex. If the user inputted 6 for length and 4 for width
    ******
    * abcd*
    * abcd*
    ******

    The abcd indicates a space.

    Here is my code so far its not yet done and there's something really wrong about it so it would be really nice if you could help or guide me. Thank you.

    Code:
    #include<stdio.h>
    
    void main(void)
    {
    int wid, len, x, y, z;
    
    printf("Enter the number for length: ");
    scanf("%d", &len);
    printf("Enter the number for width: ");
    scanf("%d", &wid);
    
    for(x=1;x<=len;x++)
    printf("*");
    
    for(y=1;y<=wid-1;y++)
    {printf("\n*");
    
    for(z=1;z<=len-2;z++)
    printf("s");}
    
    }
    Last edited by Veanne Erida; 09-17-2011 at 11:29 PM.

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    That doesn't really look like an attempt at anything. Sit down and think about the problem. Figure out how you would do it, step by step. Then once you have that done, you can go ahead and start to program. You may find some use in reading through Lesson 3:Loops

    Additionally, void main is wrong. Read How to define main()-FAQ
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    One thing that helps when organizing stuff on a display is to think how you would do it on a typewriter ... write it down keystroke by keystroke...

    Most often in the process of writing it down, you end up with a codeable solution.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    2
    I just got it now. Here's my final code.

    Code:
    #include<stdio.h>
    
    int main(void)
    {
        int wid, len, w, x, y, z;
    
        printf("Enter the number for length: ");
        scanf("%d", &len);
        printf("Enter the number for width: ");
        scanf("%d", &wid);
    
        for(x=1;x<=len;x++)
           printf("*");
    
        for(y=1;y<=wid-2;y++)
           {printf("\n*");
        for(z=1;z<=len-2;z++)
           printf(" ");
           printf("*");}
    
           printf("\n");
    
        for(w=1;w<=len;w++)
           printf("*");
    
        return 0;
    }
    I'm sure there's a better way to code it but that's all my mind can do for now. Thank you guys. God bless.
    Last edited by Veanne Erida; 09-18-2011 at 12:50 AM.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    For that particular problem your solution is as good as any...

    Just keep in mind that, during the study/learing phase, it is just as important to work out how to analyse and understand a problem as it is to code a solution.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. line-square and circle-square intersections
    By tomast in forum Game Programming
    Replies: 1
    Last Post: 11-16-2008, 08:12 AM
  2. star wars vs star trek
    By psychopath in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 06-14-2005, 10:28 PM
  3. for loops using square roots, square, Cube
    By lotf in forum C Programming
    Replies: 3
    Last Post: 03-21-2004, 04:29 AM
  4. Star Wars Kid
    By FillYourBrain in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 05-04-2003, 09:23 PM
  5. Star Wars or Star Trek
    By Garfield in forum A Brief History of Cprogramming.com
    Replies: 32
    Last Post: 09-28-2001, 08:33 AM