Presentation is loading. Please wait.

Presentation is loading. Please wait.

This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed.

Similar presentations


Presentation on theme: "This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed."— Presentation transcript:

1 This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed. If you have opened this lesson in PowerPoint, use the PowerPoint menus to view it in slide show mode. If you have opened this lesson in a browser and see a bar similar to that below, click on the Slide Show icon A notice similar to the one below may appear warning that ActiveX or other scripts are disabled. Enable the controls for this website in order to see the animations.

2 Modulo This slide show describes the modulo operation. Christine S. Wolfe Ohio University Lancaster 2008-Aug-01 Vocabulary: modulo operand

3 A bakery box holds exactly 12 donuts. We can only sell donuts by the full box. Leftover donuts can be eaten by the staff. If we bake 24 donuts, there are no leftovers. If we bake 25 donuts, there is 1 leftover. If we bake 14 donuts, there are 2 leftovers. Christine Wolfe: Ohio University Lancaster

4 A bakery box holds exactly 12 donuts. We can only sell donuts by the full box. Leftover donuts can be eaten by the staff. How many donuts can the staff eat if… … 50 donuts are baked? 2 … 73 donuts are baked? 1 … 23 donuts are baked?11 … 66 donuts are baked? 6 … 15 donuts are baked? 3 … 36 donuts are baked? 0 … 126 donuts are baked? 6 … 10 donuts are baked?10 … 79 donuts are baked? 7 … 0 donuts are baked? 0 Christine Wolfe: Ohio University Lancaster Click anywhere to see the next answer

5 Each cabin holds 5 campers. If there aren't enough campers for a full cabin, the extra campers have to sleep in the infirmary. If 5 people are camping, they all stay in a cabin and no one sleeps in the infirmary. Christine Wolfe: Ohio University Lancaster

6 Each cabin holds 5 campers. If there aren't enough campers for a full cabin, the extra campers have to sleep in the infirmary. If 11 people are camping, 10 stay in cabins and 1 sleeps in the infirmary. Christine Wolfe: Ohio University Lancaster

7 Each cabin holds 5 campers. If there aren't enough campers for a full cabin, the extra campers have to sleep in the infirmary. Assuming an unlimited number of cabins, how many people have to sleep in the infirmary if…. … 6 go camping? … 10 go camping? … 15 go camping? … no one goes camping? … 5001 go camping? … 16 go camping? … 24 go camping? … 18 go camping? … 59 go camping? … 2 go camping? Christine Wolfe: Ohio University Lancaster

8 The operation of determining "leftovers" or "remainders" is called Either of the following symbols is used to represent the modulo operation: % or Mod ( We will use % because that is the symbol used in C ) The operation to learn how many donuts are leftover after 50 are baked and stored in boxes that can hold 12 donuts is written: 50 % 12 and is read, "fifty modulo twelve" The solution is written: 2 = 50 % 12 and is read, "Two is the result of fifty modulo twelve." If 13 donuts are baked, the solution is: 1 = 13 % 12 If 24 donuts are baked, the solution is: 0 = 24 % 12 If you don't know how many donuts will be baked, you can use a variable to represent the number of donuts. In that case, the solution is: NumberOfLeftovers = NumberOfDonuts % 12 modulo. click tip In programming, the variable that holds the result or answer is ALWAYS written on the left of the assignment operator

9 The operation to learn how many campers have to sleep in the infirmary when 11 campers show up and each cabin can hold 5 campers is written: 11 % 5 and is read, "eleven modulo five" The solution is written: 1 = 11 % 5 and is read, "One is the result of eleven modulo five." If you don't know how many campers will arrive, you can use a variable to represent the number of campers. In that case, the solution is: NumberWhoSleepInInfirmary = NumberOfCampers % 5 If you don't know how many campers will arrive AND you don't know how many campers can fit in a cabin, you can use variables to represent these unknowns. NumberWhoSleepInInfirmary = NumberOfCampers % NumberCampersPerCabin Christine Wolfe: Ohio University Lancaster

10 click tip Notice that the variable names do not start with a digit and they don't include any spaces.

11 What is the result of the following modulo operations? 16 % % 2 17 % % 8 6 % 2 14 % % % 5 25 % % % 10 7 % 3 7 % % 98 Christine Wolfe: Ohio University Lancaster 44 % 0Division by zero error -17 % 5implementation specific 25 % 8 implementation specific

12 Modulo is used to solve problems where it is necessary to know if one number can be evenly divided by another number. Example 1: All even numbers and only even numbers are evenly divisible by 2. If the result of MysteryNumber % 2 is zero, then MysteryNumber must be even. Example 1: All even numbers and only even numbers are evenly divisible by 2. If the result of MysteryNumber % 2 is zero, then MysteryNumber must be even. Example 2: How many reams of paper are needed if I want to produce x number of 1 page brochures? Suppose I want to produce 600 brochures. 600 pages / 500 pages per ream = 1.2 reams But my paper is only sold in full reams. If I round off to 1 ream, I won't have enough paper. If I always buy 1 extra ream, I may buy a ream that I will never use as in the case when I need exactly 1000 pages. The solution is to test the result of NumberOfPages % 500 If the result is 0, do not add the extra ream. If the result is greater than 0, then add another ream to the order. Example 2: How many reams of paper are needed if I want to produce x number of 1 page brochures? Suppose I want to produce 600 brochures. 600 pages / 500 pages per ream = 1.2 reams But my paper is only sold in full reams. If I round off to 1 ream, I won't have enough paper. If I always buy 1 extra ream, I may buy a ream that I will never use as in the case when I need exactly 1000 pages. The solution is to test the result of NumberOfPages % 500 If the result is 0, do not add the extra ream. If the result is greater than 0, then add another ream to the order. Christine Wolfe: Ohio University Lancaster


Download ppt "This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed."

Similar presentations


Ads by Google