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.

Slides:



Advertisements
Similar presentations
Flowcharts for C programmers Ian McCrum. flowcharts Diagrams are used in many places; they can be used as a design aid or a documentation aid. As a design.
Advertisements

CSI 1306 PROGRAMMING IN VISUAL BASIC PART 2. Part 2  1. Strings  2. Translating Conditional Branch Instructions  3. Translation Set 2  4. Debugging.
Chapter 7 Introduction to Procedures. So far, all programs written in such way that all subtasks are integrated in one single large program. There is.
PROGRAMMING IN VISUAL BASIC PART 1
Object Oriented Programming A programming concept which views programs as objects with properties and ways to manipulate the object and the properties.
Modeling using VBA. Covered materials -Userforms -Controls -Module -Procedures & Functions -Variables -Scope.
Quiz 2. Quiz 2 What is output (5 Marks) [8 min] X = 987 Y = X Z = 0 Do Do While (Y > 0) Z = Z + Y Mod 10 Y = Y \ 10 Loop Y = Z Z = 0 Loop Until (Y < 10)
CSI 1306 PROGRAMMING IN VISUAL BASIC PART 5. Part 5  1. Procedures  2. Sub Procedures  3. Function Procedures.
Programming in Visual Basic
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 2, Lecture 2.
Program Design and Development
Developing Software Applications Introduction to Programming Fundamentals Scoping in VB Simple Ifs in VB.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
Adding Automated Functionality to Office Applications.
Exploring Microsoft Excel 2002 Chapter 8 Chapter 8 Automating Repetitive Tasks: Macros and Visual Basic for Applications By Robert T. Grauer Maryann Barber.
Apply Sub Procedures/Methods and User Defined Functions
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
ISAT 252 Introduction to Arrays. Should have read 2 Chapter 8 –pp , and pp
 Excel – Basic Elements  Using Macros  Excel VBA Basics  Excel VBA Advanced.
Spreadsheets in Finance and Forecasting Presentation 11 Visual Basic.
Managing Business Data Lecture 8. Summary of Previous Lecture File Systems  Purpose and Limitations Database systems  Definition, advantages over file.
Languages and Environments Higher Computing Unit 2 – Software Development.
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
Unit 11, Part 2: Introduction To Logarithms. Logarithms were originally developed to simplify complex arithmetic calculations. They were designed to transform.
Excel at Feedback “Using Excel to Generate Individual Grade Sheets for Students & Posting to the Web“ James V. Pinto John D. Eastwood
1 Visual Basic for Applications (VBA) for Excel Prof. Yitzchak Rosenthal.
Chapter 17: Arrays Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
06/10/ Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Programming Examples to Accompany Structure Topic Please use speaker notes for additional information!
Fundamental Programming Fundamental Programming More on Selection.
VBA Lab 2 I ns.Samia Al-blwi. Visual Basic Grammar Object: Visual Basic is an object-oriented language. This means that all the items in Excel are thought.
CS 320 Assignment 1 Rewriting the MISC Osystem class to support loading machine language programs at addresses other than 0 1.
MS Visual Basic Applications Walter Milner. Event-driven programming Standard approach for GUIs Contrast with old character interfaces – program determines.
Visual Basic Programming
Chapter 16: Programming Structures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Lab 4 Range Review, Control Logic and Loops ► Range Review ► Control Logic and Loops ► Exercise.
B065: PROGRAMMING Sub Procedures I. Starter  Identify the separate tasks to be performed in the programming task below (break it down into numbered sections).
HAWKES LEARNING SYSTEMS Students Matter. Success Counts. Copyright © 2013 by Hawkes Learning Systems/Quant Systems, Inc. All rights reserved. Section 6.4.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 3, Lecture 1.
22/11/ Selection If selection construct.
Chapter 16: Programming Structures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
ME 142 Engineering Computation I Using Subroutines Effectively.
Intermediate 2 Computing Unit 2 - Software Development.
ME 142 Engineering Computation I Using Subroutines Effectively.
Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
31/01/ Selection If selection construct.
Comments, Conditional Statements Continued, and Loops Engineering 1D04, Teaching Session 4.
Controlling Program Flow with Decision Structures.
Visual Basic Review LBS 126. VB programming Project Form 1Form 2Form 3 Text boxButton Picture box Objects Text box Button Objects.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software.
Pre-Algebra Tutorial. Pre-Algebra Equations x + 3 = 5 What is the value of x? At first glance this may look easy since all you have to ask yourself is.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
CIS 338: VB Variables Dr. Ralph D. Westfall April, 2011.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Macros in Excel Using VBA Time Required – 5 hours.
Computer Science Up Down Controls, Decisions and Random Numbers.
Lesson Objectives Aims Key Words
VBA - Excel VBA is Visual Basic for Applications
COMPUTATIONAL CONSTRUCTS
Exploring Microsoft Excel
PROGRAMMING Sub Procedures I.
Introduction to Problem Solving and Control Statements
If, Subroutines and Functions
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
Learning Intention I will learn about the standard algorithm for input validation.
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
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

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 * Final Else Grade = 0.4 * Midterm * Final End If MsgBox ("The Grade is " & Grade) End Sub

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"

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

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 * Final ElseIf (Gender = "F") Then Grade = 0.4 * Midterm * 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

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* *??/125*100, IF(??="F", 0.4*??/75* *??/125/100, -1))  What if we used Anchored cells like D1, and D2? –=IF(??="M", 0.6*??/$D$1* *??/$D$2*100, IF(??="F", 0.4*??/$D$1* *??/$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

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

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 * F ElseIf (Gender = "F") Then Grade = 0.4 * M * F Else Grade = -1 End If MsgBox ("The Grade is " & Grade) End Sub

Compare What about future years?

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* *??/125*100, IF(??="F", 0.4*??/75* *??/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 * F ElseIf (Gender = "F") Then Grade = 0.4 * M * 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

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

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 * F ElseIf (Gender = "F") Then Grade = 0.4 * M * F Else Grade = -1 End If MsgBox ("The Grade is " & Grade) Count = Count + 1 Loop End Sub

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

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

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 * F ElseIf (Gender = "F") Then Grade = 0.4 * M * F Else Grade = -1 End If MsgBox ("The Grade is " & Grade) Again = MsgBox("Again", 4) Loop Until (Again = 7) End Sub

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

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

 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 * F ElseIf (Gender = "F") Then Grade = 0.4 * M * F Else Grade = -1 End If Cells(Row, 5) = Grade Next Row End Sub

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

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

 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 * F ElseIf (Gender = "F") Then Grade = 0.4 * M * F Else Grade = -1 End If Cells(Row, 5) = Grade Row = Row + 1 Loop End Sub

Round 5 More Complicated Problems

Adding a new Result  Say BiasU uses a four point system for their grades. – > 4 Points – > 3 Points –66 – > 2 Points –50 – > 1 Point –0 – > 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 * F ElseIf (Gender = "F") Then Grade = 0.4 * M * 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

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 * F ElseIf (Gender = "F") Then Grade = 0.4 * M * 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

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)

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 * F ElseIf (Gender = "F") Then Grade = 0.4 * M * 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

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

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

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

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 _

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 * F ElseIf (Gender = "F") Then Grade = 0.4 * M * 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

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

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

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

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 * F ElseIf (Gender = "F") Then Grade = 0.4 * M * 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 * F ElseIf (Gender = "F") Then Grade = 0.4 * M * 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