Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 7 Empowering Programs with Math

Similar presentations


Presentation on theme: "Chapter 7 Empowering Programs with Math"— Presentation transcript:

1 Chapter 7 Empowering Programs with Math

2 LESSON: So far, we have seen the four basic math operations. To do more, the Math OBJECT gives us access to other operators and we will explore these. The format is: variable = Math.Operator (number(s)) When you click on the word, “Math”, a menu of choices pops up on the right. We will learn the different functions by example and practice. Prepare a new Chapter 7 folder on your flash drive to store these programs.

3 This is how we use the Square Root function:
ans = Math.SquareRoot (num) Type this in EXACTLY as you see it and press RUN. {First, get INPUT for the number.} {Next, perform the CALCULATION.} {Lastly, print out the OUTPUT.} Run the program three times: Use a perfect square, a non-perfect square, and a negative number.

4 To find an Exponential Expression: ans = Math.Power(base, exponent)
Now, to find , type this in EXACTLY as you see it and press RUN. {The first number, “3”, is the base The second number, “5”, is the exponent} The “3” and “5” could have been variables, also.

5 Please type in the POWERDEMO program on page 85 or on your note taking guide, EXACTLY as you see it and press RUN. Here is the result: Wow, what a crazy answer! Now, let’s look at how to get numbers to round correctly.

6 Please type in the POWERDEMO program on page 85 or on your note taking guide, EXACTLY as you see it and press RUN.

7 To Round: ans = Math.Round(num) The answer will round correctly to the nearest integer. If you add: A = Math.Round(A) right after the calculation in the POWERDEMO program, the result will look like this: Pretty good, but we can do much better!

8 What if we want to Round to the nearest penny (1/100th) we use: A = Math.Round(A*100) / 100
Add this to your POWERDEMO program, then RUN. Now, this looks right! How would you round to ONE decimal place? A = Math.Round(A*10) / 10 How would you round to THREE decimal places? A = Math.Round(A*1000) / 1000

9 Another kind of rounding: ans = Math.Floor(num)
The METHOD will round Down to the nearest integer. Better known as the Greatest Integer Function. Sounds backwards, but it is the largest integer less than or equal to the number. It always “snaps to the left.” For example, both 3.1 and 3.9 both round down to 3. 3 If we look at negative numbers, it still “snaps to the left.” However, you have to think about what is on the left. – 3.1 and – 3.9 snap to – 4. -4

10 A third kind of rounding: ans = Math.Ceiling(num)
The METHOD will round Up to the nearest integer. Better known as the Least Integer Function. Also sounds backwards, but it is the smallest integer larger than or equal to the number. It always “snaps to the right.” For example, both 3.1 and 3.9 both round up to 4. 4 If we look at negative numbers, it still “snaps to the right.” However, you have to think about what is on the right. – 3.1 and – 3.9 snap to – 3. -3

11 Mathematics is important to computing, but not as important as what people can do:

12 Chapter 7 Programming Assignment 1
Use your SHELL and SAVE AS PAYMENTS The complete program instructions are on your handout. Just a few things to emphasize: Use CursorTop & CursorLeft to organize your output. Let the user input the interest in percent form, but we will divide it by 1200 (100 to convert to decimal and 12 as it is a monthly payment. Assign that to R for the formula. Convert the formula: to be able to program with it. Assign the exponent part (1 + R)N to a separate variable and use it in the formula. Print your program and output. Turn in. Due _________

13 Absolute Value: ans = Math.Abs(num)
Absolute Value is distance from zero. Distance is always positive, therefore Absolute Value is always positive! Math.Abs(4) returns a value of 4, while Math.Abs(– 3 ) returns a value of 3 -3 4

14 This METHOD Returns The Larger Of Two Numbers:
ans = Math.Max(num1, num2) Type this in EXACTLY as you see it and press RUN. {Why would you need this? When you are comparing variables!}

15 This METHOD Returns The Smaller Of Two Numbers:
ans = Math.Min(num1, num2) Type this in EXACTLY as you see it and press RUN.

16 Remainder: ans = Math.Remainder(num1, num2)
This METHOD uses two arguments: num1 is divided by num2. It returns the integer remainder. For example: ans = Math.Remainder(20, 3) Would return a value of 2, because Believe it or not, remainders are VERY important in mathematics. Just remember the rule: Divison by 3 leaves 3 remainders: 0, 1, 2 Divison by 7 leaves 7 remainders: 0, 1, 2, 3, 4, 5, 6 etc.

17 Look at the MONEY program on page 90
Look at the MONEY program on page 90. I have slightly modified the program. Lets look at it line, by line.

18 Chapter 7 Programming Assignment 2
Use your SHELL and SAVE AS WHEN IS EASTER? The complete program instructions are on your handout, along with the Easter Sheet and Student Worksheet. First, convert each equation from the Easter Sheet on the Student Worksheet. I will grade it. Next, write the code and apply the equation conversions. Print your program and output. Turn in. Due __________

19 Trigonometric METHOD covers several trig concepts
Trigonometric METHOD covers several trig concepts. Today, we are only going to look at converting degree angles to radians. A game, described on page 93, has the user input an angle, to destroy the an enemy warehouse that is 500 feet away. “If at first you don’t succeed …” The computer uses radians, but we are more familiar with degrees, thus the conversion from lines 6 to 7. (Price, 2016) Mini Program 1: Type the program “Android Attack” and RUN until you get to 500 feet. SAVE, but do not print, I will check it via VISION. Due __________

20 Random Number between two numbers, i. e
Random Number between two numbers, i.e. from a beginning (beg) number to an ending (end) number: (beg – 1) + Math.GetRandomNumber(end – beg +1) For example, to generate a number between 10 and 100: x = ___ + Math.GetRandomNumber( ____ ) 9 91 Type this in EXACTLY as you see it and press RUN. Did anyone get a number outside of the 10 to 100 range?

21 x = ___ + Math.GetRandomNumber( ___ )
A little easier is a Random Number METHOD that begins at 1. Say, between 1 and 6. What would you have? x = ___ + Math.GetRandomNumber( ___ ) The general formula for 1 to any ending number is: ans = Math.GetRandomNumber(end) 6 Do we really need the “0”? Type this in EXACTLY as you see it and press RUN.

22 Chapter 7 Programming Assignment 3
Use your SHELL and SAVE AS DICE GAME Using the coding from the previous slide, you will create a game for you and your friends. Give your game a name and use the Cursor METHODS to give it some readability. The player will enter his/her name. Each player will take a turn using the program. It will keep a running total (sum) of his/her points for each turn. Whoever gets the most points wins! Print your program and one output. Turn in. Due __________


Download ppt "Chapter 7 Empowering Programs with Math"

Similar presentations


Ads by Google