Web www.avrams.ro

Pseudocode/ Flowchart: Solving Problems - Algorithms Representation«The Polynomial of degree 2»

Theme Formulation

Find an algorithm to solve the generalized equation of polynomial of degree 2:
a·x2+b·x+c=0

Requirement

Write a pseudocode/flowchart to compute the solution for any supplied set of the coefficients {a, b, c}

A Solution to the problem

 We need to supply to the algorithm: a, b, c

A) A pseudocode solution (here used the implementation in Python)

Steps:

# the "math" library must be loaded previously (before any invocation)
# import math
def poly2(a=0, b=0, c=0):
# print a, b, c
# to prevent usage of integral division
# a simple way to convert integer argument passed
# at runtime
a = 1.0 * a
b = 1.0 * b
c = 1.0 * c
if a == 0:
if b != 0:
x = (-1.0) * (c / b)
print("The real solution is: " + str(x))
return x
else:
print("The equation is extremely degenerated")
else:
d = b ** 2 - 4 * a * c
rp = -b / (2 * a)
ip = math.sqrt(abs(d)) / (2 * a)
if d < 0:
x1 = complex(rp,ip)
x2 = complex(rp, -ip)
print("The complex solutions are: \n"+" x1="+str(x1)+"\n x2="+str(x2))
return x1, x2
else:
x1 = rp + ip
x2 = rp - ip
print("The real solutions are: \n"+" x1="+str(x1)+"\n x2="+str(x2))
return x1, x2

The instructions (the lines here) starting with a "#" character are comments. They have the role to explain the code or to invalidate some commands used in the testing phase. The call model of the function is defined by sentence def poly2(a=0,b=0,c=0): in which the word def is a keyword that defines a function/procedure, poly2 is the name given to that by the designer/ programmer, and what follows in parenthesis are called arguments or parameters and are given as a list (elements separated by comma). The constructions of the form variablename=value (a=0,b=0,c=0) used here means that if you do not specifies the value of a variable, for example b, the value 0 will be assigned to that and used for computation. This is called default value. If you skip an argument value at call time, for example poly2(2,,3), which is equivalent to the algebraic expression 2x2+3=0, you must keep the corresponding comma since the arguments are positional (the first value in the list is assigned to the first argument in the call model, the second value in the list is assigned to the second argument in the call model, and so on). The colon character that ends the definition (the def command here) tells to the compiler/interprete that what follows is the instructions bloc associated to (the procedure/function body in that case). To show the position inside of a block of an instruction is used the indentation techniques as for the natural languages. The instructions of the form x=1.0*x uses a programmer trick to enforce x be a memory cell that holds real numbers. That is because the Python used here do not have declartive instructions as you find in other programming languages, this being the solution adopted with the scope to keep this formal language as closed as possible to the natural language English. The first usage gives the datatype. The instructions if implements the decision structure if «c» then true-branch else false-branch, where «c» is a conditional expression that can be evaluated to True or False and allows decide which branch to follow. Here is used the equality operator == (a double equal operator =). This difers of the one used in the natural language "=", but this solution is given to help the compiler/ interpreter to ditinguish easy, when parsing, between the assignment operation (=) and the comparison for equality (==).
The algorithm now is an implementation of the steps as described by the flowchart from point B. The procedure returns to the caller the value of the roots (as real numbers or as complex numbers). What differs here from other used programming languages is that Python manipulates directly the complex values, it means allow to make operations on, to print them using the normal functions you use on numbers [see to the function str()that takes in argument a number does no matter to which set belongs and converts this into a printable string].

B) A flowchart solution

A  flowchart representation of the algorithm for the polynomial of degree 2 can be:

C) Three Implementation Solutions in Visual Basic (VBA from Microsoft Word)

The figure that follows is an excerpt of an image capture of a Word 2007 Document containing command buttons to activate the solution implementation. The command buttons are placed into the document by using Developer, Controls option. The command buttons reacts at the event _Click() to start the associated procedure that implements a specific version. The Properties window in the figure shows the values for the properties of the first command button "Polynomial in 2 - V1". The document is viewed in Design Mode (the yellow button in the toolbar).

Note. You can view the image at its normal size by cliking the righat button of the mouse, while pointing the image, and choosing from the pull down menu displayed "View Image" option.

The figure that follows contains an excerpt of the code associated to the main page by double clicking the "Polynomial in 2 - V1" command button: the first solution realized with dialog functions for the required inputs and for resuls together with the event pocedures that activates the main form used by the other two versions. You can access the code by double clicking a control in the page while in design mode. It contains also the event procedures associated to each command button for the _Click event on.

The code for the first solution is:

In this implementation is used the instruction Dim to realize the rezervation of working variables such as the coefficients a, b , c, the discriminant d, and the cells for the solution x1 and x2 which are declared real numbers in double precision. The variable r is taken as string since used for the return of the InputBox() function which can be what the user types or the value associated to the cancel button. If you use another datatype for the variable in which you store the user answer/ action the pressing of the Cancel button will produces a runtime error. The values for the coefficients are requested to the user by commands like:
r = InputBox("Type a value for A", , "0")
that prompts the user to type an indicated value ("Type the value for ...") which is by default 0 ("0").
a = Val(Trim(r))
The commands of that form eliminates the trailling blanks (Trim) that eventually surround the typed number and then convert the string such prepaired in a real number (Val). The functions used here are intrinsic functions of Visual Basic language (they provided by the language). The remaining instructions implements the algorithm in a similar way as the one described to point A. What is different here (in all Visual Basic implementations) is that this language do not provide direct manipulation of complex number. The function complex implemented here, as a user defined function, has the role to realize a string that diplays a complex number like. It uses the function Round from the Math library, cited like Math.Round(), to restrict the number of the digits displayed for the fraction part,  the function Math.sqr() to compute the square root of a number, and the function Math.Abs() which returns the absolute value of a number passed in argument.
The complex numbers cannot be directely manipulated by the language (in arithmetic operations such as addition, subtractions, divisions etc.).
The result are communicated to the user by intermediate of the function MsgBox(). The strings displayed may contain sequences of function call chr(10) & chr(13) which will include the characters CR (carrige return) and LF (line feed) which allows writting the message on multiple lines as shown in the figure that follows:

The dialog with the user by simple dialog boxes.

The version 2 of the implementation uses a form to input the values for the coefficients and then to request the computation by pressing the Compute (whose name is Poly2) command button. The results are displayed in the same form. The user can finish the processing by pressing the Exit button which closes the forms and give the control onto the Word document.

The version 3 of the implementation, as the previous examples, uses a form for input values and MsgBox() function to deliver the results.

This link allows you to download in your computer the Word document (saved in compatibility mode Word 97-2003 Document) called Poly2.doc containing these implementations. If you want open in Word and test the functionality of these implementations select "Enable this Content" in the Security Alert dialog box displayed when clicking the Options button: