Additional profile information on Alfred Thompson at Google+
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)
i(1) = r.Next(0, 9)
Loop Until i(1) <> i(0)
i(2) = r.Next(0, 9)
Loop Until i(2) <> i(0) And i(2) <> i(1)
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)
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.
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. :-)
The Microsoft Visual Studio Middle School Power Toy 1.0 was originally created by Microsoft China to help meet the curriculum needs for teaching programming in that country. According to regulations/policies of China’s Ministry of Education (MOE) almost all Chinese high school students need to learn computer programming. Bring that up at the next meeting you attend where the need for programming/computer science is questioned! But I digress. This week the English language version of these tools were made available.
The Power Toy is a set of five tools that add into Visual Studio to help beginners. These tools are a free download. Three of the five work equally well with both Visual Basic and C#. Two of them (the assistant class designer and flow chart creator) only work with C# at least for now. The following descriptions comes from the download page. I plan to have individual posts on each of these tools in the very near future.