Apr 17, 2009

PPP: Basic Math Machine

A Programing Practice Problem designed for the beginner programmer with the means of programming either in console command line, or with a VPL.

I'm wanting a simple application that will take a user's input, consider the input, and output a result. If you remember in about 3rd grade, we were taught how to use "Math Machines" that could take a number, do something to it, and then output a result. What our application will do is very similar in concept.

1) Basic Math Machine: +3

Our application needs to take the user's input, add three to it, and output the result. Save this application as BasicMathMachine_PlusThree.

Input: -4, 0, 3
Expected output:
-4) -1
0) 3
3) 6

2) Basic Math Machine: +3 Loop 3

That would be a start to our masterpiece. Lets learn about a "for loop" next. After the user's input, add a for loop that goes from 0 to 2, counting by 1's. Move the calculation and output steps into the for loop. Save this application as BasicMathMachine_PlusThree_LoopThree.

Input: -4, 0, 3
Expected output:
-4) -1, 2, 5
0) 3, 6, 9
3) 6, 9, 12

3) Basic Math Machine: +x Loop 3

Story time! The government of an unnamed state once funded a business to write an application that would do some major math concerning state taxes. In their documentation for the creation of this application, they specified the tax rate that was current. They failed to realize in time that tax rates often change. Since they didn't ask for that part of the application to be a variable, it was hard coded.

Each time the tax rate changes, they had two options. Commission someone else to write a new application for them, or ask the business to alter it for them. This was unnecessarily expensive, much to the business's enjoyment.

Currently, our "+3" is hard coded into our application. Depending on what that three represents, hard code isn't necessarily bad. If we were calculating the circumference of a circle, for example, we would want to use pi. I'm going out on a limb, but pi isn't going to change in the near future.

Lets say that that three represents the size of shipments of our mysterious product. Business is growing, and we now have other options available for shipments. Modify the previous application so that we may alter the shipment size at run time. Save this application as BasicMathMachine_PlusX_LoopThree.

Input:
x = -4, 0, 3
s = 5

Expected output:
-4) 1, 6, 11
0) 5, 10, 15
3) 8, 13, 18

4) Basic Math Machine: +/-x Loop y

Two more changes before our application is ready to be sold. The first is to un-hard code the number of shipments that occur, allowing the user to select how many times to loop at run time. Save this application as BasicMathMachine_PlusX_LoopY.

5EC) Extra Credit:

We've had a few unhappy customers who wish to ship some of our product back. Alas, we are only able to add to our total sales. Un-hard code the type of math we use, allowing for adding or subtracting the user's imput. Save this application as BasicMathMachine_PlusOrSubtractX_LoopY.

1 comment:

Unknown said...
This comment has been removed by a blog administrator.