hollow square

This is a discussion on hollow square within the C# Programming forums, part of the General Programming Boards category; need a hollow square 1-20 only. I cannot figure out how to make it hollow, only solid. where if you ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    4

    Question hollow square

    need a hollow square 1-20 only. I cannot figure out how to make it hollow, only solid. where if you input something less than 1=a square of 1 asterek(sp) and if greater than 20=square of 20x20

    here is what I have, just don't understand how to make it hollow, do I use a loop or if else..

    Code:
    static void Main(string[] args)
            {
    
                int side;
    
                Console.Write("Enter the side of your square: ");
                side = Convert.ToInt32(Console.ReadLine());
    
                Console.WriteLine();
                            
                    for (int i = 0; i < side;++ i )
                       
                {
                    for (int j = 0; j < side; ++j )
                        Console.Write("* ");
                    Console.WriteLine();
                    
                }
                 
                
                Console.WriteLine();
    
            }
        }
    Last edited by mdnealy; 07-04-2009 at 06:14 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Either (a) you'll need to decide, inside your loop whether to print a * or a space--if you look at coordinates (your i and j), what's true when you need to print * and what's true when you need to print space or (b) you'll have to adjust your loops slightly to print "side" vs "inside".

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    Why in the world are you writing asterisks in C# when you have so many simple graphics at your disposal? If this is for an assignment, I understand, but IMO they sorely need to alter the curriculum a bit.

    Simple window-graphics are about as easy and basic as they can be in C# .NET.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    Seems like a weird way to draw a rectangle, but anyway...

    Code:
    using System;
    
    namespace ConsoleApplication4
    {
        class Program
        {
            static void Main()
            {
                Console.Write("select the length of the sides: ");
    
                int len;
    
                if (!int.TryParse(Console.ReadLine(), out len))
                    Main(); // invalid input
    
                if (len < 1 || len > 20)
                    Main(); // invalid input
    
                for (int i = 0; i < len; i++)
                {
                    if (i == (len - 1) || i == 0) // first or last line
                        Console.WriteLine(Repeater("* ", len));
                    else // middle line
                        Console.WriteLine("* " + Repeater("  ", (len - 2)) + "* ");
                }
    
                Console.WriteLine();
                Main();
            }
    
            static String Repeater(String text, int count)
            {
                String tmp = "";
    
                for (int i = 0; i < count; i++)
                    tmp += text;
    
                return tmp;
            }
        }
    }

  5. #5
    Registered User
    Join Date
    Jul 2009
    Posts
    4
    I am just starting out in C# so please be a little patient .

    When I run the program I posted, it will give me a full square, i don't want a rectangle. but what i am having trouble with is how I make it hollow. The book we are learning from is easy when I put a program together like a gradebook or a calculator, but to create a graphic picture in console is throwing me off. The square can only have an input from 1 to 20. I do not have any examples in which to complete this in. I can create gui's but this is console only. In the post above this, it has commands that we have not gotten to. I am a quick study but I learn with repition(sp). It is an assignment but I am just having trouble grasping it. Once I see the code, I can usually build off of that. Dietel books are a little vague and I did not go over this in class...
    Last edited by mdnealy; 07-05-2009 at 07:23 AM.

  6. #6
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    I used the word Rectangle because in Graphics, a square would fall into the designation of Rectangle, although obviously all the sides will be the same length.

    Could you please explain how the code I've posted fails to meet your criteria?

  7. #7
    Registered User
    Join Date
    Jul 2009
    Posts
    4
    Code:
    if (!int.TryParse(Console.ReadLine(), out len))
                    Main(); // invalid input
    
                if (len < 1 || len > 20)
                    Main(); // invalid input
    
                for (int i = 0; i < len; i++)
                {
                    if (i == (len - 1) || i == 0) // first or last line
                        Console.WriteLine(Repeater("* ", len));
                    else // middle line
                        Console.WriteLine("* " + Repeater("  ", (len - 2)) + "* ");
                }
    
                Console.WriteLine();
                Main();
            }
    
            static String Repeater(String text, int count)
            {
                String tmp = "";
    
                for (int i = 0; i < count; i++)
                    tmp += text;
    
                return tmp;
    some of the coding i highlighted we haven't gotten to yet. like tryparse. it is quite a few chapters away. your code is helpful especially once we get there. your If statements helped. but like I said i am just a beginner in coding

    I am trying to rewrite it to match up to what we have learned so far.

  8. #8
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    Ok, well int.TryParse works by evaluating a string and converting the result to an integer, similar to Convert.ToInt32().

    However it is useful in that it incorporates error handling. In other words, if the user entered something non-numeric, rather than C# throwing an exception, it will simply return FALSE instead.

    Example 1:

    Code:
    int i;
    bool test = int.TryParse("12345", out i);
    Example 1 shows that the string value "12345" will be converted to the integer value of 12345 and stored in local variable i. The return value of the int.TryParse operation will be TRUE (because the conversion was successful).

    Example 2:

    Code:
    int i;
    bool test = int.TryParse("hello world", out i);
    Example 2 shows me trying to convert "hello world" into a number, which clearly would never work. As a result, local variable i remains unassigned, and the int.TryParse operation returns FALSE, but without throwing an exception.

    Hope this helps. :-)

  9. #9
    Registered User
    Join Date
    Jul 2009
    Posts
    4
    it helped tons and made more sense. thanks for taking the time to do this. this discussion board has given me more answers than i have found in about 4 days!! thanks again for your help!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loop seg error
    By Zishaan in forum Game Programming
    Replies: 2
    Last Post: 03-28-2007, 01:27 PM
  2. Forced moves trouble!!
    By Zishaan in forum Game Programming
    Replies: 0
    Last Post: 03-27-2007, 06:57 PM
  3. Help with my draughts game
    By Zishaan in forum C++ Programming
    Replies: 9
    Last Post: 03-24-2007, 07:33 AM
  4. hollow square
    By jms318 in forum C Programming
    Replies: 5
    Last Post: 11-30-2006, 10:36 PM
  5. Hollow square
    By Kayoss in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 04:49 PM

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