Computer Science Teacher

# July, 2008

• #### The Four Digit Problem

So I was remembering a piece of code I had to write once. Honestly I don’t remember exactly why I had to write it. I think it may have been part of a set of patterned data for some test software though. In any case the problem was to generate a four digit random number with no duplicated digits. Now there are lots of ways to do this. A simple brute force way is below.

```    Function Digit4() As Integer

Dim i(4) As Integer
i(0) = r.Next(1, 9)
Do
i(1) = r.Next(0, 9)
Loop Until i(1) <> i(0)
Do
i(2) = r.Next(0, 9)
Loop Until i(2) <> i(0) And i(2) <> i(1)
Do
i(3) = r.Next(0, 9)
Loop Until i(3) <> i(0) And i(3) <> i(1) And i(3) <> i(2)
Return i(0) * 1000 + i(1) * 100 + i(2) * 10 + i(3)
End Function```

Yes I left out the comments to save space. (That’s my story and I’m sticking to it) The way it works is to pick a single random digit, then pick a second one looping back to pick a new one if by some chance the digit drawn is the same as the one previously checked. This is done again except that the next one has two numbers to compare against and the final number has three numbers to compare against. It works (I tested it) but it doesn’t scale well.

The task I would assign students is to come up with at least two other ways to solve this problem that are more scalable and then compare them all for performance. I might hint at the word “recursion” which I’ve been thinking a lot about lately. This sample code is in Visual Basic because that is my hack something together language of choice. Converting it to other languages is another exercise left to the student.

Two notes: The best part about this post is all the discussion in the comments so don't miss them. Second is that the "solution" I entered is what students often come up with and not what I would use is a real application. I wanted to get some discussion going and that seems to have happened.

Edit: 12/2/2009 Bart Massey of Portland State University has a very helpful reply post called Random non-repeating sequences that I highly recommend. I appreciate his letting me know about it as I think it is an interesting solution that is well explained. So please go read it.
• #### Teaching Flowcharts – Have the Computer Draw One

Last week the Microsoft Visual Studio Middle School Toy was announced and I wrote about it in my blog. Today I wanted to give people a taste of what one of the features – the Visual Programming Flow Chart - looks like. It’s really pretty simple to use. Point the mouse at a function/method name and right click for the context menu like this and select Generate flow chart

The result will look something like this:

The color bars on the right let the user change the color coding for different things like loops, if statements, Try statements, etc. The resulting image can be saved as a JPEG file. This lets the user include it as documentation if they want.

This is not a super serious professional tool BTW. It seems to do a pretty good job for the student level modules I’ve tried it with though. I can see where it could be very useful for students because it shows what the logic of the code actually is rather than what the student might think it is. Note that right now it only works with C# – sorry about that. I’ve already started bugging people about Visual Basic support. :-)

• #### Goof Off Monday

I have nothing serious today. I sent something to the AP CS mailing list and got back a lot of out of office messages. I'm guessing a lot of people, especially teachers, are just relaxing these days. So today, just ways to have some fun and avoid real work. My friend Hilary sent me the following information last Friday.

We recently asked a number of students to use Popfly and submit a Popfly creation (a mash-up or a game).  10 of the games that were created were picked by the Popfly Team in the Games We Love  http://popflywiki.com/GamesWeLove.ashx entry in the Popfly wiki.

Please find the links below … beware … these games are fun!

http://www.popfly.com/users/mtaipan/theGunner

http://www.popfly.com/users/solidcell/Move%20It

http://www.popfly.com/users/karafong/ChickyBang

http://www.popfly.com/users/Dauble2k5/Shoot%20for%20the%20Stars

http://www.popfly.com/users/sachint/Demon%20Attack