Download presentation

Presentation is loading. Please wait.

Published byZachery Trudgeon Modified over 3 years ago

1
CSI 1306 BIASU Start to Finish

2
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

3
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

4
Solution 1 Use Excel to Solve the Problem

5
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

6
Solution 2 Visual Basic

7
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

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

9
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

10
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

11
My First Program 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 Option Explicit 'Written By T. James Sub BiasUv1() 'Version of BiasU found on 'Slide 10 of Powerpoint Dim Gender As String * 1 Dim Midterm As Single Dim Final As Single Dim Grade As Single Gender = InputBox("Please Enter Gender (M/F)") Midterm = InputBox("Please Enter Midterm (0-100)") Final = InputBox("Please Enter Final (0-100)") If (Gender = "M") Then Grade = 0.6 * Midterm + 0.4 * Final Else Grade = 0.4 * Midterm + 0.6 * Final End If MsgBox ("The Grade is " & Grade) End Sub

12
Round 2 Variants

13
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.

14
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

15
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"

16
Solution 2 VB Name : BiasUv1 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 Name : BiasUv2 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 If (Gender = F) Let Grade = 40% Midterm + 60% Final –Else Let Grade = -1 –Give Grade Same can be done in VB as an Else If. This layout is easier to work with

17
Translated in Visual Basic Option Explicit 'Written By T. James Sub BiasUv2() 'Version of BiasU found on 'Slide 17 of Powerpoint Dim Gender As String * 1 Dim Midterm As Single Dim Final As Single Dim Grade As Single Gender = InputBox("Please Enter Gender (M/F)") Midterm = InputBox("Please Enter Midterm (0-100)") Final = InputBox("Please Enter Final (0-100)") If (Gender = "M") Then Grade = 0.6 * Midterm + 0.4 * Final ElseIf (Gender = "F") Then Grade = 0.4 * Midterm + 0.6 * Final Else Grade = -1 End If MsgBox ("The Grade is " & Grade) End Sub Name : BiasUv2 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 If (Gender = F) Let Grade = 40% Midterm + 60% Final –Else Let Grade = -1 –Give Grade

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

19
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))

20
As a Worksheet

21
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

22
Solution 3 VB Name : BiasUv2 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 If (Gender = F) Let Grade = 40% Midterm + 60% Final –Else Let Grade = -1 –Give Grade Name : BiasUv3 Given : Gender, Midterm, Final Results : Grade Intermediates M, F, MMax (Constant), FMax (Constant) Definition : –Grade := BiasU(Gender, Midterm, Final) Method –Set MMax = 75 –Set FMax = 125 –Get Gender –Get Midterm –Get Final –Let M = Midterm/MMax * 100 –Let F = Final /FMax * 100 –If (Gender = M) Let Grade = 60% M + 40% F –Else If (Gender = F) Let Grade = 40% M + 60% F –Else Let Grade = -1 –Give Grade

23
Solution 3 VB Name : BiasUv3 Given : Gender, Midterm, Final Results : Grade Intermediates M, F, MMax (Constant), FMax (Constant) Definition : –Grade := BiasU(Gender, Midterm, Final) Method –Set MMax = 75 –Set FMax = 125 –Get Gender –Get Midterm –Get Final –Let M = Midterm/MMax * 100 –Let F = Final /FMax * 100 –If (Gender = M) Let Grade = 60% M + 40% F –Else If (Gender = F) Let Grade = 40% M + 60% F –Else Let Grade = -1 –Give Grade Option Explicit 'Written By T. James Sub BiasUv3() 'Version of BiasU found on 'Slide 23 of Powerpoint Dim Gender As String * 1 Dim Midterm As Single Dim Final As Single Dim Grade As Single Dim M As Single Dim F As Single 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 MsgBox ("The Grade is " & Grade) End Sub

24
Compare What about future years?

25
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?

26
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

27
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

28
Round 3 Multiple Gets

29
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

30
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

31
Solution 4 Name : BiasUv3 Given : Gender, Midterm, Final Results : Grade Intermediates M, F, MMax (Constant), FMax (Constant) Definition : –Grade := BiasU(Gender, Midterm, Final) Method –Set MMax = 75 –Set FMax = 125 –Get Gender –Get Midterm –Get Final –Let M = Midterm/MMax * 100 –Let F = Final /FMax * 100 –If (Gender = M) Let Grade = 60% M + 40% F –Else If (Gender = F) Let Grade = 40% M + 60% F –Else Let Grade = -1 –Give Grade Name : BiasUv4 Given : Gender, Midterm, Final Results : Grade Intermediates M, F, MMax (Constant), FMax (Constant), Count, NRec Definition : –Grade := BiasU(Gender, Midterm, Final) Method –Set MMax = 75 –Set FMax = 125 –Let Count = 1 –Get NRec –Loop When (Count <= NRec) Get Gender Get Midterm Get Final Let M = Midterm/MMax * 100 Let F = Final /FMax * 100 If (Gender = M) –Let Grade = 60% M + 40% F Else If (Gender = F) –Let Grade = 40% M + 60% F Else –Let Grade = -1 Give Grade Let Count = Count + 1 –Finish Loop

32
Solution 4 Name : BiasUv4 Given : Gender, Midterm, Final Results : Grade Intermediates M, F, MMax (Constant), FMax (Constant), Count, NRec Definition : –Grade := BiasU(Gender, Midterm, Final) Method –Set MMax = 75 –Set FMax = 125 –Let Count = 1 –Get NRec –Loop When (Count <= NRec) Get Gender Get Midterm Get Final Let M = Midterm/MMax * 100 Let F = Final /FMax * 100 If (Gender = M) –Let Grade = 60% M + 40% F Else If (Gender = F) –Let Grade = 40% M + 60% F Else –Let Grade = -1 Give Grade Let Count = Count + 1 –Finish Loop Option Explicit 'Written By T. James Sub BiasUv3() 'Version of BiasU found on 'Slide 32 of Powerpoint Dim Gender As String * 1 Dim Midterm As Single Dim Final As Single Dim Grade As Single Dim M As Single Dim F As Single Dim Count As Integer Dim NRec As Integer Const MMax = 75 Const FMax = 125 Count = 1 NRec = InputBox("Please Enter Number of Records") Do While (Count <= NRec) 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 MsgBox ("The Grade is " & Grade) Count = Count + 1 Loop End Sub

33
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

34
Solution 5 Name : BiasUv3 Given : Gender, Midterm, Final Results : Grade Intermediates M, F, MMax (Constant), FMax (Constant) Definition : –Grade := BiasU(Gender, Midterm, Final) Method –Set MMax = 75 –Set FMax = 125 –Get Gender –Get Midterm –Get Final –Let M = Midterm/MMax * 100 –Let F = Final /FMax * 100 –If (Gender = M) Let Grade = 60% M + 40% F –Else If (Gender = F) Let Grade = 40% M + 60% F –Else Let Grade = -1 –Give Grade Name : BiasUv5 Given : Gender, Midterm, Final Results : Grade Intermediates M, F, MMax (Constant), FMax (Constant), Again Definition : –Grade := BiasU(Gender, Midterm, Final) Method –Set MMax = 75 –Set FMax = 125 –Loop Get Gender Get Midterm Get Final Let M = Midterm/MMax * 100 Let F = Final /FMax * 100 If (Gender = M) –Let Grade = 60% M + 40% F Else If (Gender = F) –Let Grade = 40% M + 60% F Else –Let Grade = -1 Give Grade Get Again –Finish Loop When Not Again

35
Solution 5 Name : BiasUv5 Given : Gender, Midterm, Final Results : Grade Intermediates M, F, MMax (Constant), FMax (Constant), Again Definition : –Grade := BiasU(Gender, Midterm, Final) Method –Set MMax = 75 –Set FMax = 125 –Loop Get Gender Get Midterm Get Final Let M = Midterm/MMax * 100 Let F = Final /FMax * 100 If (Gender = M) –Let Grade = 60% M + 40% F Else If (Gender = F) –Let Grade = 40% M + 60% F Else –Let Grade = -1 Give Grade Get Again –Finish Loop When Not Again Option Explicit 'Written By T. James Sub BiasUv5() 'Version of BiasU found on 'Slide 35 of Powerpoint Dim Gender As String * 1 Dim Midterm As Single Dim Final As Single Dim Grade As Single Dim M As Single Dim F As Single Dim Again As Integer '6 for Yes, 7 for No Const MMax = 75 Const FMax = 125 Do 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 MsgBox ("The Grade is " & Grade) Again = MsgBox("Again", 4) Loop Until (Again = 7) End Sub

36
Round 4 Get and Give from Excel

37
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

38
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

39
Solution 6 Name : BiasUv3 Given : Gender, Midterm, Final Results : Grade Intermediates M, F, MMax (Constant), FMax (Constant) Definition : –Grade := BiasU(Gender, Midterm, Final) Method –Set MMax = 75 –Set FMax = 125 –Get Gender –Get Midterm –Get Final –Let M = Midterm/MMax * 100 –Let F = Final /FMax * 100 –If (Gender = M) Let Grade = 60% M + 40% F –Else If (Gender = F) Let Grade = 40% M + 60% F –Else Let Grade = -1 –Give Grade Name : BiasUv6 Given : Gender, Midterm, Final Results : Grade Intermediates M, F, MMax (Constant), FMax (Constant), Row, FirstRow (Constant), LastRow (Constant) Definition : –Grade := BiasU(Gender, Midterm, Final) Method –Set MMax = 75 –Set FMax = 125 –Set FirstRow = 5 –Set LastRow = 8 –Go to Worksheet Slide 40 –Loop For each Student Get Gender Get Midterm Get Final Let M = Midterm/MMax * 100 Let F = Final /FMax * 100 If (Gender = M) –Let Grade = 60% M + 40% F Else If (Gender = F) –Let Grade = 40% M + 60% F Else –Let Grade = -1 Give Grade –Finish Loop

40
Name : BiasUv6 Given : Gender, Midterm, Final Results : Grade Intermediates M, F, MMax (Constant), FMax (Constant), Row, FirstRow (Constant), LastRow (Constant) Definition : –Grade := BiasU(Gender, Midterm, Final) Method –Set MMax = 75 –Set FMax = 125 –Set FirstRow = 5 –Set LastRow = 8 –Go to Worksheet Slide 39 –Loop For each Student Get Gender Get Midterm Get Final Let M = Midterm/MMax * 100 Let F = Final /FMax * 100 If (Gender = M) –Let Grade = 60% M + 40% F Else If (Gender = F) –Let Grade = 40% M + 60% F Else –Let Grade = -1 Give Grade –Finish Loop Option Explicit 'Written By T. James Sub BiasUv6() 'Version of BiasU found on 'Slide 40 of Powerpoint Dim Gender As String * 1 Dim Midterm As Single Dim Final As Single Dim Grade As Single Dim M As Single Dim F As Single Dim Row As Integer Const MMax = 75 Const FMax = 125 Const FirstRow = 5 Const LastRow = 8 Worksheets("Slide 40").Activate For Row = FirstRow To LastRow 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 Cells(Row, 5) = Grade Next Row End Sub

41
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 –

42
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

43
Solution 6 Name : BiasUv3 Given : Gender, Midterm, Final Results : Grade Intermediates M, F, MMax (Constant), FMax (Constant) Definition : –Grade := BiasU(Gender, Midterm, Final) Method –Set MMax = 75 –Set FMax = 125 –Get Gender –Get Midterm –Get Final –Let M = Midterm/MMax * 100 –Let F = Final /FMax * 100 –If (Gender = M) Let Grade = 60% M + 40% F –Else If (Gender = F) Let Grade = 40% M + 60% F –Else Let Grade = -1 –Give Grade Name : BiasUv7 Given : Gender, Midterm, Final Results : Grade Intermediates M, F, MMax (Constant), FMax (Constant), Row, FirstRow (Constant) Definition : –Grade := BiasU(Gender, Midterm, Final) Method –Set MMax = 75 –Set FMax = 125 –Set FirstRow = 5 –Go to Worksheet Slide 44 –Let Row = FirstRow –Loop Until No More Students Get Gender Get Midterm Get Final Let M = Midterm/MMax * 100 Let F = Final /FMax * 100 If (Gender = M) –Let Grade = 60% M + 40% F Else If (Gender = F) –Let Grade = 40% M + 60% F Else –Let Grade = -1 Give Grade Let Row = Row + 1 –Finish Loop

44
Name : BiasUv7 Given : Gender, Midterm, Final Results : Grade Intermediates M, F, MMax (Constant), FMax (Constant), Row, FirstRow (Constant) Definition : –Grade := BiasU(Gender, Midterm, Final) Method –Set MMax = 75 –Set FMax = 125 –Set FirstRow = 5 –Go to Worksheet Slide 44 –Let Row = FirstRow –Loop Until No More Students Get Gender Get Midterm Get Final Let M = Midterm/MMax * 100 Let F = Final /FMax * 100 If (Gender = M) –Let Grade = 60% M + 40% F Else If (Gender = F) –Let Grade = 40% M + 60% F Else –Let Grade = -1 Give Grade Let Row = Row + 1 –Finish Loop Option Explicit 'Written By T. James Sub BiasUv7() 'Version of BiasU found on 'Slide 44 of Powerpoint Dim Gender As String * 1 Dim Midterm As Single Dim Final As Single Dim Grade As Single Dim M As Single Dim F As Single Dim Row As Integer Const MMax = 75 Const FMax = 125 Const FirstRow = 5 Worksheets("Slide 44").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 Cells(Row, 5) = Grade Row = Row + 1 Loop End Sub

45
Round 5 More Complicated Problems

46
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

47
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

48
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

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

50
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

51
Round 6 Use Subroutines

52
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?

53
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

54
Option Explicit 'Written By T. James Sub BiasUv9() 'Version of BiasU found on 'Slide 56 of Powerpoint Dim Gender As String * 1 Dim Midterm As Single Dim Final As Single Dim Grade As Single Dim M As Single Dim F As Single Dim Row As Integer Const MMax = 75 Const FMax = 125 Const FirstRow = 5 Worksheets("Slide 56").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 Cells(Row, 5) = Grade Row = Row + 1 Loop End Sub Sub PointV9() Dim Grade As Single Dim Point As Integer Dim row As Integer Const FirstRow = 5 Worksheets("Slide 56").Activate row = FirstRow Do Until (IsEmpty(Cells(row, 1))) Grade = Cells(row, 5) 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, 6) = Point row = row + 1 Loop End Sub

55
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)

56
Option Explicit 'Written By T. James Sub BiasUv9() 'Version of BiasU found on 'Slide 56 of Powerpoint Dim Gender As String * 1 Dim Midterm As Single Dim Final As Single Dim Grade As Single Dim M As Single Dim F As Single Dim Row As Integer Const MMax = 75 Const FMax = 125 Const FirstRow = 5 Worksheets("Slide 56").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 Cells(Row, 5) = Grade Row = Row + 1 Loop End Sub Sub PointV9() Dim Grade As Single Dim Point As Integer Dim row As Integer Const FirstRow = 5 Worksheets("Slide 56").Activate row = FirstRow Do Until (IsEmpty(Cells(row, 1))) Grade = Cells(row, 5) 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, 6) = Point row = row + 1 Loop End Sub Sub MainV9() Call BiasUv9 Call PointV9 End Sub

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

58
Round 7 Procedures

59
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

60
BiasU Name: BiasUvA Given: Gender, M, F Results Grade Intermediates None Definition –Grade:=BiasU(Gender,M,F) Method –Get Gender –Get M –Get F –If (Gender = M) Let Grade =60%M + 40%F –Else if (Gender = F) Let Grade = 40% + 60%F –Else Let Grade = -1 –Give Grade NOTE THIS IS BASICALLY THE VERY FIRST ALGORITHM Name : BiasUv7 Given : Gender, Midterm, Final Results : Grade Intermediates –M, F, MMax (Constant), FMax (Constant), –Row, FirstRow (Constant) Definition : –Grade := BiasU(Gender, Midterm, Final) Method –Set MMax = 75 –Set FMax = 125 –Set FirstRow = 5 –Go to Worksheet Slide 44 –Let Row = FirstRow –Loop Until No More Students Get Gender Get Midterm Get Final Let M = Midterm/MMax * 100 Let F = Final /FMax * 100 If (Gender = M) –Let Grade = 60% M + 40% F Else If (Gender = F) –Let Grade = 40% M + 60% F Else –Let Grade = -1 Give Grade Let Row = Row + 1 –Finish Loop

61
Point Name: BiasPts Given: Grade Result: Point Interm: Row, –FirstRow(Constant) Def: Point := BiasPts(Grade) Method –Go to Worksheet –Row = First Row –Loop Until no more Students Get Grade If Grade >= 90 –Let Point = 4 Else If Grade >= 75 –Let Point = 3 Else If Grade >= 66 –Let Point = 2 Else If Grade >= 50 –Let Point = 1 Else –Let Point = 0 Give Grade Let Row = Row + 1 –Finish Loop Name: BiasPts Given: Grade Result: Point Interm: None Def: Point := BiasPts(Grade) Method –Get Grade –If Grade >= 90 Let Point = 4 –Else If Grade >= 75 Let Point = 3 –Else If Grade >= 66 Let Point = 2 –Else If Grade >= 50 Let Point = 1 –Else Let Point = 0 –Give Grade

62
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

63
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 _

64
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 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 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

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

66
Round 8 Functions

67
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

68
BiasU Name: BiasUvA Given: Gender, M, F Results Grade Intermediates None Definition –Grade:=BiasU(Gender,M,F) Method –Get Gender –Get M –Get F –If (Gender = M) Let Grade =60%M + 40%F –Else if (Gender = F) Let Grade = 40% + 60%F –Else Let Grade = -1 –Give Grade NOTE THIS IS BASICALLY THE VERY FIRST ALGORITHM Name : BiasUv7 Given : Gender, Midterm, Final Results : Grade Intermediates –M, F, MMax (Constant), FMax (Constant), –Row, FirstRow (Constant) Definition : –Grade := BiasU(Gender, Midterm, Final) Method –Set MMax = 75 –Set FMax = 125 –Set FirstRow = 5 –Go to Worksheet Slide 44 –Let Row = FirstRow –Loop Until No More Students Get Gender Get Midterm Get Final Let M = Midterm/MMax * 100 Let F = Final /FMax * 100 If (Gender = M) –Let Grade = 60% M + 40% F Else If (Gender = F) –Let Grade = 40% M + 60% F Else –Let Grade = -1 Give Grade Let Row = Row + 1 –Finish Loop

69
Point Name: BiasPts Given: Grade Result: Point Interm: Row, –FirstRow(Constant) Def: Point := BiasPts(Grade) Method –Go to Worksheet –Row = First Row –Loop Until no more Students Get Grade If Grade >= 90 –Let Point = 4 Else If Grade >= 75 –Let Point = 3 Else If Grade >= 66 –Let Point = 2 Else If Grade >= 50 –Let Point = 1 Else –Let Point = 0 Give Grade Let Row = Row + 1 –Finish Loop Name: BiasPts Given: Grade Result: Point Interm: None Def: Point := BiasPts(Grade) Method –Get Grade –If Grade >= 90 Let Point = 4 –Else If Grade >= 75 Let Point = 3 –Else If Grade >= 66 Let Point = 2 –Else If Grade >= 50 Let Point = 1 –Else Let Point = 0 –Give Grade

70
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

71
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

72
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

73
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

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

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

76
Conclusion

77
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

Similar presentations

Presentation is loading. Please wait....

OK

BTEc unit 12 software development

BTEc unit 12 software development

© 2018 SlidePlayer.com Inc.

All rights reserved.

To ensure the functioning of the site, we use **cookies**. We share information about your activities on the site with our partners and Google partners: social networks and companies engaged in advertising and web analytics. For more information, see the Privacy Policy and Google Privacy & Terms.
Your consent to our cookies if you continue to use this website.

Ads by Google

Ppt on tuberculosis Download ppt on oxidation and reduction reaction Ppt on railway track engineering Ppt on cellular technologies and security Topics for ppt on environmental education Ppt on effect of global warming on weather radar Ppt on water pollution in hindi language Ppt on short story writing Ppt on two point perspective art Ppt on charge coupled device