# CSI 1306 BIASU Start to Finish. Background  This is a very simple project. It is not the difficulty that is being presented but the different ways one.

## Presentation on theme: "CSI 1306 BIASU Start to Finish. Background  This is a very simple project. It is not the difficulty that is being presented but the different ways one."— Presentation transcript:

CSI 1306 BIASU Start to Finish

Background  This is a very simple project. It is not the difficulty that is being presented but the different ways one can solve a problem.  The second purpose of this file is to start with a simple program and move all the way to a full Excel function, plus look at all sorts of different ways of solving the exact same problem

Problem  BiasU uses a Biased marking scheme to grade its students  For Female students it is 60% Final + 40% Midterm  For Male students it is 40% Final + 60% Midterm  Note Male and Midterm start with M, and Final and Female start with F, which is the bases for the biased marking

Solution 1 Use Excel to Solve the Problem

Excel Solution  Excel can do this right out of the box  =IF( Condition, True, False) –=IF(?? ="M", 0.60*??+0.40*??, 0.40*??+0.60*??) ?? Are cells on the worksheet In Example Column B, C, and D Double Click on Object below to see

Solution 2 Visual Basic

Evaluate the Problem  What is it they want:? –Numeric Grade, Lets call it NG  What is it that they are providing me? –Gender (Male or Female, or M/F) –Midterm (Mark out of 100) –Final (Mark out of 100)  Lets Give it a Name –How about BiasU

Algorithm Header  Name : BiasU  Given : Gender, Midterm, Final  Results : Grade  Definition : –Grade := BiasU(Gender, Midterm, Final)

Main Body  Step 1 – Get your Givens –Get Gender –Get Midterm –Get Final  Step 2 – Do your Calculations –If Gender is Male Let Grade equal 60% Midterm plus 40% Final –Otherwise (Student is Female) Let Grade equal 40% Midterm plus 60% Final  Step 3 – Give your Results –Give Grade  NOTE This Algorithm is acceptable and very English in language

My First Algorithm  Name : BiasU  Given : Gender, Midterm, Final  Results : Grade  Definition : –Grade := BiasU(Gender, Midterm, Final)  Method –Get Gender –Get Midterm –Get Final –If (Gender = M) Let Grade = 60% Midterm + 40% Final –Else Let Grade = 40% Midterm + 60% Final –Give Grade  NOTE This Algorithm is a bit more mathematical but easier to translate into VB

Round 2 Variants

The Program so far  The first program was very simple, and deliberately so. It is now the starting point.  A good trick for a first time programmer is to take one small part of the problem and solve it then build out from there.  This is the equivalent of taking small bites out of a big meal, no choking.

Variant 1  What if the user puts "Q" for gender –The program now says anyone who is not "M" is automatically a female –This include "m" for males in VB

Solution 1 Excel  Lets add a flag for unwanted values, say -1 since no one can get a grade of -1.  If(??="M", 0.6*??+0.4*??, If(??="F", 0.4*?/+0.6*??, -1))  Note an If within an If, not nice but still "useable"

Variant 2  What if Midterm was out of 75 and Final out of 125.  M = Midterm/75 * 100  F = Final/125 * 100

Excel  We can add columns to do this, but if we wanted to do it as a single cell, what would it look like? –=IF(??="M", 0.6*??/75*100+0.4*??/125*100, IF(??="F", 0.4*??/75*100+0.4*??/125/100, -1))  What if we used Anchored cells like D1, and D2? –=IF(??="M", 0.6*??/\$D\$1*100+0.4*??/\$D\$2*100, IF(??="F", 0.4*??/\$D\$1*100+0.4*??/\$D\$2/100, -1))

As a Worksheet

Complexity  As one can see this is now getting very complex to do as a simple cell formula in Excel  VB on the other hand can keep it very simple –Need two intermediates say M and F –Need two Constants say MMax and FMax MMAX = 75 and FMAX = 125

Changing  What if the faculty wanted to check if there were absents, or incompletes –Absent if Midterm and Final = 0 -> Grade = -2 –Incomplete if Final = 0 -> Grade = -3  What if next year the midterm is out of 90 and the final out of 130  What if the bias is not high enough and they go to a 70/30 split?

Which will be easier to change? =IF(??="M", 0.6*??/75*100+0.4*??/125*100, IF(??="F", 0.4*??/75*100+0.4*??/125/100, - 1)) Const MMax = 75 Const FMax = 125 Gender = InputBox("Please Enter Gender (M/F)") Midterm = InputBox("Please Enter Midterm (0-" & MMax & ")") Final = InputBox("Please Enter Final (0-" & FMax & ")") M = Midterm / MMax * 100 F = Final / FMax * 100 If (Gender = "M") Then Grade = 0.6 * M + 0.4 * F ElseIf (Gender = "F") Then Grade = 0.4 * M + 0.6 * F Else Grade = -1 End If

 Clearly it becomes easier to maintain the Visual basic side.  This does not mean Excel can't be done, just allows the user to have more than one solution to doing the problem  This is the last of the Excel for the moment

Round 3 Multiple Gets

Doing more than one record  As a program it does work, but having to run it over and over again, makes it somewhat impractical.  Although true this is a good method to work through a problem –Solve for any ONE solution –Solve for ALL solutions –Solve for Many Records

Different types of Loops  One can do a finite number of repetitions –Get Number of Records –Let Count = 1 –Loop When (Count < Number of Records) Process a Record Let Count = Count + 1 –Finish Loop

Different types of Loops  What if one does not know (or wish to count) the number of records. Why not just ask the user do you have any more records to process –Loop Process a Record Get Again –Finish Loop When NOT Again

Round 4 Get and Give from Excel

Returning back to Excel  Using the user for input and output would work well if one was using Visual Basic, but these programs are being done under Excel  So why not use Excel worksheet for the data? –Get Gender from Col B –Get Midterm from Col C –Get Final From Col D –Give Grade to Col E –Data Starts in Row 5 –Data Finishes in Row 8 –Data Found on Worksheet Slide 39

Solution 6  Set FirstRow  Set LastRow  Go to Worksheet  Loop For Each Student –Get the Givens For the current Row –Process the Record –Give the Results For the current Row  Finish Loop

Unknown number of students  This system will work, but if the number of students change each year.  Define a key field –Every student has to have a name –Keep processing until there are no more names –

Solution 7  Set FirstRow  Go to Worksheet  Loop Until No More Students –Get the Givens For the current Row –Process the Record –Give the Results For the current Row  Finish Loop

Round 5 More Complicated Problems

Adding a new Result  Say BiasU uses a four point system for their grades. –90-100 -> 4 Points –75-89.9 -> 3 Points –66 – 74.9 -> 2 Points –50 – 65.9 -> 1 Point –0 – 49.9 -> 0 Points

Algorithm  Name: BiasPts  Given: Grade  Result: Point  Interm: None  Def: Point := BiasPts(Grade)  Method –Get Grade –If Grade >= 90 Point = 4 –Else If Grade >= 75 Point = 3 –Else If Grade >= 66 Point = 2 –Else If Grade >= 50 Point = 1 –Else Point = 0 –Give Grade

One Big Solution  One can just add on to the previous solution to add the points to the next column  Loop –Gets –If Gender … –If Grade > 90 … –Give Grade –Give Points  Finish Loop

One Big Solution  Advantages –This is very simply implementation –Builds on what already works  Disadvantage –Not Modular –Not re-usable

Option Explicit 'Written By T. James Sub BiasUv8() 'Version of BiasU found on 'Slide 50 of Powerpoint Dim Gender As String * 1 Dim Midterm As Single Dim Final As Single Dim Grade As Single Dim Point As Integer Dim M As Single Dim F As Single Dim Row As Integer Const MMax = 75 Const FMax = 125 Const FirstRow = 5 Worksheets("Slide 50").Activate Row = FirstRow Do Until (IsEmpty(Cells(Row, 1))) Gender = Cells(Row, 2) Midterm = Cells(Row, 3) Final = Cells(Row, 4) M = Midterm / MMax * 100 F = Final / FMax * 100 If (Gender = "M") Then Grade = 0.6 * M + 0.4 * F ElseIf (Gender = "F") Then Grade = 0.4 * M + 0.6 * F Else Grade = -1 End If If (Grade >= 90) Then Point = 4 ElseIf (Grade >= 75) Then Point = 3 ElseIf (Grade >= 66) Then Point = 2 ElseIf (Grade >= 50) Then Point = 1 Else Point = 0 End If Cells(Row, 5) = Grade Cells(Row, 6) = Point Row = Row + 1 Loop End Sub

Round 6 Use Subroutines

Limitations  Doing everything with one Sub, and one End Sub works, but becomes much harder to manage as the number of tasks increases.  Sub –Do Task1 –Do Task2 –… –Do Task10  End Sub  How do to find, and make modifications to Task6?

Solution 9 - Subroutines  Subroutines take each task and solves it within it own Sub and End Sub  Lets keep Version 7 of the BiasU (rename to V9) and create a PointV9 to handle just the point system

Minour problem  Now the user has to run two programs BiasU and Point. –Why not make a program just to run those two programs  Name : Main  Given : Nothing  Results : Nothing  Def: Main  Method –Call BiasU (Use BiasU) –Call Point (Use Point)

Subroutines  Advantages –Modular –Easier maintenance  Disadvantage –Lots of depenencies –Redundancy

Round 7 Procedures

Difference  Both Subroutines and Procedures are done in VBA as "Subs", the difference is Procedures use parameters for transferring information.  Lets move the duplicate work from the subroutines to main –Worksheets information –Row Information –Gets/Gives from Excel –Constants

MAIN  Name : Main  Given : Nothing  Results : Nothing  Def: Main  Method –Call BiasU (Use BiasU) –Call Point (Use Point)  Name: Main  Given: –Gender, Midterm, Final  Results –Grade, GPoint  Intermed: –Row, FirstRow (Constant) –MMax, FMax(Constants)  Def: –Grade,GPoint := Main(Gender, Midterm, Final)  Method –Set MMax =75 –Set FMax = 125 –Set FirstRow = 5 –Go To Worksheet 64 –Let Row = First Row –Loop Until no More Students Get Gender Get Midterm Get Final Grade := BaisU(Gender, Midter/MMax *100, Final/FMax * 100) GPoint := Point(Grade) Give Grade Give GPoint Let Row = Row + 1 –Finish Loop

Notes  Main has become much more complicated  But each of the Procedures are much easier.  Remember for VB Parameter passing –Given/Gets – ByVal –Results/Gives – ByRef –Wrap new line _

Procedures  Advantages –Portable –Reusable –Easy to Maintain  Disadvantages –Parameter Passing –Still have to Run Excel Program

Round 8 Functions

 We can rewrite Procedures as functions –Remember a function can only return one value, but procedures can return several values  To Convert a Procedure to a Function –Algorithms NO CHANGES –Visual Basic Change the ByRef to a Dim (remove the ByRef) Define the Function as the same Type as the ByRef Add Function Name = your "Dim" variable as last line Change the calling Program to use "=" instead of Call

MAIN  Name : Main  Given : Nothing  Results : Nothing  Def: Main  Method –Call BiasU (Use BiasU) –Call Point (Use Point)  Name: Main  Given: –Gender, Midterm, Final  Results –Grade, GPoint  Intermed: –Row, FirstRow (Constant) –MMax, FMax(Constants)  Def: –Grade,GPoint := Main(Gender, Midterm, Final)  Method –Set MMax =75 –Set FMax = 125 –Set FirstRow = 5 –Go To Worksheet 64 –Let Row = First Row –Loop Until no More Students Get Gender Get Midterm Get Final Grade := BaisU(Gender, Midter/MMax *100, Final/FMax * 100) GPoint := Point(Grade) Give Grade Give GPoint Let Row = Row + 1 –Finish Loop

Sub BiasUvA(ByVal Gender As String, _ ByVal M As Single, _ ByVal F As Single, _ ByRef Grade As Single) If (Gender = "M") Then Grade = 0.6 * M + 0.4 * F ElseIf (Gender = "F") Then Grade = 0.4 * M + 0.6 * F Else Grade = -1 End If End Sub Function BiasU(ByVal Gender As String, _ ByVal M As Single, _ ByVal F As Single) As Single Dim Grade As Single If (Gender = "M") Then Grade = 0.6 * M + 0.4 * F ElseIf (Gender = "F") Then Grade = 0.4 * M + 0.6 * F Else Grade = -1 End If BiasU = Grade End Sub

Sub GPointV9(ByVal Grade As Single, _ ByRef Point As Integer) If (Grade >= 90) Then Point = 4 ElseIf (Grade >= 75) Then Point = 3 ElseIf (Grade >= 66) Then Point = 2 ElseIf (Grade >= 50) Then Point = 1 Else Point = 0 End If End Sub Function GPoint(ByVal Grade As Single) _ As Integer Dim Point As Integer If (Grade >= 90) Then Point = 4 ElseIf (Grade >= 75) Then Point = 3 ElseIf (Grade >= 66) Then Point = 2 ElseIf (Grade >= 50) Then Point = 1 Else Point = 0 End If GPoint = Point End Sub

Option Explicit 'Written By T. James Sub MainVA() 'Version of BiasU found on 'Slide 64 of Powerpoint Dim Gender As String * 1 Dim Midterm As Single Dim Final As Single Dim Grade As Single Dim Point As Integer Dim row As Integer Const MMax = 75 Const FMax = 125 Const FirstRow = 5 Worksheets("Slide 64").Activate row = FirstRow Do Until (IsEmpty(Cells(row, 1))) Gender = Cells(row, 2) Midterm = Cells(row, 3) Final = Cells(row, 4) Call BiasUvA(Gender, Midterm / MMax * 100, _ Final / FMax * 100, Grade) Call GPointVA(Grade, Point) Cells(row, 5) = Grade Cells(row, 6) = Point row = row + 1 Loop End Sub Option Explicit 'Written By T. James Sub Main() 'Version of BiasU found on 'Slide 74 of Powerpoint Dim Gender As String * 1 Dim Midterm As Single Dim Final As Single Dim Grade As Single Dim Point As Integer Dim row As Integer Const MMax = 75 Const FMax = 125 Const FirstRow = 5 Worksheets("Slide 64").Activate row = FirstRow Do Until (IsEmpty(Cells(row, 1))) Gender = Cells(row, 2) Midterm = Cells(row, 3) Final = Cells(row, 4) Grade = BiasU(Gender, Midterm / MMax * 100, _ Final / FMax * 100) Point = GPoint(Grade) Cells(row, 5) = Grade Cells(row, 6) = Point row = row + 1 Loop End Sub

Functions  Advantages –Portable –Reusable –Can be Placed on Excel Worksheet  Disadvantages –Special Paramter

Use Function in Excel  Cell E5 = BiasU(B5, C5/\$E\$1*100,D5/\$E\$2*100)  Cell F5 = GPoint(E5)

Conclusion

 Remember VB, and Excel are not "One or the Other" they work together.  With some Programming knowledge the user has more options –Excel –VB with User Input –VB working with Excel –VB using Subroutines –VB using Procedures –VB Functions –VB Functions on Excel

Download ppt "CSI 1306 BIASU Start to Finish. Background  This is a very simple project. It is not the difficulty that is being presented but the different ways one."

Similar presentations