I have created a function in Swift to solve and give the solutions to a quadratic function. I don't know how to adapt my function so that it will give the imaginary solutions instead of printing, "There are no real solutions."
I am relatively new to programming and could use some help. Here is my code:
func quadraticFormulaSolver(variableA a: Double, variableB b: Double, variableC c: Double) -> (Double, Double) {
let firstSolution: Double = (-b + sqrt((b * b) + (-4.0 * a * c))) / 2.0
let secondSolution: Double = (-b - sqrt((b * b) + (-4.0 * a * c))) / 2.0
let checkSolution: Double = sqrt((b * b) + (-4.0 * a * c))
if checkSolution > 0 {
print("There are two real solutions and they are \(firstSolution) and \(secondSolution)")
return(firstSolution, secondSolution) }
guard firstSolution != 0.0 else {
print("There is one real solution and it is \(firstSolution)")
return(firstSolution, secondSolution) }
guard checkSolution < 0 else {
print("There are no real solutions")
return(firstSolution, secondSolution) }
return(firstSolution, secondSolution)
}