A simple challenge

A homework question was posted on a forum recently. As usual it was met with the cries of “We won’t do your homework for you” (We will, of course, help if you get stuck. But don’t expect any assistance without at least trying first)

The assignment was to write a console application that accepted a width and a height from the user and then created a rectangle using asterisks. e.g. This might be the result of the program

*      *
*      *
*      *

Now the query was asked how to complete this using loops (or conditional statements, we weren’t really sure – he said loops, but proceeded to talk about if/else statements)

One of the responses was along the lines of “I can do this without loops”, modified to add “Or conditional statements”, and modified again to add “Or recursion”

So, my challenge to you is to write a small program above without loops, conditional statements or recursion. It sounds difficult, but it isn’t really. I have a solution (from which the example output above has been taken) which I’ll post this evening.

Technorati Tags: ,,,,

5 thoughts on “A simple challenge

  1. What about this horror:class Program { static void Main(string[] args) { int w = int.Parse(args[0]); int h = int.Parse(args[1]); Console.Write( (‘c’ + new string(‘w’, h – 2) + ‘c’) .Replace(“w”, “*” + new string(‘ ‘, w – 2) + “*n”) .Replace(“c”, new string(‘*’, w) + “n”)); } }There is defo a nice way to do this though. I will think on it.

  2. Tested John’s solution, and was impressed to see the advertised rectangles appearing. Pretty, pret-tay good.(Where’s the real answer, Colin? It’s a week overdue!)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s