1 / 15

CS111 Intro to Programming

Learn how computers perform basic arithmetic with integers and decimals, resolve issues with integer division, and dynamically adjust musical compositions. Focus on order of operations and casting.

ajones
Download Presentation

CS111 Intro to Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS111 Intro to Programming Day 7: Using Arithmetic Expressions

  2. Learning Objectives • Understand how computers perform basic arithmetic when using whole numbers (Integers) and decimal numbers (Floats). • Identify and resolve issues with integer division. • Demonstrate basic computer arithmetic by dynamically adjusting a musical composition to fit a specified number of seconds.

  3. Whole numbers and Decimals

  4. The Road Not Taken Computers treat whole numbers and decimal numbers differently. In programming we refer to whole numbers as integers and decimal numbers as floats.

  5. Processing Integers and Floats In the past computers contained a physically separate processor that was optimized for performing floating point arithmetic. Today there is one physical processor, but internally there is a dedicated floating point unit (FPU) that is optimized for the task.

  6. Expressions

  7. Arithmetic Expressions You should already be familiar with the operators. + - * /

  8. Mathematical Expressions To start, we will focus on integers, not floating points. We know volume of a rectangular prism is equal to length x width x height. length = 10 width = 5 height = 15 volume = length * width * height 750 Right-hand side evaluated first

  9. Mathematical Expressions To start, we will focus on integers, not floating points. To split a package of cookies among friends, we use the equation: # cookies / n. numCookies = 12 numFriends = 6 cookiesPerFriend = numCookies / numFriends 2 RHS evaluated first

  10. Integer Division - Casting If we want to keep the precision, we need to treat the integers as floats, then perform the calculation. numCookies = 12 numFriends = 5 cookiesPerFriend = float(numCookies) / numFriends 2 .4 “cast” numCookies as a float

  11. Order of Operations Evaluated from left to right in order of precedence. What you are used to in math. Can lead to bugs in code ifyou aren’t careful! When in doubt, use parentheses for clarity.

  12. Think-Pair-Share

  13. Order of Operations Determine the order of evaluation in the following expressions. • x = a + b + c + d + e • y = a + b * c - d / e • k = a / (b * (c + (d - e)))

  14. Order of Operations Determine the order of evaluation in the following expressions. • x = a + b + c + d + e • y = a + b * c - d / e • k = a / (b * (c + (d - e))) 5 1 2 3 4 5 3 1 4 2 5 4 3 2 1

  15. MiniTask

More Related