#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# Solving random math problems with random mathematical operators.

Posts: 81

I'm working on a program that solves math problems using the four randomly generated numbers AND randomly generated math operators (x, /, +, -). The answer needs to be a calculated number, but so far the answer is an exact copy of the original math problem. For instance, if the math problem is: "10+5/8x2" the answer should be: "11.25" rather than: "10+5/8x2". I can understand why it's doing what it is doing, I just need it to insert the proper math operator in place of the text version. That's the sticking point.

The intent of this program is to drill an elementary-school-aged nephew in basic math concepts by presenting a math problem to solve then displaying the answer once complete. This is a stripped-down version for simplicity.

Any advice, of course, is much appreciated. Thanks.

``````-- OrderOfOperation

supportedOrientations(LANDSCAPE_ANY)
displayMode(STANDARD)

function setup()
print("OrderOfOperation:\nSolves a simple math problem while taking Order-Of-Operation rules into account.\n\nTouch screen for next problem.")

-- Randomly generate four numbers and three operations: multiply, divide, add and subtract
opTypeTbl = {" x "," / "," + "," - "}
first     = math.random(1,10)
op1       = opTypeTbl[math.random(1,4)]
second    = math.random(1,10)
op2       = opTypeTbl[math.random(1,4)]
third     = math.random(1,10)
op3       = opTypeTbl[math.random(1,4)]
fourth    = math.random(1,10)
end

function draw()
background(40, 40, 50)
Lab(first,op1,second,op2,third,op3,fourth)
end

function Lab(first,op1,second,op2,third,op3,fourth)
-- Display Problem and Answer given four random numbers and three random operators.

local first  = first
local op1    = op1
local second = second
local op2    = op2
local third  = third
local op3    = op3
local fourth = fourth

fill(255, 255, 255, 255)
fontSize(40)

local p = "Problem:  "..first..op1..second..op2..third..op3..fourth
text(p, WIDTH/2,HEIGHT/2+100) -- Example problem: "10 + 5 / 8 x 2"

local a = "Answer:  "..first..op1..second..op2..third..op3..fourth
-- Answer: 11.25

text(a,WIDTH/2,HEIGHT/2-100)
end

function touched(t)
-- Calls next problem:
if t.state == ENDED then
restart()
end
end

``````

## Comments

• Posts: 185

@Scotty Hopefully this is what you're looking for:

``````function setup()
local problem = "10+5/8x2"
problem = problem:gsub("x", "*")
local answer = load("return " .. problem)()

print(problem .. " = " .. answer)
end
``````
• Posts: 81

Thank you, @Steppers .

That is exactly what I am looking for. However, there is some mystery in the line that has the "load" statement. I'll have to study up on that and your use of gsub.

If you rerun the modified version you'll see what my goal was. This will be blended into a more involved program.

Thanks again for the simple solution and the quick response.

``````-- Order Of Operation    New and Improved.

supportedOrientations(LANDSCAPE_ANY)
displayMode(STANDARD)

function setup()
print("OrderOfOperation:\n\nSolves a simple math problem while taking Order-Of-Operation rules into account.")

-- Randomly generate four numbers and three operations: multiply, divide, add and subtract
opTypeTbl = {" x "," / "," + "," - "}
first     = math.random(1,10)
op1       = opTypeTbl[math.random(1,4)]
second    = math.random(1,10)
op2       = opTypeTbl[math.random(1,4)]
third     = math.random(1,10)
op3       = opTypeTbl[math.random(1,4)]
fourth    = math.random(1,10)

problemUser = first..op1..second..op2..third..op3..fourth -- This is what the user sees.
problem = problemUser:gsub("x", "*") -- This is what the processor sees.
answer = load("return " .. problem)() -- A bit of magic happend here.
answer = string.format("%.2f",answer) -- Limited to 2 decimal places.

-- Extra output just to illustrate what's going on
print("User sees:\n".. problemUser .. " = " .. answer)
print("Processor sees:\n"..problem .. " = " .. answer)
print("Notice how any multiplication signs are handled.")

end

function draw()
background(40, 40, 50)

Lab(problemUser,answer)
end

function Lab(problemUser,answer)
-- Display Problem and Answer

local p = problemUser
local a = answer

fill(255, 255, 255, 255)
fontSize(40)

p = "Problem = "..p
text(p, WIDTH/2,HEIGHT/2+50)

a = "Answer = "..a
text(a,WIDTH/2,HEIGHT/2-50)

fontSize(14)
text("Touch screen for next problem",WIDTH/2,100)

end

function touched(t)

-- Calls next problem:
if t.state == ENDED then
restart()
end

end

``````
• Mod
Posts: 1,061

The load just takes the string provided, compiles it, and runs it as lua code. The "return" in the one provided makes the expression's value come back as the result of the call to load.

• Posts: 81

@Steppers and @RonJeffries , I learn something new everyday. Thanks you.

Sign In or Register to comment.