tags:

views:

124

answers:

5

Hello!

I'm a student and I got a homework i need some minor help with =)

Here is my task:

Write an application that prompts the user to enter the size of a square and display a square of asterisks with the sides equal with entered integer. Your application works for side’s size from 2 to 16. If the user enters a number less than 2 or greater then 16, your application should display a square of size 2 or 16, respectively, and an error message.

This is how far i've come:

        start:
        int x;
        string input;
        Console.Write("Enter a number between 2-16: ");
        input = Console.ReadLine();
        x = Int32.Parse(input);
        Console.WriteLine("\n");
        if (x <= 16 & x >= 2)
        {
          control statement
          code
          code
          code
        }
        else
        {
            Console.WriteLine("You must enter a number between 2 and 16");
            goto start;
        }

I need help with...

... what control statment(if, for, while, do-while, case, boolean) to use inside the "if" control.

My ideas are like...

do I write a code that writes out the boxes for every type of number entered? Thats ALOT of code...

..there must be a code containing some "variable++" that could do the task for me, but then what control statment suits the task best?

But if i use a "variable++" how am I supposed to write the spaces in the output, because after all, it has to be a SQUARE?!?! =)

I'd love some suggestions on what type of statements to use, or maybe just a hint, of course not the whole solution as I am a student!

+1  A: 

You do not have to write code for every type of number entered. Instead, you have to use loops (for keyword).

Probably I must stop here and let you do the work, but I would just give a hint: you may want to do it with two loops, one embedded in another.

I have also noted some things I want to comment in your code:

  1. Int32.Parse: do not use Int32, but int. It will not change the meaning of your code. I will not explain why you must use int instead: it is quite difficult to explain, and you would understand it later for sure.
  2. Avoid using goto statement, except if you were told to use it in the current case by your teacher.
  3. Console.WriteLine("\n");: avoid "\n". It is platform dependent (here, Linux/Unix; on Windows it's "\r\n", and on MacOS - "\n\r"). Use Environment.NewLine instead.
  4. x <= 16 & x >= 2: why & and not ||?
  5. You can write string input = Console.ReadLine(); instead of string input; followed by input = Console.ReadLine();.
MainMa
Why FOUR? Two should be enough..
Oren A
@Oren A: thank you; by mistake I thought the requirement was to draw an *empty* rectangle (with borders).
MainMa
Why the downvotes? Even the 4-loop idea is perfectly valid assuming that the square should be filled with spaces (which is ambiguous in the spec).
Gabe
@MainMa: Oh, in that case you were right (-: (-- undone)
Oren A
+1 Thank you for the clear input! That was very interesting with the "\n" thing, good to know, and thanks for giving a code to replace it!
A: 

the control statement is:

for (int i = 0; i < x; i++)
{
  for ( int j = 0; j < x; j++ )
  {
    Console.Write("*");
  }
  Console.WriteLine();
}

This should print a X by X square of asterisks! I'ma teacher and I left the same task to my students a while ago, I hope you're not one of them! :)

David Conde
try doing the homework alone next time.... I gave the answer this time, because its a very simple exercise, but if you can't crack this one, you should study more.
David Conde
@dconde: You can always provide some hint without revealing a complete solution. As long as the OP states that it is a homework, asking for a help is fine!
Tomas Petricek
I realized that too late... sorry, I just hope this gives fierflash some boost to start programming.. :) hopefuly
David Conde
Aah thank you, I'll take some time to think about this... I was hanging on by the thought that the square would be empty(which i suppose would make it a bit harder).... Guess I need to go back to some outside-inside-box thinking haha..
+1  A: 

Since it's homework, we can't give you the answer. But here are some hints (assuming solid *'s, not white space in-between):

  1. You're going to want to iterate from 1 to N. See for (int...
  2. There's a String constructor that will allow you to avoid the second loop. Look at all of the various constructors.

Your current error checking does not meet the specifications. Read the spec again.

You're going to throw an exception if somebody enters a non-parsable integer.

goto's went out of style before bell-bottoms. You actually don't need any outer control for the spec you were given, because it's "one shot and go". Normally, you would write a simple console app like this to look for a special value (e.g., -1) and exit when you see that value. In that case you would use while (!<end of input>) as the outer control flow.

Rob
+1  A: 

If x is greater or equal to 16, why not assign 16 to it (since you'll eventually need to draw a square with a side of length 16) (and add an appropriate message)?

Oren A
-1: the idea is that X must be read from the console... not just assing a number to it..!
David Conde
@dconde: My idea is that once it is assigned a value equal or greater than 16, the length should be 16, and so he should "fix" it.
Oren A
+1. I thought of this as well. The 16 get's assigned only if a number above 16 is the input, as the assignment states that number above 16 should print a 16 square.
cake
+1. This doesn't mean avoiding the console input, this means testing the value gotten from the console and clipping it to the acceptable range.
Ben Voigt
@Timwi: How is it irrelevant to the question? How about this part - " If the user enters a number less than 2 or greater then 16, your application should display a square of size 2 or 16, respectively, and an error message."
Oren A
Good point, I missed that. Comment deleted.
Timwi
@Timwi: and -1 undone? (-:
Oren A
[No.](http://meta.stackoverflow.com/questions/6250/)
Timwi
Ok. Thanks anyhow.
Oren A
+4  A: 

It's not the answer you're looking for, but I do have a few suggestions for clean code:

  1. Your use of Int32.Parse is a potential exception that can crash the application. Look into Int32.TryParse (or just int.TryParse, which I personally think looks cleaner) instead. You'll pass it what it's parsing and an "out" parameter of the variable into which the value should be placed (in this case, x).
  2. Try not to declare your variables until you actually use them. Getting into the habit of declaring them all up front (especially without instantiated values) can later lead to difficult to follow code. For my first suggestions, x will need to be declared ahead of time (look into default in C# for default instantiation... it's, well, by default, but it's good information to understand), but the string doesn't need to be.
  3. Try to avoid using goto when programming :) For this code, it would be better to break out the code which handles the value and returns what needs to be drawn into a separate method and have the main method just sit around and wait for input. Watch for hard infinite loops, though.

It's never too early to write clean and maintainable code, even if it's just for a homework assignment that will never need to be maintained :)

David
I would just like to emphasize 3. And I would also like to re-emphasize it (-:
Oren A
@Timwi: Good catch, thanks! I've updated it. Guess I need to pay more attention when writing here (the kids are being particularly loud tonight...)
David