Stuff that's in my head

Can open... Worms everywhere! The blog of Colin Angus Mackay
posts - 291, comments - 278, trackbacks - 43

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

Width:8
Height:5
********
*      *
*      *
*      *
********

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: ,,,,

Print | posted on Tuesday, January 22, 2008 8:17 AM

Feedback

# re: A simple challenge

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.
1/22/2008 9:42 AM | John McDowall
Gravatar

# re: A simple challenge

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!)
1/30/2008 4:01 PM | Ron Todd

# Solution: A simple challenge

Solution: A simple challenge
2/2/2008 1:43 AM | Stuff that's in my head
Gravatar

# re: A simple challenge

The solution is now available here: http://blog.colinmackay.net/archive/2008/02/02/1777.aspx
2/2/2008 1:44 AM | Colin Angus Mackay

Post Comment

Title  
Name  
Email
Url
Comment   

Powered by: