Part 1 Arithmetic Operator Precedence

Similar documents
Part 1 Simple Arithmetic

Part 1 Your First Function

CSI Lab 02. Tuesday, January 21st

Part 1 - Your First algorithm

Part 1 - Your First algorithm

Chapter 2. Designing a Program. Input, Processing, and Output Fall 2016, CSUS. Chapter 2.1

Area rectangles & parallelograms

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Someone else might choose to describe the closet by determining how many square tiles it would take to cover the floor. 6 ft.

8.5 Volume of Rounded Objects

Control, Quick Overview. Selection. Selection 7/6/2017. Chapter 2. Control

Skill 1: Multiplying Polynomials

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG

More Formulas: circles Elementary Education 12

MITOCW ocw f99-lec12_300k

Basic and Intermediate Math Vocabulary Spring 2017 Semester

Formal Methods of Software Design, Eric Hehner, segment 1 page 1 out of 5

Name: Target 12.2: Find and apply surface of Spheres and Composites 12.2a: Surface Area of Spheres 12.2b: Surface Area of Composites Solids

Variables and Constants

(Refer Slide Time: 00:51)

Project 2: How Parentheses and the Order of Operations Impose Structure on Expressions

= 25)(10) 10. =

In our first lecture on sets and set theory, we introduced a bunch of new symbols and terminology.

lundi 7 janvier 2002 Blender: tutorial: Building a Castle Page: 1

Table of Contents. Introduction to the Math Practice Series...iv Common Mathematics Symbols and Terms...1

Overview (4) CPE 101 mod/reusing slides from a UW course. Assignment Statement: Review. Why Study Expressions? D-1

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I

GENERAL MATH FOR PASSING

Dr. Scheme evaluates expressions so we will start by using the interactions window and asking Dr. Scheme to evaluate some expressions.

How to Excel at Middle School Math Competitions Huasong Yin

YCL Session 4 Lesson Plan

Lesson 6: Manipulating Equations

Essentials. Week by. Week. Mathmania. Algebraically Speaking. Investigate Data. Solve It!

VBScript: Math Functions

Exponents. Reteach. Write each expression in exponential form (0.4)

MAT 003 Brian Killough s Instructor Notes Saint Leo University

PIETRO, GIORGIO & MAX ROUNDING ESTIMATING, FACTOR TREES & STANDARD FORM

Archdiocese of Washington Catholic Schools Academic Standards Mathematics

UNIT 3 CIRCLES AND VOLUME Lesson 5: Explaining and Applying Area and Volume Formulas Instruction

Post Experiment Interview Questions

What is a Fraction? Fractions. One Way To Remember Numerator = North / 16. Example. What Fraction is Shaded? 9/16/16. Fraction = Part of a Whole

Note: Please use the actual date you accessed this material in your citation.

More Complicated Recursion CMPSC 122

Math Fundamentals for Statistics (Math 52) Unit 4: Multiplication. Scott Fallstrom and Brent Pickett The How and Whys Guys.

Virginia Mathematics Checkpoint Assessment GEOMETRY G.14. Topic: Three-Dimensional Figures

Unit 4 End-of-Unit Assessment Study Guide

6.001 Notes: Section 8.1

An Introduction to Maple This lab is adapted from a lab created by Bob Milnikel.

CITS2401 Computer Analysis & Visualisation

SAMLab Tip Sheet #1 Translating Mathematical Formulas Into Excel s Language

Workbook Also called a spreadsheet, the Workbook is a unique file created by Excel. Title bar

MITOCW watch?v=ytpjdnlu9ug

Objective- Students will be able to use the Order of Operations to evaluate algebraic expressions. Evaluating Algebraic Expressions

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #23 Loops: Precedence of Operators

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. A Guide to this Instructor s Manual:

Drawing a Circle. 78 Chapter 5. geometry.pyde. def setup(): size(600,600) def draw(): ellipse(200,100,20,20) Listing 5-1: Drawing a circle

5th Grade Mathematics Essential Standards

Definition: A data structure is a way of organizing data in a computer so that it can be used efficiently.

SEQUENCES AND SERIES Sequences CC Standard

YEAR 9 AUTUMN TERM PROJECT NETS and SURFACE AREA

My 5 th Grade Summer Math Practice Booklet

Dealing with Output. Producing Numeric Output CHAPTER 3. Task. Figure 3-1. The program in action

R Basics / Course Business

4. Java Project Design, Input Methods

EMERGENCY SHELTER DESIGN STEM LEARNING AT ITS BEST

MITOCW watch?v=0jljzrnhwoi

Direct Variations DIRECT AND INVERSE VARIATIONS 19. Name

FAQ - Podium v1.4 by Jim Allen

CS112 Lecture: Variables, Expressions, Computation, Constants, Numeric Input-Output

SP 713 Slide Rule Practice for our MIT Museum visit By Alva Couch and Elizabeth Cavicchi

10.7 Surface Area and Volume of Cylinders

After an "Hour of Code" now what?

MITOCW watch?v=se4p7ivcune

Telling a Story Visually. Copyright 2012, Oracle. All rights reserved.

Math 3 Coordinate Geometry Part 2 Graphing Solutions

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

Table of Contents. Introduction to the Math Practice Series...1

Intro. Speed V Growth

Curriculum Catalog

UNIT 4: LENGTH, AREA, AND VOLUME WEEK 16: Student Packet

Troubleshooting Maple Worksheets: Common Problems

Chapter 7 Connect Algebra to Geometry

S3 (3.1) N5 Volume.notebook April 30, 2018

Middle School Summer Review Packet for Abbott and Orchard Lake Middle School Grade 7

Middle School Summer Review Packet for Abbott and Orchard Lake Middle School Grade 7

Tier 2: GEOMETRY INTRODUCTION TO GEOMETRY Lessons Abbreviation Key Table... 7 G1 LESSON: WHAT IS GEOMETRY?... 8 G1E... 9 G1EA...

4. Write sets of directions for how to check for direct variation. How to check for direct variation by analyzing the graph :

WELCOME! (download slides and.py files and follow along!) LECTURE 1

Foundations, Reasoning About Algorithms, and Design By Contract CMPSC 122

Chapter 2. The Midpoint Formula:

Name: Dr. Fritz Wilhelm Lab 1, Presentation of lab reports Page # 1 of 7 5/17/2012 Physics 120 Section: ####

Session 27: Fractals - Handout

MITOCW MIT6_172_F10_lec18_300k-mp4

Volume by Slicing (Disks & Washers)

You may use a calculator for these practice questions. You may

Microsoft Office Excel 2010 Extra

2017 Summer Review for Students Entering Pre-Algebra 7 & Pre-Algebra 8

Lecture 3. Input, Output and Data Types

(Refer Slide Time 3:31)

CS112 Lecture: Working with Numbers

Transcription:

California State University, Sacramento College of Engineering and Computer Science Computer Science 10: Introduction to Programming Logic Activity C Expressions Computers were originally designed for their fantastic ability to perform complex math. That hasn t changed much over the many, many decades since the first mainframes. Even today, most of our favorite applications make use of complex math (behind the scenes). For example, the video games that you play (rather than study) are heavily reliant on computations from determining the path of a projectile, the gravity under your avatar s feet, and even things like making realistic shadows, shading, and mist. Part 1 Arithmetic Operator Precedence Before doing anything on the computer, think about the following expression: 4 + 6 / 2 * 3 What is the result? It could be 15 or it could be 13 or, perhaps, 5. Which is it? Hopefully, you recognize that the right answer is 13. This is because of the "precedence" of operators. The multiplication operator (an asterisk in almost all programming languages) has higher precedence than the addition operator. Both multiplication and division have the same level and are calculated from left-to-right. There is more discussion of this issue in the lecture textbook. Part 2 Simple Mathematics Let's look at a very simple mathematical equation from geometry the diameter of a circle vs. its radius. Of course, back in high school, you learned that the diameter is equal to twice the radius. (Yes, you probably guessed by now, it's time to dust off your geometry boots) 1. Start a new flowchart 2. Add an assignment that computers the radius from given diameter. The following is the pseudocode from the textbook: Set radius = diameter / 2 3. Uh oh, that needs two variables. So, add declarations to the start of your program. Make them real numbers. 4. Let's set the diameter to a value. Add an assignment that sets diameter to 25. 5. Add an output statement to the end of the flowchart. Output the radius.

2 6. Execute your flowchart. Did you get the correct result? If not, see what you did wrong. Let's make the results look better. Just printing a number doesn't help the user. 7. Use an Output statement to make your output look like this. (Remember the concatenation operator?) The radius is 12.5 Hard-coding the diameter to 25 is good for testing, but doesn't make a very useful program. 8. Replace the Diameter assignment statement with an input statement. 9. Add a helpful prompt an output statement asking the user to enter the diameter. In this part, you did the calculations separately from the output statement, saving the result (and then printing that saved result in the output statement). You could have alternatively just output diameter / 2. Both approaches would work. But there is a big difference. By saving the result in a variable, we can reuse it later in the program. Part 3 Mathematical Expressions In your future programs especially if you are doing something with graphics you will use geometry. So, let's do something with circles and shapes. The following is the equation used to calculate the area of a circle. How can we implement this formula in Flowgorithm? The formula uses π right next to the variable r. We know that this means that they will be multiplied. However, computers aren't that smart. So, while we know there is a multiplication operator there, the computer doesn't Also, how are we going to get π? Naturally, you can write out 3.1415, but that can get a bit tiresome and makes your program hard to read. Fortunately, since pi is a common constant, many programming languages have a built-in constant for it. In Flowgorithm, it is simply pi. You can use pi as a constant anywhere a variable will work. Finally, what about the exponent? Some languages provide an exponentiation operator. Many languages (sadly) don't. When the operator is supported (as it is in Flowgorithm), the symbol "^" is used. 1. Add a new declaration to store the area. The formula uses A, but that's very short and not very descriptive. In computer science, it is good to have readable variable names. Use the name: circle. 2. Add a new assignment statement for the area according to the formula above. You created a variable name radius, so make sure you use the right name in your expression instead of r. 3. Output the area of the circle to the screen. 4. Execute and check the results. 5. If you didn't do so already, add a nice caption to the output statement. Let the user know it s the area of a circle.

Take moment to think about how the calculation would look if you didn't use the value stored in radius. Without it, you would have to use diameter/2 instead. Would you need parenthesis around diameter/2?. Think about operator precedence. The answer is "yes", you would need them. The exponent operator is performed before multiplication and division. 3 Part 4 More mathematics Now that you can compute the area of a circle, it would be nice to also compute the area for a few more geometric shapes. Below is the formula to compute the surface area of a sphere. Notice that the mathematical formula again uses A to represent the area. Mathematical books do this all the time. However, for your flowchart, this is a different value than the area of a circle. NOTE: Basic Structure of Programs You might have guessed already that programs generally have a very simple structure. First, variables are declared. Second, input values are read (with prompts). Third, calculations are performed. Finally, the results are output. This basic approach makes programs easier to read, write, and modify. Declare variables Input Values Calculations Output results 1. Add a new variable (a real) to store the surface area of a sphere. Choose a good name. 2. Implement the formula above. 3. Output the calculated result with a nice caption. 4. Execute your flowchart and check the results. Excellent work! So far your flowchart: inputs a diameter (with a nice prompt) and outputs the area of a circle and the surface area of a sphere. Let's add one more geometric shape to your flowchart. This time, you will calculate the surface area of a tube such as a pipe, straw, etc. The formula is below. Notice that you will need another variable to store the surface area of the tube as well as the height.

4 5. Add variable declarations for both height and the area of a tube (which you are calculating). Make both reals (real). 6. Add a new input shape for the height. Put it right after you input the diameter. 7. Add a nice prompt for the height. 8. Add an assignment shape for the volume of a tube. 9. Add an output shape with a nice caption. 10. Execute your flowchart and check the results. Part 5 Putting It All Together Wow! That is a lot of math! You calculated the area of a circle, sphere, tube, and displayed all these calculations nicely to the screen. Your flowchart is actually useful! So, let's use your skills to calculation something truly important the surface area of a Minion. Minions are helpful (and mischievous) little creatures that help Gru with his evil plans. Well, at least when they aren't looking for bananas. But, why does Gru need the surface area of a minion? Perhaps, he is planning to paint them in camouflage. Is he planning to invade Sacramento Hall to take over Sac State? (gasp!) Or, perhaps, he is getting enough sun tan lotion to take them all to the Bahamas! Either way it's probably better we don't know. Minions are actually very mathematically shaped creatures and Gru can use basic geometry to figure out their surface area:

5 So, a minion's surface area is half the area of a sphere (their "forehead"), the surface area of a tube (their "head/body"), and the area of a circle (their "bum"). We can use the formulas above to figure out the result. At this point, it is important to look at what your flowchart already does. You already calculated the volume of sphere, tube, and circle. You could create a very fancy expression by combining all the above formulas. There is nothing wrong with that approach. However, programmers often like to break a large problem into smaller parts. NOTE: Divide and Conquer Sometimes it is useful to break complicated arithmetic expressions down in this way to help you (the human) understand them. Other times, you'll be happy with the parenthesized, and somewhat more complicated, combined expressions. There is no real rule for which is best. Whatever makes it easier for you (and others) to understand is the best choice. Let's first try it the hard way. 1. Add another variable (a real again) to hold the surface of a minion. The name is up to you. 2. Add a new assignment shape to calculate the area of minion. Don't use your existing values for the sphere, circle or tube. Combine the formulas above. 3. Output the result of the calculation. Give it a nice caption. 4. Execute your flowchart and check the results. Once you make sure the math is correct, let's see if doing it the "easy" way gets the same result. 5. Add another variable to store the second minion calculation. Think of a good name. 6. Add an assignment shape to compute the surface area. This time, use the sphere, circle, and tube variables you calculated. 7. Output the result with a nice caption 8. Execute your flowchart. The two calculations should match. If not, you made a mistake. Fix it. Which approach do you prefer?

The End Result 6 Your program should have the following: Variable declarations: diameter, radius, area of circle, area of sphere, area of a tube, area of minion (using a complex expression), and the area of a minion (using existing variables) Input statements: diameter and height Assignments: radius, area of circle, area of sphere, area of a tube, area of minion (using a complex expression), and the area of a minion (using existing variables) Output: all the calculated variables with nice captions.