Free Software Tool which solves a first order equation: ax + b = c
Free Software Tool which solves a first order equation:
ax+ b = c
This hub provides a program for math students working with a first order algebraic equation. The user is prompted for coefficients a, b and c, while the software solves for unknown x. You will need Microsoft Excel. We will go through how to install the program here. It will involve copying and pasting my code into Excel. Open up Excel, save the new file as “1st order.xlsm”. In newer versions of Windows, you will save as type “Excel Macro-Enabled Workbook”. The workbook/spreadsheet should contain three sheets: “Sheet1”, “Sheet2”, and ‘Sheet3”. Right click on the “Sheet3” tab at bottom, and delete it. Do the same for “Sheet2”. “Sheet1” remains. It will be a spreadsheet with embedded code to solve the equation. Our program will look for the name “Sheet1”, so do not change it. You will need access to the Developers tab in Excel. To make it visible in the new spreadsheet, do as follows:
1. File --> Options (click)
2. Customize Ribbon (click)
3. Check the “Developer” box and click “OK” at bottom.
4. You will now see “Developer” tab. Click it.
5. Section dealing with macros and VBA (Visual Basic) is now visible at far left.
We will create a macro, into which we will soon paste some code. Over on the left, find and click the tab “Record Macro”. A box pops up. It has been given a default name, such as “Macro1”. Just leave it as is. There will also be a shortcut key entry, make it “ctrl a”. Click OK. At this point, do not press any keys or perform random mouse clicks, or you will actually start recording keystrokes/commands in the macro. Rather, immediately click the “stop recording” tab, located in same place where “Record Macro” was. We have created a blank routine into which we will now place some code. Click on “Macros” tab and you will see the blank macro we just created. Click “Edit”. The macro will open up in a window, see picture in the diagram. Place the blinking cursor as shown, then copy and paste in this code:
On Error GoTo errorhandle
Dim ast As String, a As Single, bst As String, b As Single, cst As String, c As Single
Dim eqnstr As String, xdecimal As Single, xfractionstr As String, decsolnstr As String, num As Single
Dim lenast As Single, lenbst As Single, lencst As Single
Dim rtast As String, rtbst As String, rtcst As String
Dim lfast As String, lfbst As String, lfcst As String
Dim slasha As Single, slashb As Single, slashc As Single, endstr As String
Dim counta As Single, countb As Single, countc As Single, searchstr As String
Dim x As Single
ast = InputBox("enter coefficient 'a' for ax + b = c. Include sign if negative," _
& "do not use commas or spaces, fractions are OK: 5, -5, 2/15, -2/15....")
bst = InputBox("enter coefficient 'b' for ax + b = c. Include sign if negative," _
& "do not use commas or spaces, fractions are OK: 5, -5, 2/15, -2/15....")
cst = InputBox("enter coefficient 'c' for ax + b = c. Include sign if negative," _
& "do not use commas or spaces, fractions are OK: 5, -5, 2/15, -2/15....")
ast = Trim(ast)
bst = Trim(bst)
cst = Trim(cst)
lenast = Len(ast)
lenbst = Len(bst)
lencst = Len(cst)
If InStr(ast, "/") <> 0 Then
slasha = InStr(ast, "/")
lfast = Left(ast, (slasha - 1))
rtast = Right(ast, (lenast - slasha))
a = Val(lfast) / Val(rtast)
Else
a = Val(ast)
rtast = "1"
End If
If InStr(bst, "/") <> 0 Then
slashb = InStr(bst, "/")
lfbst = Left(bst, (slashb - 1))
rtbst = Right(bst, (lenbst - slashb))
b = Val(lfbst) / Val(rtbst)
Else
b = Val(bst)
rtbst = "1"
End If
If InStr(cst, "/") <> 0 Then
slashc = InStr(cst, "/")
lfcst = Left(cst, (slashc - 1))
rtcst = Right(cst, (lencst - slashc))
c = Val(lfcst) / Val(rtcst)
Else
c = Val(cst)
rtcst = "1"
End If
If b > 0 Then eqnstr = ast & "x + " & bst & " = " & cst
If b < 0 Then eqnstr = ast & "x " & bst & " = " & cst
If b = 0 Then eqnstr = ast & "x " & " = " & cst
num = c - b
xdecimal = num / a
decsolnstr = "x = " & xdecimal
a = a * Val(rtast) * Val(rtbst) * Val(rtcst)
b = b * Val(rtast) * Val(rtbst) * Val(rtcst)
c = c * Val(rtast) * Val(rtbst) * Val(rtcst)
x = 0
counta = 0
ast = Str(a)
Do
searchstr = Mid(ast, (Len(ast) - x), 1)
x = x + 1
If searchstr = "0" Then
counta = counta + 1
Else
Exit Do
End If
Loop
x = 0
countb = 0
bst = Str(b)
Do
searchstr = Mid(bst, (Len(bst) - x), 1)
x = x + 1
If searchstr = "0" Then
countb = countb + 1
Else
Exit Do
End If
Loop
If countb < counta Then counta = countb
x = 0
countc = 0
cst = Str(c)
Do
searchstr = Mid(cst, (Len(cst) - x), 1)
x = x + 1
If searchstr = "0" Then
countc = countc + 1
Else
Exit Do
End If
Loop
If countc < counta Then counta = countc
ast = Left(ast, Len(ast) - counta)
bst = Left(bst, Len(bst) - counta)
cst = Left(cst, Len(cst) - counta)
ast = Trim(ast)
bst = Trim(bst)
cst = Trim(cst)
a = Val(ast)
b = Val(bst)
c = Val(cst)
num = c - b
If (num < 0 And a < 0 Or num > 0 And a < 0) Then xfractionstr = "x = " & -num & "/" & -a
If (num < 0 And a > 0 Or num > 0 And a > 0) Then xfractionstr = "x = " & num & "/" & a
If (num = 0) Then xfractionstr = "0"
Sheets("Sheet1").Activate
Range("A1").Select
ActiveCell.Value = "equation"
Range("B1").Select
ActiveCell.Value = "decimal solution for 'x'"
Range("A2").Select
ActiveCell.Value = eqnstr
Range("B2").Select
ActiveCell.Value = decsolnstr
Range("C1").Select
ActiveCell.Value = "fractional solution for 'x'"
Range("C2").Select
ActiveCell.Value = xfractionstr
Range("C3").Select
ActiveCell.Value = "may need to simplify fraction"
Range("A1").Select
Range(Selection, "C3").Select
Selection.Columns.AutoFit
ActiveWindow.Zoom = True
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Range("C4").Select
Exit Sub
errorhandle:
MsgBox _
("There was an error. Most likely with coefficients you entered. Try again."), vbOKOnly
When you paste the code in, it is likely there will be two areas in red indicating errors in the transfer (one area near top of code, one at bottom). They can be corrected (see pictures) by placing the mouse cursor exactly flush with the “dash” at line end and hitting “delete key”. The red type will become black, indicating it is now correct.
Close the program box and re-save the Excel workbook, keeping it open. Hit “ctrl a” and the program will run. That’s it, hope it works well for you.