Mastering MOD and DIV (“/”) MOD DIV S TAT E MENT S S U B R O UT INE S T R ACE TA B L ING P R E D ICTI ONS U S E O F MO D A ND D I V Why VB.Net ? The Language resembles Pseudocode - good for teaching and learning fundamentals of programming Operators Operator Mathematical Function Addition + ^ * / Subtraction Exponential MOD \ (or DIV) in VB.Net “\” is used. Example 1+2=3 4–2=2 2^4 = 16 2^3 = 8 etc Multiplication 7 * 2 = 14 Division - ordinary 12 / 4 = 3 Modulus (returns remainder from integer division) Integer Division – discards the decimal place Mod 4 = 3 or 255 Mod 10 = 5 19 \ 4 = 4 or 19 DIV 4 = 4 MOD In Computing, the modulo operation finds the remainder of division of one number by another (sometimes called modulus). For instance, the expression "5 mod 2" would evaluate to a quotient of 2? and a remainder of 1? "9 mod 3" would evaluate to leaves a remainder of 0? 0? 1? because 5 divided by 2 leaves because the division of 9 by 3 has a quotient of 3? and What about when the first no. is smaller! ? What about 1 Mod 4? Answer: If you divide 1 by 4, you get 0? with a remainder of 1. That's all the modulus is, the remainder after division. In this case, 4 CANNOT go into 1 at all, ? which is why the answer is 0. The remainder is simply the first number, in this case = 1! MOD 10 MOD 5 = 15 MOD 5 = 12 MOD 5 = 5 MOD 13 = 1 MOD 4 = 3 MOD 10 = 9 MOD 10 = 10 MOD 9 = 0? 0? 2? 5? 1? 3? 9? 1? Remember, the MOD is just the REMAINDER After DIVISION! DIV DIV 10 DIV 5 = 15 DIV 5 = 14 DIV 3 = 2 DIV 10 = 22 DIV 11 = 1 DIV 1 = Remember, the is just the finding the number of divides in long division 2? 3? 4? 0? 2? 1? i.e. 10 DIV 7 = 1 (7 goes into 10 only ONCE) In DIV, we are concerned only with the quotient (not the remainder) *It is useful to note that in VB.net there is no DIV - but “/” is used instead Analyse the following code n=2 N T 2 L 0 R 0 2 Mod 10 = 2 Output 0 2/10 = 1/5 (but this is an integer) so 0 T+L 0+2 =2 N=R N=0 =0 L=2 R=0 T=2 N=0 So what’s the point of MOD and DIV • Both can be used to calculate various results. *If you are still not sure about MOD and DIV – watch this video: • They basically produce a single value – so are often used in Functions. finds the number of divides in long division i.e. 10 DIV 7 = 1 • They act as Boolean values (e.g. in MOD – either there IS a remainder or there IS NOT a remainder) • The next slide(s) look at examples of the use of MOD and DIV in coding http://www.youtube.com/watch?v=b5cb_nfDyyM Analyse this – any idea what it is doing? Here we are calling the sub with the parameter “number” – that means the variable or parameter “number” is being PASSED by Value to the subroutine This is a type of recursion - as it is a subroutine that “CALLS ITSELF” (that is the definition of recursion = “a function that calls itself”) Here I have declared an array that can hold 8 integers. (0 to 7 – which gives us 8!) Analyse this – any idea what it is doing? This is a familiar “For Loop”, but this time it is going from 7 to 0 (and stepping down by 1) Usually For loops go from 0 to 10 (incrementing by 1). Note the change here. Here you can see the DIV function being used. …and here’s MOD! This is the output of the program. (The input is a single integer number and the output is……an array of digits) …so - - - -what is the output!? What is this program doing!? Completing a complex trace table… • Can feel like climbing a very difficult mountain with no end in sight (especially when the algorithm is complex) • It’s actually quite simple • The keys to success are: • BE PATIENT • BE METHODICAL • BE UTTERLY AND PAINSTAKINGLY ACCURATE (NO CARELESS MISTAKES) • DO NOT GIVE UP Trace table it ….(I have amended the array slightly for this example) Try it with the number input: 2 Number Myarray(0) Myarray(1) Myarray(2) Myarray(3) 2 Numbertoconvert i output 2 3 *This is DIV 0 0 =numbertoconvert \ (2 ^ i ) =2 \ (2 ^ 3) numbertoconvert =numbertoconvert Mod 2 ^ i Numbertoconvert = 2 Mod 2 ^ 3 Numbertoconvert = 2 Mod 8 Numbertoconvert = 2 =numbertoconvert \ (2 ^ i ) =2 \ (2 ^ 2) =2\8 =2\4 =0 =0 2 2 2 numbertoconvert =numbertoconvert Mod 2 ^ i Numbertoconvert = 2 Mod 2 ^ 2 Numbertoconvert = 2 Mod 4 Numbertoconvert = 2 Part 2 – nearly there! Try it with the number input: 2 Number Myarray(0) Myarray(1) Myarray(2) Myarray(3) 2 Numbertoconvert i output 2 3 *This is DIV 0 0 2 2 2 *We are here 1 1 0 0 0 0 =numbertoconvert \ (2 ^ i ) =numbertoconvert \ (2 ^ i ) =2 \ (2 ^ 1) = 0 \ (2 ^ 0) =2\2 =1 numbertoconvert =numbertoconvert Mod 2 ^ i Numbertoconvert = 2 Mod 2 ^ 1 Numbertoconvert = 2 Mod 2 Numbertoconvert = 0 =0\1 =0 numbertoconvert =numbertoconvert Mod 2 ^ i Numbertoconvert = 0 Mod 2 ^ 0 Numbertoconvert = 0 Mod 1 Numbertoconvert = 0 And finally – what is the output! Try it with the number input: 2 Number Myarray(0) Myarray(1) Myarray(2) Myarray(3) 2 Numbertoconvert i output 2 3 *This is DIV 0 0 2 2 2 1 1 *We are here 0 0 0 0 Myarray(3) = 0 Myarray(2) = 0 The input was: 2 It is simply asking you to OUTPUT the values of Myarray(i) – starting with Myarray(3) and going down to MyArray(0) And the output is: 0 0 1 0 Myarray(1) = 1 Myarray(0) = 0 WHAT IS IT ? A Decimal to Binary Converter! The one on the left doesn’t work perfectly because the array only allows for 4 bits. Try coding the one on the right yourself to see how it works! [ESC]* Teachers only: Code to cut and paste -students are advised to type it out line by line If you’re feeling adventurous, here is another example of a program that uses MOD • See if you can code it in yourself (use VB.Net Console)