C Board  

Go Back   C Board > General Programming Boards > C# Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-04-2009, 06:12 PM   #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.
mdnealy is offline   Reply With Quote
Old 07-04-2009, 06:34 PM   #2
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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".
tabstop is offline   Reply With Quote
Old 07-05-2009, 01:07 AM   #3
Super Moderator
 
Bubba's Avatar
 
Join Date: Aug 2001
Posts: 7,470
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.
__________________
If you aim at everything you will hit something but you won't know what it is.
Bubba is offline   Reply With Quote
Old 07-05-2009, 05:02 AM   #4
Registered User
 
Join Date: Mar 2009
Location: england
Posts: 76
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;
        }
    }
}
theoobe is offline   Reply With Quote
Old 07-05-2009, 07:21 AM   #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.
mdnealy is offline   Reply With Quote
Old 07-05-2009, 08:17 AM   #6
Registered User
 
Join Date: Mar 2009
Location: england
Posts: 76
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?
theoobe is offline   Reply With Quote
Old 07-05-2009, 09:18 AM   #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.
mdnealy is offline   Reply With Quote
Old 07-05-2009, 10:45 AM   #8
Registered User
 
Join Date: Mar 2009
Location: england
Posts: 76
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. :-)
theoobe is offline   Reply With Quote
Old 07-05-2009, 03:23 PM   #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!!
mdnealy is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 05:47 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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