How to Solve Quadratic Equations in a Program

Using VBScript with the Quadratic Formula

© Mark Alexander Bain

Jan 14, 2009
Quadratic Equations and the Quadratic Formula, Mark Alexander Bain
A computer programmer can use VBScript to create powerful Microsoft Windows applications or as a learning tool as shown by this tutorial on quadratic equations.

Quadratic equations are used by school children around the world, and all of those children will (hopefully) know that each quadratic equation can be solved fairly easily by using the quadratic formula.

Quadratic equations are, therefore, a good starting point for anyone learning how to program. When this simple formula is written in a language such as VBScript then the would be programmer can quickly build their confidence in producing a Windows based application.

Quadratic Equations and the Quadratic Formula

A quadratic equation, as every school child knows, is one that takes the general form of:

ax² + bx + c = 0 (where a ≠ 0)

The quadratic equation can be solved by using the quadratic formula:

x = (-b ± √ (b² - 4ac))/2a

Two answers (the equation's roots) will be obtained from the formula depending on whether addition or subtraction is used in the numerator, so that:

root 1 = (-b + √ (b² - 4ac))/2a

and:

root 2 = (-b - √ (b² - 4ac))/2a

The next step is to code the formula into VBScript

Using the Quadratic Formula with VBScript

VBScript is a suitable for learning how to program because:

  • VBScript is installed on every computer that run Microsoft Windows
  • A VBScript application is a text file with a .vbs extension
  • A VBScript application is run by double clicking on it in Microsoft Windows Explorer

All that's needed to start programming is a text editor (such as Notepad). In this case the code will be encapsulated into a subroutine:

sub solve_quad_eq (a, b, c, r1, r2)

The subroutine requires 5 inputs:

  • the equation's a coefficient
  • the equation's b coefficient
  • the equation's c coefficient
  • root 1
  • root 2

The first thing that the subroutine will do is to make sure that the a coefficient is not zero:

if a = 0 then
msgbox "a must be greater than 0"
exit sub
end if

The subroutine must calculate both roots for the equation, but rather than retyping the same formula twice (and then just have + or - in the numerator) the common sections of the formula should be calculated individually:

dim l_numerator : l_numerator = -1 * b
dim r_numerator : r_numerator = sqr (b * b - 4 * a * c)
dim denom : denom = 2 * a

The roots can then be found by using the variables created above:

r1 = (l_numerator + r_numerator) / denom
r2 = (l_numerator - r_numerator) / denom
end sub

All that's left to do is to run the subroutine

Running the VBScript Code

The subroutine will needs some inputs for it to be run correctly. First it will need the quadratic equation coefficients:

dim a : a = 5
dim b : b = -3
dim c : c = -2

As well as the coefficients it needs two inputs that will be updated with the values for the equation roots:

dim r1, r2

Then the subroutine can be run with these variables:

solve_quad_eq a, b, c, r1, r2

and the results (the equation roots) can be displayed to the user:

msgbox r1 & " " & r2

If this code is saved to a file (for example c:\vb\quad_eq.vbs) then it can be run by double clicking on it, and the user will see that the roots for 5x²-3x-2=0 are 1 and -0.4.

Summary

VBScript is a language that's suitable for programmers starting to build applications for Microsoft Windows. With the minimum of effort equations such as the quadratic formula can be coded and written into subroutines.


The copyright of the article How to Solve Quadratic Equations in a Program in Computer Programming Tutorials is owned by Mark Alexander Bain. Permission to republish How to Solve Quadratic Equations in a Program in print or online must be granted by the author in writing.


Quadratic Equations and the Quadratic Formula, Mark Alexander Bain
Solving Quadratic Equations with VBScript, Mark Alexander Bain
     


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo