How to Solve Quadratic Equations in a VSCode

in utopian-io •  7 years ago  (edited)

A computer programmer can use VSCode to create powerful Microsoft Windows applications or as a learning tool as shown by this tutorial on quadratic equations.

What Will I Learn?

How to Solve Quadratic Equations in a Visual Studio Code

Requirements

Visual Studio Code https://code.visualstudio.com

Difficulty

  • Basic

Tutorial Contents

  1. Quadratic Equations and the Quadratic Formula
  2. Using the Quadratic Formula with VBScript
  3. Running the VBScript Code

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 Visual Studio Code. 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



image.png


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



image.png


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



image.png


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



image.png


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.



Posted on Utopian.io - Rewarding Open Source Contributors

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Your contribution cannot be approved because it is not as informative as other contributions. See the Utopian Rules. Contributions need to be informative and descriptive in order to help readers and developers understand them.

  • The tutorial doesn't provide any value to the community

You can contact us on Discord.
[utopian-moderator]

Hey @forkonti, I just gave you a tip for your hard work on moderation. Upvote this comment to support the utopian moderators and increase your future rewards!