Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Monday, August 29, 2022

Book Review:Getting Started with Processing

Getting Started with Processing

by Casey Reas and Ben Fry

The Premise

The existence of Processing programming language is a godsend. This is a computer programming language that I tell everybody to learn, and this little computer programming book has a lot to do with it. It is both approachable and logical, something I wish more book writers will follow. Mostly, I'm disappointed. Most computer programming book writers, either imply or outright stated, that the skill of Computer Programming cannot be learned in a book! This is one of the few books that not only stated that it can, but also expected!


Since the intended demographic is for Artists, the technical info is necessarily brief. For further reading, I suggest that you search for books by Dan Shiffman, who also have a YouTube channel. He's a great educator and motivator, and someone you should look up to!



The Beginning

The book begins simply, as is most computer programming book. Between pages 1 and 6, the authors laid down the reason for being, the reason for existence, and the implied purpose of the book. This isn't a book full of heavy technical issues. It's not a complete Reference, or even a complete Tutorials. This book is a good collection of introductory info where you can begin to be productive immediately. The value of this book isn't in what this book includes, but it is in what this book excludes.


After the obligatory installation procedure, the text shows a simple one line program. It draws an ellipse on screen. The fact that the program starts with a graphical interface, where other books starts with "Hello World" program, speaks volumes about the intention of the authors, and I marveled by its efficiency in getting the message across. Not as impressed, though, as the second program shown, which is a simple drawing program using mouse, both as button press and as positions. Interestingly enough, the discussion of variables is still a few chapters away!



Starting to Code

The next few chapters, chapter 5 to 8, explores the concept of computer programming in greater details. Notice that I didn't say in great details. It is an expansion of ideas, yet not comprehensive. What is there, though, is enough to get you started. Again, I was impressed with the examples. This is what efficient coding is all about. To be able to get something that is not only beautiful to look at, but also very quickly coded, that is something to be admired.


About the only thing that I would change is the inclusion of Arrays. The authors, for some reason, decided to present the Array concept in chapter 10. About the only reason that I can think of, is that Processing programming language allows the existence of Array of Objects and therefore shows you the Object creation method before showing you the array. But this is nitpick on my part. I never found the concept of Object Oriented Programming Style to be useful. OO is a good alternative, but I never found the concept of Procedural Programming to be a limitation. 


Looking back into it, I am impressed about the ease the materials were introduced. What other books treat as a big deal, this book presented the materials as a common everyday occurrence. Even the concept of setup() and draw(), which is unique to Processing, is presented as something that naturally occurs. This, of course, is the heart of Event based coding and is a whole paradigm itself. To have such concepts presented naturally, is something that I have never seen done, before or after.



External Materials

In fact, it makes the last chapters inclusion to feel like an afterthought. "Oh, by the way, Processing platform is also capable of doing these things..." is the statement presented. And yes, I find the fact that there is a built-in 3D features to be present is a good deal, but little guidance is spent in how to properly use it. I guess this is a limitation of the format. It's a very slim book, after all. What it successfully presented as non-threatening subject, it fails in comprehensiveness. This is compensated by the fact that the authors includes website addresses, so I don't mind it all that much.



The Conclusion

I think that the book succeeds for the demographic beyond expectation. The materials included are strictly traditional coding practice, but the presentation is such that not only it is fun, but is non-threatening to the lay person. I wish more books would be like that because then the computer programming profession will truly belong to the people. This book is one of the few books that I decided to purchase in paper format, despite the fact that I already have it in digital format. The book is just that good. A must have in my library.




Tuesday, August 14, 2012

Japanese Abacus Soroban 2



'Soroban - Japanese Abacus - SKH302-2
'Harry Hardjono
'August 2012
'
'Apparently, moving shapes is faster than show/hide shapes.

InitLoop:
GraphicsWindow.MouseUp=OnMouse
GraphicsWindow.Title="Small Basic Abacus"
GraphicsWindow.Width = 400
GraphicsWindow.Height=400
GraphicsWindow.FontSize=20
GraphicsWindow.FontName="Courier"
GraphicsWindow.Clear()
GraphicsWindow.Show()
GraphicsWindow.BrushColor="white"
GraphicsWindow.FillRectangle(0,0,GraphicsWindow.Width,GraphicsWindow.Height)
GraphicsWindow.BrushColor="black"

DoBG()
SetBead()
For i=0 To 14
BB[i]=0
SB[i]=0
EndFor
DoBead()
DoDigit()
IBB=BB
ISB=SB

DrawLoop:
BB=IBB
SB=ISB
x= GraphicsWindow.MouseX
y= GraphicsWindow.MouseY

DoMouse()
DoBead()
DoDigit()


Program.Delay(150) 'Wait 150 miliseconds
Goto DrawLoop

Sub DoBG
  GraphicsWindow.BrushColor="Black" 'Draw Frame
  GraphicsWindow.FillRectangle(0,0,399,212)
  GraphicsWindow.BrushColor="White"
  GraphicsWindow.FillRectangle(12,12,375,188)
  GraphicsWindow.BrushColor="Brown"
  For i=0 to 14
  GraphicsWindow.FillRectangle(23+(i*25),12,3,188)
  endfor
  GraphicsWindow.BrushColor="Black" 'Draw Slate & Rod
  GraphicsWindow.FillRectangle(0,62,399,13)

EndSub

Sub SetBead
'BigBead
  For i=0 to 14
  GraphicsWindow.BrushColor=GraphicsWindow.GetRandomColor()
  SSBB[0][i]=Shapes.AddEllipse(25,25)
  Shapes.Move(SSBB[0][i],12+(i*25),12)
  EndFor

'SmallBead
For j=0 To 3
  For i=0 to 14
  GraphicsWindow.BrushColor=GraphicsWindow.GetRandomColor()
  SSSB[j][i]=Shapes.AddEllipse(25,25)
  Shapes.Move(SSSB[j][i],12+(i*25),75+(j*25))
  EndFor
endfor

'Digits
  For i=0 to 14
  GraphicsWindow.BrushColor=GraphicsWindow.GetRandomColor()
  SSDG[i]=Shapes.AddText("0")
  Shapes.Move(SSDG[i],17+(i*25),212)
  EndFor

EndSub

Sub DoDigit
  For i=0 to 14
      Shapes.SetText(SSDG[i],(5*BB[i]+SB[i]))
endFor
EndSub


Sub DoBead
  For i=0 to 14
        Shapes.Move(SSBB[0][i],12+(i*25),12+(BB[i]*25))
EndFor

    For i=0 to 14
     For j=0 to 3
      If (SB[i]<=j) then
        Shapes.Move(SSSB[j][i],12+(i*25),75+((j+1)*25))
        Else
        Shapes.Move(SSSB[j][i],12+(i*25),75+((j)*25))
    endif  
    EndFor
EndFor

EndSub


Sub DoMouse
  map_var="mx1=12;mx2="+x+";mx3=387;my1=0;my3=15" 'Xcoord mapped to 0-14
  map()
  cx=Math.Floor(map_var["my2"])
  If (cx>=0 and cx<=14) then
    If (y>12 And y<62 p="p" then="then">      map_var="mx1=12;mx2="+y+";mx3=62;my1=2;my3=0" 'Ycoord mapped to 1-0
      map()
      cy=Math.Floor(map_var["my2"])
      If (cy>=0 And cy<=1) Then
        BB[cx]=cy
      EndIf
    EndIf

    If (y>75 And y<200 p="p" then="then">      map_var="mx1=75;mx2="+y+";mx3=200;my1=0;my3=5" 'Ycoord mapped to 4-0
      map()
      cy=Math.Floor(map_var["my2"])
      If (cy>=0 And cy<=4) Then
        SB[cx]=cy
      EndIf
    EndIf
  EndIf

EndSub

'----------------------------------------------
'map function
'----------------------------------------------
Sub map
  'x1-x2-x3 y1-y2-y3
  '(x2-x1)/(x3-x1)=(y2-y1)/(y3-y1)
  'y1+(y3-y1)*(x2-x1)/(x3-x1)=y2
  map_var["my2"]=((map_var["my3"]-map_var["my1"])*(map_var["mx2"]-map_var["mx1"])/(map_var["mx3"]-map_var["mx1"]))+map_var["my1"]
EndSub
 
'----------------------------------------------
'event function
'----------------------------------------------
Sub OnMouse
  if (BB<>IBB Or ISB<>SB) Then
    Sound.PlayClick()
  EndIf
  IBB=BB
  ISB=SB
EndSub

 

Tuesday, August 7, 2012

Small Basic Auto Indent (with Subroutine lister)

'Indent - LDH635
'Harry Hardjono
'August 2012
'
CSD=0
indentlevel=0
indenttext="  "
indentarray=""
indentPlus=" sub for while if else elseif "
indentMin=" endsub endfor endwhile endif else elseif "


Filename=Program.Directory+"\indent1.sb"
FileSlurp()
For i=1 To Array.GetItemCount(FileData)
  SkipLeadSpace()
  CatalogSub()
  DoIndent()
  'TextWindow.WriteLine(OutData[i])
endfor


TextWindow.WriteLine("Sub List:")
For i=1 To Array.GetItemCount(SubData)
  TextWindow.Write(SubData[i])
  TextWindow.WriteLine("() ")
EndFor


Sub DoIndent
  StrIn=Text.ConvertToLowerCase(OutData[i])
  SpcInx=text.GetIndexOf(StrIn," ")
  If SpcInx=0 then
    SpcInx=Text.GetLength(StrIn)
  endif 
  StrIn=Text.GetSubText(OutData[i],1,SpcInx)
  StrIn=Text.ConvertToLowerCase(StrIn)
  If (text.GetLength(StrIn) > 1 And Text.GetIndexOf(indentMin,text.Append(" ",StrIn))>0) then
    indentlevel=indentlevel-1
    If indentlevel < 0 then
      indentlevel=0
    endif
    If indentlevel=0 then 
      indentarray[0]=""
    else
      indentarray[indentlevel]=text.Append(indentarray[indentlevel-1],indenttext)
    endif
    'TextWindow.WriteLine("DoIndentMin: "+StrIn)
  endif
  
  TextWindow.WriteLine(indentarray[indentlevel] + OutData[i])
  
  
  If (Text.GetIndexOf(indentPlus,StrIn) > 0) then
    indentlevel=indentlevel+1
    If indentlevel=0 then 
      indentarray[0]=""
    else
      indentarray[indentlevel]=text.Append(indentarray[indentlevel-1],indenttext)
    endif
    'TextWindow.WriteLine("DoIndentPlus: "+StrIn)
  endif
endsub




Sub CatalogSub
  StrIn=Text.ConvertToLowerCase(OutData[i])
  if text.StartsWith(StrIn,"sub ") then
    StrIn=Text.GetSubTextToEnd(StrIn,5)
    SpcInx=text.GetIndexOf(StrIn," ")
    If SpcInx=0 then
      SpcInx=Text.GetLength(StrIn)
    endif 
    StrOut=Text.GetSubText(OutData[i],1,SpcInx+4)
    
    CSD=CSD+1
    SubData[CSD]=StrOut
  endif
endsub


Sub SkipLeadSpace
  StrIn=FileData[i]
  StrOut=""
  For SLS_i=1 to Text.GetLength(StrIn)
    If (Text.GetCharacterCode(Text.GetSubText(StrIn,SLS_i,1))>32) then 'Non-space
      if (StrOut="") then  
        StrOut=Text.GetSubTextToEnd(StrIn,SLS_i)
      EndIf
    EndIf
  endfor  
  OutData[i]=StrOut
endsub




Sub FileSlurp
  'Read a file and assign it to an array
  'Input Filename (string)
  'Output FileData (array)
  FileLength=Text.GetLength(File.ReadContents(Filename))
  FileData=""
  FL=0
  FS_i=1
  While FL
    FileData[FS_i]=File.ReadLine(Filename,FS_i)
    FL=FL+Text.GetLength(FileData[FS_i])+2
    If FileData[FS_i]="" Then 'Fudge for blank lines in file
      FileData[FS_i]=" "
    EndIf
    FS_i=FS_i+1
  Endwhile
EndSub


Monday, August 6, 2012

Petit Computer #1


Petit Computer Journal#1

What is Petit Computer?

If you have been following Nintendo DSi development, then you'd know that SmileBoom has developed a downloadable computer program called Petit Computer. This is a development environment to create your own games on Nintendo DSi using BASIC. The idea is that you will write your own programs, and instead of buying other people software, you just write your own program. It's that easy! No more useless junk!

Why would you want to do that? In the beginning of computer history, there was simply no computer program available. You can either hire expensive professionals, or write your own program. Most people who grew up with early Personal Computer would write their own program. There are old magazines that tells people how to program their computer.

Later in the development of personal computer, softwares have become very complicated. Programming a computer is no longer within the realm of hobbyist computer. I still remember the disbelief that a C development system is 4 Gigabytes. Just how complicated can writing a computer program be? Very, very complicated!

And yet, most people do not need complicated programs. It does not take a rocket scientist to write a simple Blackjack program. Or create a maze. Or word search program. I did Sudoku program in one afternoon.

You don't need ridiculous amount of education in other to write useful program. You don't even need to write one. You can simply read what other people have done, and run their program on your Nintendo DS.

In short, Petit Computer is what Nintendo DSi homebrew, had it officially be done by Nintendo. Notice I said Nintendo DSi. That's because even though it's billed to be compatible with Nintendo 3DS, it doesn't take advantage of that platform. You can of course use Nintendo 3DS to run the program, but it's not necessary. If you don't have the device yet, I suggest the XL version. You won't have to squint at the small screen, and you can actually tap the keys with your fingernails, instead of stylus.

What's the difference between this and other BASIC?

SmileBasic, the BASIC language of Petit Computer, is very different from other BASIC languages. Most people are probably familiar with MS QBasic. Well, the difference between the two is that if you put this command "A=10:A=A/10:PRINT A" into Petit Computer, you'd get a "1". With QBasic, you'd get a "10".

Haha, joking aside, there is quite a big difference with the other BASIC. For one thing, this version of BASIC is very primitive when it comes to program structures. There is only FOR-NEXT loop. There is no such thing as WHILE-WEND, REPEAT-UNTIL, SWITCH-CASE. Rumor has it, you can't even GOTO out of the FOR-NEXT loop because you will corrupt the stack. That is strange. I will need to check for that. I think what happens is that the stack will run out because you cannot nest loops too deep.

Another difference is that SmileBasic is heavily tied into the Nintendo DSi hardware. This means sprites, background pictures, music synthesizers. Very powerful capabilities that is available for your use. You can easily make a Mario level game. Really.

The flip side to it is that if you aspire to make "modern" program such as the ones made by Visual Basic, then you are out of luck. This style of BASIC is old-style. Looking at the keywords of the language, I can't help to think that maybe this version isn't too friendly for beginners. Too much closely resembling Assembly Language/Machine Language. And you know what they say about Machine Language. If you cannot think in terms of Hexadecimal, then you may as well give up.

How hard is it to program a computer?

Paraphrasing an old joke:
Typing the program: $10.
Knowing what to type: $9990.

Computer programming is one of the easiest, if not the easiest job around. The qualification of that is IF YOU KNOW WHAT YOU'RE DOING. Do people know what they're doing? Surprisingly enough, no.

That's actually normal. Unless you're doing routine database programming, you probably will have to dissect the problem enough to come up with a solution. The problem you're solving is unique because if it's a common problem, then other people would have solved it already. Therefore, your problem solving skill must be top-notch.

The good news is, as a beginner, you don't have to be an expert solver. You're just starting out! By definition, other people have trodden the path before you, and you are going through familiar territory. If you get stuck anywhere, a simple question to the appropriate forum will suffice to get you going. There are plenty of tutorials on the Net. I plan to make some myself.

Do you need to be good at Math? No, you don't. I know I'm bucking the convention here, but we're talking about hobbyist computer programming. If you can balance your checkbook, then you can write computer program. If you know how to use spreadsheet program. You definitely have all the skills required. Higher level math is only necessary if you aspire to be a software ENGINEER. For hobbyist level, the most important skill needed is "Show and Tell". Really.


Should I really do this?

Why would you spend your time writing computer program on a very primitive system when you can have the world given to you by the latest and greatest in computer programming technology? How about Python? Ruby? Aren't they so great? Yes, they are, if you want to be productive.

If, however, you want to have fun and make games on Nintendo DSi, then this is the system to use! Not despite it being primitive, but because of it. Remember that old style 8 bit Apple, Atari, Commodore computer are rather primitive, and those computers were very useful. It doesn't take much to write useful programs.

I regularly write programs in Small Basic because it's fun. I will write programs in Smile Basic because it's fun. Remember: computer programming is easy, IF you know what you're doing. So the trick is, to first learn what you need to know in order to write the program, then write the program. By then, it'd be easy.

This is especially true with drag and drop programming. Let's take, for example, Macromedia Director. It's a very powerful platform, to be sure. But do you really learn how to program? Or do you learn to do use the package? Take away Director, and given Basic, can you replicate what you did with Director? Doubt it. Take away Basic, can you replicate what you did with Director? In my case, it took only 2 days, including learning the Director!

I hope I have convinced you to do this. If not, well, there's always Small Basic. :)


Tuesday, July 31, 2012

Small Basic Programming Challenge#1


'Programming Challenge#1
'Microsoft Small Basic
'Harry Hardjono
'July 2012
'

text1="I'm singing in the rain!"
text2="The quick brown fox jumps over the lazy dog."
text3="a SMALL misfortune"

t1="abcdefghijklmnopqrstuvwxyz"
t2="ABCDEFGHIJKLMNOPQRSTUVWXYZ"

Tin=text1
UpperLower()
Tin=text2
UpperLower()
Tin=text3
UpperLower()

Tin1=text3
Tin2=text2
Cin="SMALL"
CharIndex()
If Cout="" then
TextWindow.WriteLine("Not found!")
else
TextWindow.WriteLine(Cout)
endif

Tin1=text3
Tin2=text2
Cin="Small"
CharIndex()
If Cout="" then
TextWindow.WriteLine("Not found!")
else
TextWindow.WriteLine(Cout)
endif

Tin=text1
MakeAlpha()
TextWindow.WriteLine(Tout)
Tin=text2
MakeAlpha()
TextWindow.WriteLine(Tout)
Tin=text3
MakeAlpha()
TextWindow.WriteLine(Tout)

Tin=text1
IsPangram()
TextWindow.WriteLine(Tout)
Tin=text2
IsPangram()
TextWindow.WriteLine(Tout)
Tin=text3
IsPangram()
TextWindow.WriteLine(Tout)


Tin1=text1
Tin2="AEIOU"
StringOut()
TextWindow.WriteLine(Tout)
Tin1=text2
Tin2="aeiou"
StringOut()
TextWindow.WriteLine(Tout)
Tin1=text3
Tin2="qwrtypsdfghjklzxcvbmn"
StringOut()
TextWindow.WriteLine(Tout)


Filename=Program.Directory+"\hangman.txt"
FileSlurp()
For i=1 To Array.GetItemCount(FileData)
TextWindow.WriteLine(FileData[i])
endfor

Sub FileSlurp
'Read a file and assign it to an array
'Input Filename (string)
'Output FileData (array)
FileLength=Text.GetLength(File.ReadContents(Filename))
FileData=""
FL=0
FS_i=1
While FL < filelength
  FileData[FS_i]=File.ReadLine(Filename,FS_i)
  FL=FL+Text.GetLength(FileData[FS_i])+2
  If FileData[FS_i]="" Then 'Fudge for blank lines in file
    FileData[FS_i]=" "
  EndIf
  FS_i=FS_i+1
Endwhile
EndSub



Sub UpperLower
TextWindow.WriteLine(Tin)
Text_Lower()
TextWindow.WriteLine(Tout)
Text_Upper()
TextWindow.WriteLine(Tout)
EndSub


Sub Text_Lower
  'Make Tin string lowercase
  'Input Tin (string)
  'Output Tout (string)
  Tout=""
  For TL_i=1 To Text.GetLength(Tin)
    tchar=text.GetSubText(Tin,TL_i,1)
    tindx=text.GetIndexOf(t2,tchar)
    If tindx>0 Then
      tchar=text.GetSubText(t1,tindx,1)
    EndIf
    Tout=Text.Append(Tout,tchar)
  EndFor
EndSub


Sub Text_Upper
  'Make Tin string uppercase
  'Input Tin (string)
  'Output Tout (string)
  Tout=""
  For TL_i=1 To Text.GetLength(Tin)
    tchar=text.GetSubText(Tin,TL_i,1)
    tindx=text.GetIndexOf(t1,tchar)
    If tindx>0 Then
      tchar=text.GetSubText(t2,tindx,1)
    EndIf
    Tout=Text.Append(Tout,tchar)
  EndFor
EndSub

Sub CharIndex
  'Given 2 strings and a char, find the corresponding char from location t1 at t2
  'return "" if not found. return char/string otherwise
  'Input Tin1 = in index string
  'Input Tin2 = out index string
  'Input Cin = character / string
  'Output Cout = character/string
  tindx=text.GetIndexOf(Tin1,Cin)
  If (tindx=0) Then
    Cout=""
  Else
    Cout=text.GetSubText(Tin2,tindx,text.GetLength(Cin))
  EndIf
EndSub

Sub MakeAlpha
  'Given a string, pick out all alphabet. Store in UPPERCASE
  'Input Tin = string (text)
  'Output Tout = string (alphabets)
  Tout=""
  For MA_i = 1 To Text.GetLength(Tin)
    tchar=text.ConvertToUpperCase(text.GetSubText(Tin,MA_i,1))
    tindx=text.GetIndexOf(t2,tchar)
    If tindx>0 Then
      tindx=text.GetIndexOf(Tout,tchar)
      If tindx=0 Then
        Tout=Text.Append(Tout,tchar)
      EndIf
    EndIf
  EndFor
EndSub


Sub IsPangram
  'Given a string, see if all alphabets is represented
  'Input Tin = string(text)
  'Output Tout = string "TRUE" if yes. "FALSE" if false
  Tin1=text.ConvertToUpperCase(Tin)
  Tout="TRUE"
  For IP_i=1 To Text.GetLength(t2)
    tchar=Text.GetSubText(t2,IP_i,1)
    tindx=text.GetIndexOf(Tin1,tchar)
    If (tindx=0) Then
      Tout="FALSE"
    EndIf
  EndFor
EndSub

Sub CharOut
  'Given 2 strings return string of char found in Tin1, but not in Tin2
  'return string
  'Note: No character upper/lower conversion is done.
  'Input Tin1 = main index string
  'Input Tin2 = sub index string
  'Output Tout = character/string
  Tout=""
  For CO_i=1 To Text.GetLength(Tin1)
    tchar=text.GetSubText(Tin1,CO_i,1)
    tindx=text.GetIndexOf(Tin2,tchar)
    If (tindx=0) Then
      Tout=Text.Append(Tout,tchar)
    EndIf
  EndFor
EndSub

Sub StringOut
  'Given 2 strings return string of char found in Tin1, but not in Tin2
  'return string
  'Note: Convert all char to UPPER
  'Note: Calls CharOut(), MakeAlpha()
  'Input Tin1 = main index string
  'Input Tin2 = sub index string
  'Output Tout = character/string
 
  Tin1=text.ConvertToUpperCase(Tin1)
  Tin2=text.ConvertToUpperCase(Tin2)
  CharOut()
  Tin=Tout
  MakeAlpha()
EndSub

Tuesday, July 24, 2012

Programming Challenge#1


Programming Challenge#1
One Hour Programming Project
July 25, 2012

1. Upper and Lower caps.
given a string:
Make string all caps.
Make string all lower caps.
Example:
a SMALL misfortune (original text)
a small misfortune (lower caps)
A SMALL MISFORTUNE (upper caps)


2. Find location of char/string in another string
given 2 equal length string
find the location of a char in text 1
Find the character in same location in text 2
Example:
a SMALL misfortune (text 1)
The quick brown fox (text 2)
Result for searching A (char)
q
Result for searching SMALL (string)
e qui


3. Alphabets
given a text string (text 1)
and a substring (text 2)
Find:
1. all letters in the text string
2. Determine if text string is pangram (all letters used)
3. all letters in text 1 string, NOT in text 2
Example:
The quick brown fox jumps over the lazy dog. (text 1)
AEIOU (text 2)
Result:
1. THEQUICKBROWNFXJMPSVLAZYDG
2. TRUE
3. THQCKBRWNFXJMPSVLZYDG

Bonus challenge: Load a text file and display the content to the screen.
Extra Challenge: Assigning each line to an array.

Tuesday, May 15, 2012

Small Basic Converter Code

One of April challenges is writing a converter, the kind that converts meter to yard. A lot of people are doing it the hard coding way. I am way to busy to do that. So, I decided to spend one hour on it, and no more! The result is here. Not perfect, but close enough.

There are several design issues:
1. The display for various conversion and the result is rolled into one.
2. You only select the original data, all the possible conversions is done automatically.
3. Range is limited to valid input
4. The data is provided via text. You can modify the text to suit.
5. More importantly, the text also include range of numbers, which the program uses for conversion. The ratio for conversion is calculated automatically. This includes negative numbers, or shifting numbers (i.e. 1-5 into 3-7)
6. I use a hidden Main scale to facilitate ease of conversion.
7. Pay attention to Field[] entries! They defined the data field locations!

These design decisions helped me contained the implementation of this program into one hour.

I did encounter one bug. When copying Choice Loop into NumLoop, I forgot to change the Goto statement, so it went back to Choice Loop. Easily fixed.

Turns out, the algorithm for conversion is robust enough to handle out of range condition, so that I don't have to restrict its input. I could have avoided that bug after all.

It also means, that I don't have to show the range of numbers. It means I can just display the descriptions, and the converted numbers. It would make a cleaner presentation.


'Small Basic Converter - ZLW480
'By Harry Hardjono
'April 2012
'
Init:
MainMin=0
MainNum=0
MainMax=9999999

Field[1]=1 'N
Field[2]=3 'Description
Field[3]=14 'From
Field[4]=18 'Min
Field[5]=28 'To
Field[6]=30 'Max
Field[7]=40 ':

'Data[0]="N DescriptionFrom To :"
Data[1]="1 USD From 0 To 100000 : "
Data[2]="2 GBP From 0 To 158308 : "
Data[3]="3 CAD From 0 To 99962 : "
Data[4]="4 EUR From 0 To 130240 : "
Data[5]="5 AUD From 0 To 103447 : "

TextWindow.WriteLine("Small Basic Converter")

MainLoop:
For i=1 To Array.GetItemCount(Data)
TextWindow.Write(Text.GetSubText(Data[i],1,Text.GetLength(Data[i])))
mx1=MainMin
mx2=MainNum
mx3=MainMax
my1=Text.GetSubText(Data[i],Field[4],Field[5]-Field[4])
my3=Text.GetSubText(Data[i],Field[6],Field[7]-Field[6])
map()
TextWindow.WriteLine(my2)
EndFor
TextWindow.WriteLine(" ")
ChoiceLoop:
TextWindow.Write(("Which data(1-"+Array.GetItemCount(Data))+")?")
Choice=TextWindow.ReadNumber()
If (Choice<1 or Choice>Array.GetItemCount(Data)) then
TextWindow.WriteLine("Out of Range!")
Goto ChoiceLoop
endif
NumLoop:
TextWindow.WriteLine(Text.GetSubText(Data[Choice],Field[4],Field[5]-Field[4])+" to "+Text.GetSubText(Data[Choice],Field[6],Field[7]-Field[6]))
TextWindow.Write("Enter the amount: ")
Num=TextWindow.ReadNumber()
' If (NumText.GetSubText(Data[Choice],Field[6],Field[7]-Field[6])) then
' TextWindow.WriteLine("Out of Range!")
' Goto NumLoop
'endif

mx1=Text.GetSubText(Data[Choice],Field[4],Field[5]-Field[4])
mx2=Num
mx3=Text.GetSubText(Data[Choice],Field[6],Field[7]-Field[6])
my1=MainMin
my3=MainMax
map()
MainNum=my2

Goto MainLoop
Sub map 'map function
'x1-x2-x3 y1-y2-y3
my2=((my3-my1)*(mx2-mx1)/(mx3-mx1))+my1
EndSub