# Challenge of the Month - December 2013

• ### General discussion

• These challenges are intended for people who are learning to program for the first time or for those returning to programming who want to start using SmallBasic.  Some will be easy, some will be hard - but they will all make you think, and more importantly be GREAT FUN!

Please post your solutions / partial solutions / questions / feedback etc. into this thread that will remain 'sticky' for the month.  The only rule is that your solution must use standard SmallBasic methods (no extensions).

It would be good if people could post their problems with these challenges so that a discussion can start so that everyone can learn from each other.

Also post feedback on the kind of challenges that you want to see more of in the future.

Curriculum Challenges 2.6

1. Write a program to display 9 Flickr images in a 3 by 3 grid scaled to fit in a grid within the GraphicsWindow.
2. Load the 9 Flickr images into an array of ImageList objects and then show one of the images to fill the GraphicsWindow when the user clicks one of the 9 images.
3. Make a program that continuously scrolls Flickr images from left to right continuously downloading images - make it as smooth as possible.

Text Challenges

1. Draw a face in the TextWindow.  Consider using colours (TextWIndow.ForegroundColor and TextWindow.BackgroundColor), suitable characters (Text.GetCharacter) and careful positioning (TextWindow.CursorLeft and TextWindow.CursorRight).

Graphical Challenges

1. Write a program that show a reflection of a picture you draw.
2. Draw a snowflake with 6 sided symmetry.

Maths Challenges

1. Write a program to calculate the largest number of prime numbers in 1 minute.

Physics Challenges

1. Write a program to simulate a hot air balloon.  Perhaps include a burner.
• The balloon goes up when the total displaced mass of air is greater than the mass of the balloon+basket (Buoyancy).  See here, here or here for some ideas.
• The mass of air in the balloon goes down when it is heated (Thermal expansion).
• The air in the balloon slowly cools with time (Conduction, Convection).
• As we go higher the air thins and the mass of air displaced goes down (The balloon has an altitude limit).

Game Challenge

1. Write a clone of the Simon game.

Community Suggestions (by Anthony Yarrell)

1. Implement the ROT-13 substitution cypher using Small Basic. For those who are interested, check out the Wikipedia page for ROT-13 which gives lots of details for how it works.

Community Suggestions (by Nonki)

1. Calculate the frequency of other pitches while pitch A is 440.00Hz.
2. Create a control called "Radio Button".
3. Develop an operation between colors.

Community Suggestions (by Jibba Jabba)

1. Build an extensible drop-down menu

Do you have an idea for a future challenge? Please post it here!

Sunday, December 1, 2013 12:13 PM

### All replies

• Here are two programs that I wrote for the reflection challange:

JKR470 this one get a random image and makes a reflection

KKB378 this one makes a reflection of what you draw above the white line. press enter to render.

also see: CLR717

It is written: "'As surely as I live,' says the Lord, 'every knee will bow before me; every tongue will acknowledge God.'" Romans 14:11

Tuesday, December 3, 2013 7:23 PM
• @ Zock77 - very good answers.

Further suggestions - could do the drawing one so the reflection is drawn as you draw rather than when Return is pressed.  Perhaps also the reflection could be slightly transparent or duller in some way.

Tuesday, December 3, 2013 8:21 PM
• Just playing around with it: BGW081

It gets darker the farther down it goes.

It is written: "'As surely as I live,' says the Lord, 'every knee will bow before me; every tongue will acknowledge God.'" Romans 14:11

Tuesday, December 3, 2013 11:29 PM
• Game Challenge   Simon game.   VJL218-0

Simon image changed. Clicked color visible.

Wednesday, December 4, 2013 12:58 PM
• I started to program Simon game: FHV760.

At first, I created a mask pattern below with this program and GIMP.

Nonki Takahashi

Thursday, December 5, 2013 12:29 PM
• This is my game challenge: FHV760-5.

Nonki Takahashi

Thursday, December 5, 2013 2:21 PM
• My program that will show a reflection of a picture the user draws: QKR009

My computer didn't like this running without big delays, so I set up a few "Program.Delay(20)" code pieces in there to help it.

Comments throughout if you want to change something like make it run better/smoother on your computer!

For reference, my computer has a Dual-Core processor that runs at 1.7 GHz. If you have anything better than that, go ahead and reduce the delays!

I can write 1000 lines of code in roughly 10 seconds. Here's how:
i = 1

For i = 1 to 1000
TextWindow.WriteLine(i+ ". Code")
Program.Delay(10)
EndFor

Make Sense?

Friday, December 6, 2013 1:33 AM
• Hey Joman, I commented out the Program.Delays and it ran really well on my pc. Good program I reckon.

Saturday, December 7, 2013 7:28 AM
• Here is my version of the reflection while drawing program: TZH768

It is written: "'As surely as I live,' says the Lord, 'every knee will bow before me; every tongue will acknowledge God.'" Romans 14:11

Saturday, December 7, 2013 7:53 PM
• Here's my start on the pic viewer challenge.  MXK111

It's a continuous d/load of Flickr pics drawn on the GW every half second.

It'll be interesting when I get to the continuous scroll left to right part.

Saturday, December 7, 2013 9:17 PM
• Hey Joman, I commented out the Program.Delays and it ran really well on my pc. Good program I reckon.

What frequency does your processor run at? I'm curious how my new computer (just got it today) will run it :P

I can write 1000 lines of code in roughly 10 seconds. Here's how:
i = 1

For i = 1 to 1000
TextWindow.WriteLine(i+ ". Code")
Program.Delay(10)
EndFor

Make Sense?

Sunday, December 8, 2013 12:19 AM
• Joman, my laptop runs at 1.73GHz.

New PC, good stuff. Hope it flies.

Sunday, December 8, 2013 3:43 AM
• Joman, my laptop runs at 1.73GHz.

New PC, good stuff. Hope it flies.

What makes it crash on my lappy then?!

Thanks! And it does :P  I'm glad I bought it!

I can write 1000 lines of code in roughly 10 seconds. Here's how:
i = 1

For i = 1 to 1000
TextWindow.WriteLine(i+ ". Code")
Program.Delay(10)
EndFor

Make Sense?

Sunday, December 8, 2013 5:26 AM
• This is my maths challenge: WSJ469.

26701 is my record in local (Windows Vista / Intel Core Solo 1.2GHz).

Trial 1 17:09:50 Largest prime [2917] is 26591.
Trial 2 17:10:50 Largest prime [2929] is 26701.
Trial 3 17:11:50 Largest prime [2924] is 26681.
Trial 4 17:12:50 Largest prime [2921] is 26641.
Trial 5 17:13:50 Largest prime [2924] is 26681.
Press any key to continue...

Nonki Takahashi

Sunday, December 8, 2013 8:26 AM
• Hi Joman, re: "what makes it crash on your lappy"

I don't know. However in your sample you're running code in the subroutine MouseMove.

I would avoid this because that subroutine is called every time the mouse moves.

So AFAIK, because that sub has code and program delays in it your OS would be queuing the events up big time. I've read that sometimes these queued up events get deleted by the OS when another event takes place, maybe the isbuttonDown property is the co-culprit.

See if this sample works: QKR009-0

Basically I took all the code out of the MouseMove sub and put it in a While loop. All I put in the MouseMove sub was 1 line of code, mouseMoving = "True"

Sunday, December 8, 2013 9:22 AM
• Maths Challenges    Prime Number WXB989-1

Windows 7   /  Celeron Dual Core CPU T3100 1.9GHz

• Edited by Monday, December 9, 2013 2:37 AM change photo , code Number
Sunday, December 8, 2013 12:07 PM
• For the math challenge (max number of primes in 1 min we have 4520 and 2929 on different PCs).  Any more?
Sunday, December 8, 2013 1:08 PM
• On my 2.3 GHz AMD triple core running XP SP3 I got an average of 4770 primes with WXB989-0

But looking at the program I realized that checking of the division is only necessary up to the square root of the value of a.

I added after the line a=a+2  a line: sqr = Math.SquareRoot(a)

and after For i= 2 to NN

If div[i]>sqr then
i=NN
EndIf

Then the program calculated the 10252th prime as 107741 in 60 sec. A check on internet confirms this value.

Jan [ WhTurner ] The Netherlands

Sunday, December 8, 2013 4:04 PM
• I realized that checking of the division is only necessary up to the square root of the value of a.

I added after the line a=a+2  a line: sqr = Math.SquareRoot(a)

and after For i= 2 to NN

If div[i]>sqr then
i=NN
EndIf

Then the program calculated the 10252th prime as 107741 in 60 sec. A check on internet confirms this value.

Jan [ WhTurner ] The Netherlands

Thank you, WhTurner33.

I changed the code.  WXB989-1   and  above photo.

Monday, December 9, 2013 2:35 AM
• I updated my program: WSJ469-0.

6705th prime (67289) is my new record in local (Windows Vista / Intel Core Solo 1.2GHz).  I also changed increment from 1 to 2 and divisor limit from last prime to squareroot(prime candidate).

Trial 1 15:05:14 Largest prime [6679] is 67043.
Trial 2 15:06:14 Largest prime [6701] is 67247.
Trial 3 15:07:14 Largest prime [6695] is 67189.
Trial 4 15:08:14 Largest prime [6705] is 67289.
Trial 5 15:09:14 Largest prime [6701] is 67247.
Press any key to continue...

Nonki Takahashi

Monday, December 9, 2013 6:45 AM
• I uploaded a sample of community suggestion (by Nonki) 3: JWS691.

Nonki Takahashi

Monday, December 9, 2013 7:28 AM
• This is a sample for community suggestion (by Nonki) 1: PPJ352.

Nonki Takahashi

Monday, December 9, 2013 3:08 PM
• QGX228

My result for the highest prime number using trial division method in up to 1 minute.

On i7 @ 1.73GHz.

I found this method on the blog:      Using Command Line Arguments

I think this code could be further optimised by ditching the multi function call setting the While loop.

e.g. While uptoMinute

Sub OnTick

uptoMinute = "False"

And use the same setting to only store the lastPrime and its index at the If isPrime condition.

A Clock calc either side of the While loop could verify the Timer flags accuracy. Maybe set flag just under a minute to be reasonable.

Also found a good link via msdn search showing the faster sieve method with a good sample in VB. And there's further optimised code samples using the same method.

http://www.codeproject.com/Articles/429694/Finding-prime-numbers

Glad I chose to learn on .NET using SB. Good developer resources and Surface Pro & windows phone look good.)

Tuesday, December 10, 2013 12:15 AM
• This is a sample for community suggestion (by Nonki) 2: SZW634.

Nonki Takahashi

Tuesday, December 10, 2013 4:58 AM
• This is my graphical challenge 2: ﻿﻿QMD035-0.

Nonki Takahashi

Tuesday, December 10, 2013 8:05 AM
• This is an upgraded version of Snowflake: QMD035-2.

Nonki Takahashi

Wednesday, December 11, 2013 10:32 AM
• See if this sample works: QKR009-0

That's awesome! I never thought about putting it that way. Well, I did think of using a While loop.

This works well! A+.

I can write 1000 lines of code in roughly 10 seconds. Here's how:
i = 1

For i = 1 to 1000
TextWindow.WriteLine(i+ ". Code")
Program.Delay(10)
EndFor

Make Sense?

Thursday, December 12, 2013 12:36 AM
• If anyone's doing the Graphics Challenge there's some really good Wiki articles for this:

Thursday, December 12, 2013 1:14 AM
• Here's a better Mirror program than my last using Jibba Jabba's logic:  QKR009-1

Comments at the beginning tell what I added and I commented throughout, but left the majority of JJ's comments.

Thank you, Jibba Jabba, for the better logic :D

I can write 1000 lines of code in roughly 10 seconds. Here's how:
i = 1

For i = 1 to 1000
TextWindow.WriteLine(i+ ". Code")
Program.Delay(10)
EndFor

Make Sense?

Thursday, December 12, 2013 5:41 AM
• Hey Joman

That's awesome. You packed some serious features in there.

Thursday, December 12, 2013 10:28 AM
• My tangential solution to CC Draw Radio Buttons.

It's just one column of Radio buttons where column 1 could be made mutually exclusive to a column 2. But you may wish to check both? or none. TODO. So I don't know if they should be called Radio Buttons?

Had fun, learnt lots about SB (the language I code in) and finally solved the w/around for positioning a hidden GW on the desktop. You need to ref the default GW size.

Also features button detection where you have to click precisely on the button (not near it) and buttons drawn as a circle which can animate the set up a bit.

Runs locally:  VVC400

Thursday, December 12, 2013 8:16 PM
• Jibba Jabba,

Nice tangental solution!

Traditionally, Radio Buttons are used for one choice.  Check Boxes are used for multiple choices.

A check box sample is here.

Nonki Takahashi

Friday, December 13, 2013 12:46 AM
• Haha thanks Jibba Jabba!

I am also thinking of doing half-scale on the diagonals as well as mirroring both V and H axes at once and adding mirroring on both diagonals at once. Maybe all of my options at once. But that might be a bit much.

I can write 1000 lines of code in roughly 10 seconds. Here's how:
i = 1

For i = 1 to 1000
TextWindow.WriteLine(i+ ". Code")
Program.Delay(10)
EndFor

Make Sense?

Friday, December 13, 2013 3:21 AM
• Hey thanks Nonki

Great article and code sample too.

Your right. They've got 'conventions' for using them.

I like your snow flakes. definitely got to try that.

Friday, December 13, 2013 3:35 AM
• lol. I thought it was really good. I like features. They do something.

Friday, December 13, 2013 3:50 AM
• Another update for Graphical Challenge (1): WCR226

A few new features. It'd have been out yesterday but I spent a little too much time playing on it. Plus my sister loves it, so I had to let her play. Lots of wasted time...

Anyway, enjoy the software :P

I can write 1000 lines of code in roughly 10 seconds. Here's how:
i = 1

For i = 1 to 1000
TextWindow.WriteLine(i+ ". Code")
Program.Delay(10)
EndFor

Make Sense?

Friday, December 13, 2013 6:43 PM
• I uploaded my program for graphics challenge 1: GJR357.

Please draw in right half side of the window.

Nonki Takahashi

Tuesday, December 17, 2013 4:01 PM
• Graphical Challenges    SMT641

• Write a program that show a reflection of a picture   (you don't draw)
1.    31 characters shape appear and reflect.   some characters are not asymmetry.
• Edited by Wednesday, December 18, 2013 6:02 PM fixing post
Wednesday, December 18, 2013 2:59 AM
• Above Something wrong.  Strange format.  can't Reply, Edit,Quote,Delete.

*    some characters are not  "symmetry"   is right.
Wednesday, December 18, 2013 3:13 AM
• NaochansON's last post is located in <div id="content"> in this page...
Something was happen in this thread.

Nonki Takahashi

p.s. Oh! this post is also in <div id="content">.

This trouble has been fixed.
Wednesday, December 18, 2013 12:21 PM
• HI all,

The post has been fixed and the error was in the last section of List.

The thread should be working properly now.

Wednesday, December 18, 2013 6:04 PM
• Launchpad, thank you for fix.

Nonki Takahashi

Thursday, December 19, 2013 6:33 AM
• FBB946 is my flickr show.

Best Regards Martin

Thursday, December 19, 2013 4:18 PM
• A possible solution for curriculum challenge 3, import LNP547.  Includes some asynchronous code (parallel Timer thread to download images to a Stack in the background for later use by the scrolling images) which has been discussed a bit in forum questions.

Merry Christmas.

Friday, December 20, 2013 1:51 PM
• RJC750

My final solution to Community Challenge - Radio Buttons.

1. I cloned the MS RB's found http://msdn.microsoft.com/en-us/library/windows/desktop/aa511488.aspx here. It's hard to tell the difference :)
2. I broke the convention of Initialising by setting a default RB. Because some API's support this and also as I wanted to force a User selection. I've seen this done on Govt web forms etc.. Unconventional.
3. I reverse structured the program into an EDP structure and learnt some stuff in the process. Thanks to litdevs article. Fascinating stuff. The structure tells a real story of how the program works.

I still have a bit of a structure issue with this one that can be solved by making my Subroutines carry out more specific tasks, I think.

Thanks Nonki, very educational challenge.

Saturday, December 21, 2013 10:17 PM
• Here's a question on my radio button solution.

Do you think I did the right / reasonable thing by breaking the "should pre-set a button at start" convention? Keeping in mind some API's allow this.

The scenario:

I'm the user, the buttons represent the size and type of pizza. So while placing the order I insist that staff don't complete the order until they confirm with the customer the size & type then select and continue. The program can't continue forward until a selection is made.

I'd rather send the customer nothing than send them the wrong thing.

Saturday, December 21, 2013 10:28 PM
• I have ideas for a future challenge:

1. Draw G clef and F clef.
2. Draw wave figures of chords (e.g. C major, F minor, G 7th).
3. Make a scoreboard for any sports.

Nonki Takahashi

Monday, December 23, 2013 9:12 AM
• You have two groups of radio buttons, one group is for size, another is for thickness of pizza.

Radio buttons are for selection, so by default, one of them should be selected.

And I recommend you grouping (classifying) subroutines.  Specially for this challenge, that's important what subroutines are for radio buttons.  For example, UpdateProgram() is both for the application and for radio buttons.  If you classify a set of radio button subroutines, test them first.  Then code the application using radio button subroutines.

Nonki Takahashi

Monday, December 23, 2013 10:57 AM
• Hi Nonki

Thanks for the fb. Much appreciated and very helpful. I just studied and summarised your solution and really like how you've done the Subroutines for the rb's and the logic.

You're right the convention is "should be 1 selected" at start. I'm still learning on this of which you're being very helpful.

To me a convention and a rule are 2 different things. A rule is you must do or there could be consequences or unable to do any other way. Whereas a convention is a wise and accepted method.

An analogy for this could be what side of the road to drive on is a rule and what side of the footpath to walk on is a convention. A reasonable occasion to break convention might be if there's only 1 of me walking down the footpath and there's 10 American tourists coming the other way. Odds are they'd be walking on the right RHS and me on the left. Collision would be imminent. So it could be reasonable for me to break convention on such occasion. Interestingly (I read) apparently Java API allows non setting of rb's at start.

RE: UpdateProgram()

I'm new to learning program structures. What I was thinking here was structuring the program at the highest level as an EDP. Thereby the main loop would continually check for any changes in the programs state and then notify the UpdateScreen() if any screen changes where needed. What I'm keen to try soon is pausing the main loop and resuming only when an event takes place, including programed events. That's where your WaitStatusChange() subroutine interests me.

Thanks again and happy Xmas to you and all SB people.

Oh, here's the summary of your solution I did: XDH362. (for my education only here)

Monday, December 23, 2013 1:35 PM
• Hello everybody

I've just been trying to apply the tip of classifying the rb subroutines. I can't figure it out for this program. Anyone got a pseudo sample that might clarify.

RJC750

Wednesday, December 25, 2013 7:29 PM
• Hello everybody

I've just been trying to apply the tip of classifying the rb subroutines. I can't figure it out for this program. Anyone got a pseudo sample that might clarify.

RJC750

My pseudo sample  : XJM979-0

Thursday, December 26, 2013 9:08 AM
• Hey thanks Naochan.

I'm currently exploring EDP structure and I'm looking at lots of different stuff, here and abroad. I enjoy and learn from some of the discussions that take place.

Atm I'm very interested in exploring any/if differences b/w the game loop and event loop exist and how they can be explored using SB.

Litdevs article has been a terrific springboard on this for me.

e.g. Most EDP's don't need a maximum frame rate but the structure might benefit from allowing one especially since SB isn't built for asynchronous programming and the dispatch and processing of all events requires a continuous process. But we've got the Timer.

So event happens, program resumes, event processed, program updated, screen gets updated anyway, all updates completed, program paused = more resources available.

I reckon SB is great for trying/learning some of this stuff.

So I sort of see this as a sequential program structure built to take unsequential events.

I'm a bit of a nOOb on this but totally love it.

Thursday, December 26, 2013 5:30 PM
• Hi litdev,

On the chance that you get tired of thinking up new challenges every month you might want to use some/all of these as non-community challenges and could you pls moderate the merits of the critical thinking challenge (but no spell check).

Properties and Operations

Briefly explain the difference between an Operation and a Property. Using the words "has" and "does". - for beginners.

TextWindow Challenge: - for intermediate skill level

Write a program that changes the TextWindow backgroundColor to all the colours available to the TW and display (animate) the current color's name in the TW.title.  To set TW.backgroundColor: 1st set the color then clear the window.

Good Coding Techiniques

Identify/discover the usefulness of using good variables as opposed to just plugging values straight into functions and methods. This is a 1st step toward writing extensible code. May as well briefly define a function and method too.  This article can help:  Small Basic: Programming Tips

GraphicsWindow Challenge: - for beginner gamers

Using your good variables add a box (rectangle) or a ball (ellipse) and continuously move it from one side of the window to the other so that no part of the shape leaves the window but the Shape touches the edge. Use Shapes.Move method and not Shapes.Animate().

You can further advance this (intermediate skill level) by using acceleration so that the shape is moving at its fastest in the centre of the GW and slowest at the edges.

Other Stuff

WebUser & Critical thinking Challenge: - for nOOb critical thinkers

Proove the following equation and show findings.

let a = 5, b = 7, c = 3 and d = 4 then

c(b + 2) / d + a = 12.75

This is something I'm looking at atm: - for critical thinkers

Using other software, like Paint.NET revisit the Simon Game and do the Graphics with the 3D tilt effect. The other software might be useful for getting the positions of the big color buttons. A similar sort of concept of using human movement to get data for complex 3D animations. Not sure here, but I'm doing it.

Monday, December 30, 2013 6:31 PM
• Great suggestions, will use - thanks Jibba Jabba.
Monday, December 30, 2013 6:40 PM
• let a = 5, b = 7, c = 3 and d = 4 then

c(b + 2) / d + a = 12.75

Who can prove that with SmallBasic, as a calculation in my head gives the answer 11.75 ?

Jan [ WhTurner ] The Netherlands

Monday, December 30, 2013 6:53 PM

I couldn't prove it either.

Monday, December 30, 2013 6:57 PM
• I prove it to be an incorrect statement.

Monday, December 30, 2013 6:59 PM
• Since I've been learning to program using SB and this forum I've learnt more than just learning to code in SB.

I think I've been developing skills that are/will be beneficial for a Software Developer, if that's what I choose to pursue. I think that's the very spirit of the Small Basic project.

Critical thinking is one good skill. Here's wikipedias definition of it. http://en.wikipedia.org/wiki/Critical_thinking

Worth a look I reckon.

We had a thread earlier that discussed perquisite skills for programming and some points were raised about creativity, math, science etc. Here's a quoted extract from the aforementioned link on critical thinking:

"..... This process incorporates passion and creativity, but guides it with discipline, practicality and common sense...."

My thoughts are: Cyber space just keeps getting cooler on a large part because of this. It's an evolving inorganic origanically derived self correcting virtual brain. The sooner my last post becomes superseded, the better.

Monday, December 30, 2013 9:05 PM