Math question within code

This is more of a math question than a code, but still involves the CL.

here is my code:

i=input2

r=randomGenerator()

a=r.int(-10,10)
b=r.int(-10,10)
c=r.int(-10,10)
d=r.int(-10,10)
e=r.int(-10,10)
h=r.int(-10,10)



x=numericValue("\frac{\left(${e}*${c}-${b}*${h}\right)}{\left(${e}*${a}-${b}*${d}\right)}")

y=numericValue("\frac{\left(${d}*${c}-${a}*${h}\right)}{\left(${d}*${b}-${a}*${e}\right)}")



correct= when parseOrderedPair(i.latex).x ="${x}" and parseOrderedPair(i.latex).y="${y}" "yes" otherwise "no"


content: "
`${a}x+${b}y=${c}`
`${d}x+${e}y=${h}`

"

I wrote this with the idea that the two standard form equations would be randomly made, and the coefficients would be integers. What I didn’t predict (but should have) is that the x and y values for the solution are decimals, and sometimes crazy ones.

Any ideas on how to try and rewrite the set up for the values to make the x and y be integers, or at the very least, cleaner decimals?

This is what I have thought of so far. Having every rounding option all the way to 11 places, and it can be paired up either way. If anyone has a better idea, im open ears.

xfloat=numericValue("\frac{\left(${e}${c}-${b}${h}\right)}{\left(${e}${a}-${b}${d}\right)}")
x11="${numericValue("\\operatorname{round}\\left(${xfloat},11\\right)")}"
x10="${numericValue("\\operatorname{round}\\left(${xfloat},10\\right)")}"
x9="${numericValue("\\operatorname{round}\\left(${xfloat},9\\right)")}"
x8="${numericValue("\\operatorname{round}\\left(${xfloat},8\\right)")}"
x7="${numericValue("\\operatorname{round}\\left(${xfloat},7\\right)")}"
x6="${numericValue("\\operatorname{round}\\left(${xfloat},6\\right)")}"
x5="${numericValue("\\operatorname{round}\\left(${xfloat},5\\right)")}"
x4="${numericValue("\\operatorname{round}\\left(${xfloat},4\\right)")}"
x3="${numericValue("\\operatorname{round}\\left(${xfloat},3\\right)")}"
x2="${numericValue("\\operatorname{round}\\left(${xfloat},2\\right)")}"
x1="${numericValue("\\operatorname{round}\\left(${xfloat},1\\right)")}"
x0="${numericValue("\\operatorname{round}\\left(${xfloat},0\\right)")}"


yfloat=numericValue("\frac{\left(${d}*${c}-${a}*${h}\right)}{\left(${d}*${b}-${a}*${e}\right)}")
y11="${numericValue("\\operatorname{round}\\left(${yfloat},11\\right)")}"
y10="${numericValue("\\operatorname{round}\\left(${yfloat},10\\right)")}"
y9="${numericValue("\\operatorname{round}\\left(${yfloat},9\\right)")}"
y8="${numericValue("\\operatorname{round}\\left(${yfloat},8\\right)")}"
y7="${numericValue("\\operatorname{round}\\left(${yfloat},7\\right)")}"
y6="${numericValue("\\operatorname{round}\\left(${yfloat},6\\right)")}"
y5="${numericValue("\\operatorname{round}\\left(${yfloat},5\\right)")}"
y4="${numericValue("\\operatorname{round}\\left(${yfloat},4\\right)")}"
y3="${numericValue("\\operatorname{round}\\left(${yfloat},3\\right)")}"
y2="${numericValue("\\operatorname{round}\\left(${yfloat},2\\right)")}"
y1="${numericValue("\\operatorname{round}\\left(${yfloat},1\\right)")}"
y0="${numericValue("\\operatorname{round}\\left(${yfloat},0\\right)")}"



correct=    when  (   (parseOrderedPair(i.latex).x ="${x11}") 
                   or (parseOrderedPair(i.latex).x ="${x10}")
                   or (parseOrderedPair(i.latex).x ="${x9}")
                   or (parseOrderedPair(i.latex).x ="${x8}")
                   or (parseOrderedPair(i.latex).x ="${x7}")
                   or (parseOrderedPair(i.latex).x ="${x6}")
                   or (parseOrderedPair(i.latex).x ="${x5}")
                   or (parseOrderedPair(i.latex).x ="${x4}")
                   or (parseOrderedPair(i.latex).x ="${x3}")
                   or (parseOrderedPair(i.latex).x ="${x2}")
                   or (parseOrderedPair(i.latex).x ="${x1}")
                   or (parseOrderedPair(i.latex).x ="${x0}"))
                   
            and   (   (parseOrderedPair(i.latex).y ="${y11}") 
                   or (parseOrderedPair(i.latex).y ="${y10}")
                   or (parseOrderedPair(i.latex).y ="${y9}")
                   or (parseOrderedPair(i.latex).y ="${y8}")
                   or (parseOrderedPair(i.latex).y ="${y7}")
                   or (parseOrderedPair(i.latex).y ="${y6}")
                   or (parseOrderedPair(i.latex).y ="${y5}")
                   or (parseOrderedPair(i.latex).y ="${y4}")
                   or (parseOrderedPair(i.latex).y ="${y3}")
                   or (parseOrderedPair(i.latex).y ="${y2}")
                   or (parseOrderedPair(i.latex).y ="${y1}")
                   or (parseOrderedPair(i.latex).y ="${y0}"))

Really complicated! I’ve dealt with this using ExamView a while back. Instead of defining all the constants, make the random defined variables a, x, b, y, d, and e. Then, calculate c and h. You may want/need to tweak your ranges to keep c and h within reasonable ranges.

Standard teacher trap, you want kids to find the solutions, so you’re coding in those terms. Start with answers! :grin:

Genius. I dont know why I didn’t even think of that. I made it way more complicated. Thank you again!

Follow up question, what about when it is in slope intercept form?

I am having a slow time working through this one. This is where I am stuck at. (The code is a bit of a mess)

i=input3

r=randomGenerator()

a=numericValue("(-1)^${r.int(0,1)}*${r.int(3,10)}")
b=numericValue("(-1)^${r.int(0,1)}*${r.int(3,10)}")
xfloat=numericValue("(-1)^${r.int(0,1)}*${r.int(0,8)}")
d=numericValue("(-1)^${r.int(0,1)}*${r.int(3,10)}")
e=numericValue("(-1)^${r.int(0,1)}*${r.int(3,10)}")
yfloat=numericValue("(-1)^${r.int(0,1)}*${r.int(0,8)}")


c1=numericValue("(${a}*${xfloat})+(${b}*${yfloat})")
c="${numericValue("\\operatorname{round}\\left(${c1},1\\right)")}"
h1=numericValue("(${d}*${xfloat})+(${e}*${yfloat})")
h="${numericValue("\\operatorname{round}\\left(${h1},1\\right)")}"



x2="${numericValue("\\operatorname{round}\\left(${xfloat},2\\right)")}"
x1="${numericValue("\\operatorname{round}\\left(${xfloat},1\\right)")}"
x0="${numericValue("\\operatorname{round}\\left(${xfloat},0\\right)")}"


y2="${numericValue("\\operatorname{round}\\left(${yfloat},2\\right)")}"
y1="${numericValue("\\operatorname{round}\\left(${yfloat},1\\right)")}"
y0="${numericValue("\\operatorname{round}\\left(${yfloat},0\\right)")}"



correct=    when  (   (parseOrderedPair(i.latex).x ="${x2}")
                   or (parseOrderedPair(i.latex).x ="${x1}")
                   or (parseOrderedPair(i.latex).x ="${x0}")
                   or (numericValue(parseOrderedPair(i.latex).x) =numericValue("${xfloat}")))
                   
            and   (   (parseOrderedPair(i.latex).y ="${y2}")
                   or (parseOrderedPair(i.latex).y ="${y1}")
                   or (parseOrderedPair(i.latex).y ="${y0}")
                   or (numericValue(parseOrderedPair(i.latex).y) =numericValue("${yfloat}")))


"yes" otherwise "no"

initial1=numericValue("\frac{${c}}{${b}}")
i1="${numericValue("\\operatorname{round}\\left(${initial1},1\\right)")}"
slope1=numericValue("-\frac{${a}}{${b}}")
s1="${numericValue("\\operatorname{round}\\left(${slope1},1\\right)")}"

initial2=numericValue("\frac{${h}}{${e}}")
i2="${numericValue("\\operatorname{round}\\left(${initial2},1\\right)")}"
slope2=numericValue("-\frac{${d}}{${e}}")
s2="${numericValue("\\operatorname{round}\\left(${slope2},1\\right)")}"

content: "Given the following two equations:
`y=${s1}x+${i1}`
`y=${s2}x+${i2}`

Solve the system and find the solution. 

(Round your decimals, or write them as fractions)

${xfloat}
${yfloat}

${correct}"

Got it worked out I think. I set it up like the other one where the b values are what is being solved for based off of the other values. Here is the code:

i=input6

r=randomGenerator()
x=r.int(-7,7)
y=r.int(-7,7)

m1=r.int(-10,10)
b1=numericValue("${y}-(${m1}*${x})")

m2=r.int(-10,10)
b2=numericValue("${y}-(${m2}*${x})")

eq1="y=${m1}x+${b1}"
eq2="y=${m2}x+${b2}"


correct=    when  ((numericValue(parseOrderedPair(i.latex).x) =numericValue("${x}")))
                   
            and   ((numericValue(parseOrderedPair(i.latex).y) =numericValue("${y}")))
            
            "yes" otherwise "no"


content: "
`${eq1}`
`${eq2}`


${correct}
"

I’d use the standard form same as before then convert unless you want integer slopes.

ax + by = c
y = -a/b x - c

It’ll probably take a little finagling to deal with the sign of the slope.

Here is the final form for all that. The code for rounding is mostly unneeded but I kept it in just in case. Works perfect. Thanks!

i=input3


n1="Pizza Barn"
n2="Shake Hut"
n3="Drive and Dine"
n4="Taco Drum"
n5="Captain C's"
n6="Dairy King"
n7="PineappleBees"
n8="McRonalds"
n9="Waffle Hut"
n10="Uhop"


r=randomGenerator()
d1=r.int(1,10)

name = when d1=1 "${n1}"when d1=2 "${n2}"when d1=3 "${n3}"when d1=4 "${n4}"when d1=5 "${n5}" when d1=6 "${n6}"when d1=7 "${n7}"when d1=8 "${n8}"when d1=9 "${n9}"when d1=10 "${n10}"  otherwise ""


q1="pizzas"
q2="milkshakes"
q3="hamburgers"
q4="tacos"
q5="fish sticks"
q6="milkshakes"
q7="steaks"
q8="McSandwhiches"
q9="waffles"
q10="pancakes"

name2 = when d1=1 "${q1}"when d1=2 "${q2}"when d1=3 "${q3}"when d1=4 "${q4}"when d1=5 "${q5}" when d1=6 "${q6}"when d1=7 "${q7}"when d1=8 "${q8}"when d1=9 "${q9}"when d1=10 "${q10}"  otherwise ""



a=numericValue("${r.int(3,10)}")
b=numericValue("${r.int(3,10)}")
xfloat=numericValue("${r.int(0,8)}")
d=numericValue("${r.int(3,10)}")
e=numericValue("${r.int(3,10)}")
yfloat=numericValue("${r.int(0,8)}")


c1=numericValue("(${a}*${xfloat})+(${b}*${yfloat})")
c="${numericValue("\\operatorname{round}\\left(${c1},2\\right)")}"
h1=numericValue("(${d}*${xfloat})+(${e}*${yfloat})")
h="${numericValue("\\operatorname{round}\\left(${h1},2\\right)")}"



x2="${numericValue("\\operatorname{round}\\left(${xfloat},2\\right)")}"
x1="${numericValue("\\operatorname{round}\\left(${xfloat},1\\right)")}"
x0="${numericValue("\\operatorname{round}\\left(${xfloat},0\\right)")}"


y2="${numericValue("\\operatorname{round}\\left(${yfloat},2\\right)")}"
y1="${numericValue("\\operatorname{round}\\left(${yfloat},1\\right)")}"
y0="${numericValue("\\operatorname{round}\\left(${yfloat},0\\right)")}"



correct=    when  (   (parseOrderedPair(i.latex).x ="${x2}")
                   or (parseOrderedPair(i.latex).x ="${x1}")
                   or (parseOrderedPair(i.latex).x ="${x0}")
                   or (numericValue(parseOrderedPair(i.latex).x) =numericValue("${xfloat}")))
                   
            and   (   (parseOrderedPair(i.latex).y ="${y2}")
                   or (parseOrderedPair(i.latex).y ="${y1}")
                   or (parseOrderedPair(i.latex).y ="${y0}")
                   or (numericValue(parseOrderedPair(i.latex).y) =numericValue("${yfloat}")))



"Correct" otherwise "Not there yet, keep working"


content: "The ${name} sells one customer ${a} large ${name2} and ${b} small ${name2} for $${c}. They sell another customer ${d} large ${name2} and ${e} small ${name2} for $${h}. How much does the ${name} charge for each pepperoni pizza and each order of breadsticks? 

Solve the system and find the solution. 




${correct}"






You could clean a lot up (i.e. get rid of all the x’s and y’s) by using this:

correct = when ( numericValue("\round(${parseOrderedPair(i.latex).x})")  
                                   = numericValue("\round(${xfloat})") and
                            numericValue("\round(${parseOrderedPair(i.latex).y})")  
                                   = numericValue("\round(${yfloat})")
...etc
1 Like