Udostępnij za pośrednictwem


Small Basic - The Triangle Area Calculator

The Three Triangles

What: This blog post teaches you how to write interactive programs.

The advantages of using subroutines aren’t very obvious if you started by writing simple programs. In this blog post, you’ll look at an example that uses subroutines to break down a problem into smaller pieces. This program can find the area of all triangles (including equilateral triangles). The different triangles you can make with this program are in Figure 1.

Figure 1: The three triangles you can make with your triangle area program

You can always find the area of a triangle using this simple formula:

In Figure 1, your user enters the base, but the height could either be entered directly by the user or calculated from other parameters of the triangle. As you’ll soon see, you don’t need to understand the details of these formulas in order to write this program!

I Don’t Understand These Formulas!

Imagine if you had to understand the details of how your cell phone works to make a call, or how your DVD player works in order to watch a movie! Luckily, you don't.

Similarly, programmers working on complex projects often don’t need to understand all the details in order to make usable programs. The design team for a large software project might include mathematicians, physicists, and engineers who create very complex math equations that model some part of the system. A programmer or software engineer can then take these equations and write a program to test these models without fully understanding the complicated math behind them.

Similarly, as you write programs, you'll find some that involve the use of special knowledge (like tough math) that has nothing to do with programming. Of course, you’re not expected to know this information; you'll just use it, or in this case, we'll just give it to you (you’re welcome). Remember that you can write the code even if you don't understand the how’s or why’s of the math involved. In other words, you don’t need to understand something in order to use it (or most people wouldn’t be allowed to use a toilet)!

Writing the Code

Figure 2 shows you the program in action. The program starts by displaying a graphical menu (a list of possible choices), and it asks your user to enter his choice. Based on the value he enters, the program calls a different subroutine to collect the needed inputs, and then it computes the area.

Figure 2: The triangle area calculator in action

The program’s made up of four parts: the main program, which gets your user’s choice and directs the flow of control, and three subroutines (each subroutine handles one type of triangle). The main program’s in Listing 1. Write this one out, because the next listings will build on it!

Listing 1: Triangle Area

 1 ' TriangleArea.sb

 2 ' Calculates the area of different triangles

 3

 4 GraphicsWindow.Title  = "Triangle Area Calculator"

 5 TextWindow.Title      = GraphicsWindow.Title

 6 GraphicsWindow.Width  = 480

 7 GraphicsWindow.Height = 360

 8 GraphicsWindow.CanResize = 0

 9 bkgndImg = Program.Directory + "\TriMenu.png"

10 GraphicsWindow.DrawImage(bkgndImg, 0, 0)

11

12 GetChoice:

13 TextWindow.Write("Enter your choice [1-3]: ")

14 choice = TextWindow.ReadNumber()

15

16 If (choice = 1 ) Then

17   Tri1Area()

18 ElseIf (choice = 2 ) Then

19   Tri2Area()

20 ElseIf (choice = 3 ) Then

21   Tri3Area()

22 Else

23   TextWindow.WriteLine("Sorry, that’s not an option.")

24 EndIf

25

26 TextWindow.WriteLine("")

27 Goto GetChoice

Lines 4-10 set up the GraphicsWindow to show the background image (right-click the image and click Save As to save it in your folder directory). This is like what we did in the “Guess My Coordinates” program in the last chapter. Line 13 asks your user to enter her choice. The statement at Line 14 reads her input and saves it in a variable named choice. The program then runs an If/ElseIf ladder (Lines 16-24) to check her choice and to call one of the three subroutines. The Goto statement in Line 27 lets her try a new computation.

The three subroutines are straightforward. Each subroutine asks your user to enter the required values (based on her choice) and then calculates the triangle’s area. Next we’ll show you the details of these subroutines. The subroutines don’t check your user’s inputs (in order to keep them short), but you should always test your user’s inputs to make sure they are valid (like if your user types a negative number for the base, you should nicely tell her that she can’t use a negative number, and then re-display the prompt).

The Tri1Area() subroutine in Listing 2 asks your user to enter the base and height of the triangle and then uses these values to calculate and display the area. Add this subroutine under your main program you wrote in Listing 1.

Listing 2: General Triangle

1 Sub Tri1Area

2   TextWindow.Write("Enter the base: ")

3   b = TextWindow.ReadNumber()

4   TextWindow.Write("Enter the height: ")

5   h = TextWindow.ReadNumber()

6   area = 0.5 * b * h

7   TextWindow.WriteLine("Area = " + area )

8 EndSub

The subroutine prompts your user to enter the base and height and then saves the values in the two variables, b and h (Lines 2-5). It then finds the area (Line 6) and displays it (Line 7).

Next, add the Tri2Area() subroutine from Listing 3 to your program...

Listing 3: Equilateral Triangle

1 Sub Tri2Area

2   TextWindow.Write("Enter the side-length: ")

3   b = TextWindow.ReadNumber()

4   h = Math.SquareRoot(0.75) * b

5   area = 0.5 * b * h

6   TextWindow.WriteLine("Area = " + area )

7 EndSub

The subroutine asks your user to enter the side-length of the equilateral triangle (Line 2). It then uses this input to find the height (Line 4). Finally, it calculates the area (Line 5) and displays it (Line 6).

The last subroutine you’ll add to your program is in Listing 4.

Listing 4: Isosceles Triangle

1 Sub Tri3Area

2   TextWindow.Write("Enter the base: ")

3   b = TextWindow.ReadNumber()

4   TextWindow.Write("Enter the side-length: ")

5   s = TextWindow.ReadNumber()

6   h = Math.SquareRoot( (s * s) - ( (b/2) * (b/2) ) )

7   area = 0.5 * b * h

8   TextWindow.WriteLine("Area = " + area )

9 EndSub

It asks your user to enter the base and side-length of the isosceles triangle (Lines 2-5), and it uses these values to find the height (Line 6). The area’s then calculated and displayed (Lines 7-8), like before.

The example in this section showed you how to build a large program in smaller pieces, where each piece does something else. The example also showed you how to write a menu-driven program that does different things, depending on what your user chooses.

SELF-CHECK

Now, can you build a similar program that calculates the perimeter of a square, rectangle, or circle? Here is an example menu (right-click and Save As the image to use it in your program):

Write the program to have it read your user’s choice, call the right subroutine to get the needed inputs, compute the perimeter, and then display the perimeter of the selected shape.

You can continue the next lesson, which will teach you about nesting your subroutines:

Small Basic - Nesting Subroutines

Learn more about Small Basic: https://blogs.msdn.com/b/smallbasic/

Small and Basically yours,

   - Majed Marji & Ninja Ed

Comments