From d200984eaf70ff9743dbf6409ff65f9399d89f2e Mon Sep 17 00:00:00 2001 From: Andrew Date: Thu, 21 Jan 2021 14:26:56 -0500 Subject: [PATCH] Simpler Quadratic Problems with a-value of 1 --- ProjectClock/MathExpressions.swift | 41 ++++++++++-------------------- 1 file changed, 13 insertions(+), 28 deletions(-) diff --git a/ProjectClock/MathExpressions.swift b/ProjectClock/MathExpressions.swift index d908330..6c11aed 100644 --- a/ProjectClock/MathExpressions.swift +++ b/ProjectClock/MathExpressions.swift @@ -2,7 +2,7 @@ // MathExpressions.swift // ProjectClock // -// Class that will generate a simple math expression +// MathElement import Foundation @@ -118,38 +118,23 @@ let c = MathExpression.random() } } +class quadraticProb{ + //generates the roots + let root1 = Int.random(in: 1...10)//ax^2 + let root2 = Int.random(in: 1...10)//bx -class QuadraticProb{ - - let a = Int.random(in: 1...10)//ax^2 - let b = Int.random(in: 1...10)//bx - let c = Int.random(in: 1...10)//c - var roots = [Int]() - - func getProblem() -> String{ - return "\(a)x^2 + \(b)x + \(c)" + //a value is 1 + let b = root1 + root2 // b value + let c = root1 * root2 // c value + + return "x^2 + \(b)x + \(c)" } - + //finds the roots of the quadratic **NOTE**: the return type is [Int], not a String func getAnswer() -> [Int]{ - let d = Int(pow(Double(b), 2) - 4 * Double(a) * Double(c)) // discriminant - - // if d>0 , equation has two distinct real roots exist. - if d > 0 { - let x1 = Int((-Double(b) + sqrt(Double(d)))/(2*Double(a))) - let x2 = Int((-Double(b) - sqrt(Double(d)))/(2*Double(a))) - roots = [x1, x2] - } - //if d=0, equation has two repeated real roots. - else if d == 0 { - let x = Int(-Double(b)/(2*Double(a))) - roots = [x] - } - // if d<0 equation has two complex roots, but idk how to calculate that by hand, so we'll return nothing - else if d < 0 { - roots = [] - } + let roots = [root1, root2] return roots } + }