Friday, February 21, 2014

Section 5.3: Sub Procedures, Part II




41082:  Write the definition of a function named quadratic that receives three double parameters a,b,c.  If the value of a is 0 then the function prints the message "no solution for a=0" and returns.  If the value of "b squared" - 4ac is negative, then the code prints out the message "no real solutions" and returns.  Otherwise the function prints out the largest solution to the quadratic equation.  The formula for the solutions to this equation can be found here:  Quadratic Equation of Wikipedia.

Function quadratic(a as double, b as double, c as double) as double
Dim x,y as double
If (a = 0) Then
System.Console.Writeline("no solution for a=0")
ElseIf ((b*b - 4*a*c) < 0)
System.Console.Writeline("no real solutions")
Else
x = ((-b + Math.Sqrt(b*b - 4*a*c)) / (2*a))
y = ((-b - Math.Sqrt(b*b - 4*a*c)) / (2*a))
If (x > y) Then
System.Console.Writeline(x)
Else
System.Console.Writeline(y)
End If End If
End Function

1 comment:

  1. Function quadratic(a As Double, b As Double, c As Double) As Double
    Dim x, y As Double
    If a = 0 Then
    System.Console.WriteLine("no solution for a=0")
    ElseIf ((b * b - 4 * a * c) < 0) Then
    System.Console.WriteLine("no real solutions")
    Else
    x = ((-b + Math.Sqrt(b * b - 4 * a * c)) / (2 * a))
    y = ((-b - Math.Sqrt(b * b - 4 * a * c)) / (2 * a))
    If (x > y) Then
    System.Console.WriteLine(x)
    Else
    System.Console.WriteLine(y)
    End If
    End If
    End Function

    ReplyDelete