CMSC 104, Version 8/061L10ArithmeticOps.ppt Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class.

Slides:



Advertisements
Similar presentations
Arithmetic Calculations
Advertisements

CMSC 104, Version 9/011 Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class Project Incremental.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 63 – Manipulating Data Using Methods – Day 2.
1 ICS103 Programming in C Lecture 4: Data Types, Operators & Expressions.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
Introduction to Computers and Programming - Class 2 1 Introduction to Computers and Programming Class 2 Introduction to C Professor Avi Rosenfeld.
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
CMSC 104, Version 9/01 1 The Box Problem: Write an interactive program to compute and display the volume and surface area of a box. The program must also.
General Programming Introduction to Computing Science and Programming I.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
CMSC 104, Version 9/011 Incremental Programming Topics Review of Incremental Programming Example of Incremental Programming Reading None.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
Input, Output, and Processing
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Operators in Python. Arithmetic operators Some operators in Python will look familiar (+, -, *, /) Others are new to you (%, //, **) All of these do work.
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
CPS120: Introduction to Computer Science Operations Lecture 9.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
CHAPTER 2 COMPONENTS OF A PROGRAMMING LANGUAGE I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Lecture 5: Stopping with a Sentinel. Using a Sentinel Problem Develop a class-averaging program that will process an arbitrary number of grades each time.
Mathematical Calculations in Java Mrs. C. Furman.
Program Development C# Programming January 30, 2007 Professor J. Sciame.
CMSC 104, Version 9/01 1 Functions, Part 3 of 3 Topics: Coding Practice o In-Class Project: The Box o In-Class Project: Drawing a Rectangle Reading: None.
JavaScript 101 Lesson 3: Variables and Arithmetic.
Doing math In java.
1 Variables and Arithmetic Operators in JavaScript.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
 Most C programs perform calculations using the C arithmetic operators (Fig. 2.9).  Note the use of various special symbols not used in algebra.  The.
Notes 2.1 Order of Operations PEMDAS If an expression has only numbers and symbols, it is a numerical expression. If it has a variable it is a variable.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
CMSC 104, Version 8/061L16IncrementalProg.ppt Incremental Programming Topics Review of Incremental Programming Example of Incremental Programming Reading.
Introduction to Computing Science and Programming I
Topics Designing a Program Input, Processing, and Output
UMBC CMSC 104 – Section 01, Fall 2016
Thinking Mathematically
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
UMBC CMSC 104 – Section 01, Fall 2016
ICS103 Programming in C Lecture 4: Data Types, Operators & Expressions
Variables and Arithmetic Operators in JavaScript
Computer Science 101 While Statement.
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Lecture 3 Expressions Richard Gesick.
Arithmetic Operators in C
Arithmetic Expressions in C
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Variables and Arithmetic Operators in JavaScript
Incremental Programming
Arithmetic Operators in C
Arithmetic Expressions & Data Conversions
Introduction to C Topics Compilation Using the gcc Compiler
THE COMPUTE STATEMENT Purpose: performs mathematical calculations
CS150 Introduction to Computer Science 1
Data Types and Expressions
Topics Designing a Program Input, Processing, and Output
Introduction to C Topics Compilation Using the gcc Compiler
Topics Designing a Program Input, Processing, and Output
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Variables in C Topics Naming Variables Declaring Variables
Incremental Programming
Data Types and Expressions
Functions, Part 3 of 4 Topics: Coding Practice Reading: None
Arithmetic Expressions & Data Conversions
Data Types and Expressions
Presentation transcript:

CMSC 104, Version 8/061L10ArithmeticOps.ppt Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class Project Incremental Programming Reading Section 2.5

CMSC 104, Version 8/062L10ArithmeticOps.ppt Arithmetic Operators in C Name Operator Example Addition +num1 + num2 Subtraction -initial - spent Multiplication *fathoms * 6 Division /sum / count Modulus %m % n

CMSC 104, Version 8/063L10ArithmeticOps.ppt Division If both operands of a division expression are integers, you will get an integer answer. The fractional portion is thrown away. Examples : 17 / 5 = 3 4 / 3 = 1 35 / 9 = 3

CMSC 104, Version 8/064L10ArithmeticOps.ppt Division (con’t) Division where at least one operand is a floating point number will produce a floating point answer. Examples : 17.0 / 5 = / 3.2 = / 9.1 = What happens? The integer operand is temporarily converted to a floating point, then the division is performed.

CMSC 104, Version 8/065L10ArithmeticOps.ppt Division By Zero Division by zero is mathematically undefined. If you allow division by zero in a program, it will cause a fatal error. Your program will terminate execution and give an error message. Non-fatal errors do not cause program termination, just produce incorrect results.

CMSC 104, Version 8/066L10ArithmeticOps.ppt Modulus The expression m % n yields the integer remainder after m is divided by n. Modulus is an integer operation -- both operands MUST be integers. Examples :17 % 5 = 2 6 % 3 = 0 9 % 2 = 1 5 % 8 = 5

CMSC 104, Version 8/067L10ArithmeticOps.ppt Uses for Modulus Used to determine if an integer value is even or odd 5 % 2 = 1 odd 4 % 2 = 0 even If you take the modulus by 2 of an integer, a result of 1 means the number is odd and a result of 0 means the number is even. The Euclid’s GCD Algorithm (done earlier)

CMSC 104, Version 8/068L10ArithmeticOps.ppt Arithmetic Operators Rules of Operator Precedence Operator(s) Precedence & Associativity ( )Evaluated first. If nested (embedded), innermost first. If on same level, left to right. * / % Evaluated second. If there are several, evaluated left to right. + -Evaluated third. If there are several, evaluated left to right. =Evaluated last, right to left.

CMSC 104, Version 8/069L10ArithmeticOps.ppt Using Parentheses Use parentheses to change the order in which an expression is evaluated. a + b * cWould multiply b * c first, then add a to the result. If you really want the sum of a and b to be multiplied by c, use parentheses to force the evaluation to be done in the order you want. (a + b) * c Also use parentheses to clarify a complex expression.

CMSC 104, Version 8/0610L10ArithmeticOps.ppt Extended Example Given integer variables a, b, c, d, and e, where a = 1, b = 2, c = 3, d = 4, evaluate the following expression: e = b % d / c * b – a e = ( b % d ) / c * b – a e = ( ( b % d ) / c ) * b – a e = ( ( ( b % d ) / c ) * b ) – a e = ( ( ( ( b %d ) / c ) * b ) – a ) e = ( ( ( ( 2 % 4 ) / 3 ) * 2 ) – 1 )

CMSC 104, Version 8/0611L10ArithmeticOps.ppt Extended Example (cont’d) e = ( ( ( ( 2 % 4 ) / 3 ) * 2 ) – 1 ) e = ( ( ( ( 2 ) / 3 ) * 2 ) – 1 ) e = ( ( ( 2 / 3 ) * 2 ) – 1 ) e = ( ( ( 0 ) * 2 ) – 1 ) e = ( ( 0 * 2 ) – 1 ) e = ( ( 0 ) – 1 ) e = ( 0 – 1 ) e = – 1 Note: Always use parenthesis when you have more than two operators!

CMSC 104, Version 8/0612L10ArithmeticOps.ppt Good Programming Practices Always use parenthesis when you have more than two operators!

CMSC 104, Version 8/0613L10ArithmeticOps.ppt Extended Example Given integer variables a, b, c, d, and e, where a = 1, b = 2, c = 3, d = 4, evaluate the following expression: e = b % d / c * b – a e = ( b % d ) / c * b – a e = ( ( b % d ) / c ) * b – a e = ( ( ( b % d ) / c ) * b ) – a e = ( ( ( ( b %d ) / c ) * b ) – a ) e = ( ( ( ( 2 % 4 ) / 3 ) * 2 ) – 1 )

CMSC 104, Version 8/0614L10ArithmeticOps.ppt Another Extended Example Given integer variables a, b, c, d, and e, where a = 1, b = 2, c = 3, d = 4, evaluate the following expression: d = e = 1 + a + b * d % c d = e = 1 + a + ( b * d ) % c d = e = 1 + a + ( ( b * d ) % c ) d = e = ( 1 + a ) + ( ( b * d ) % c ) d = e = ( ( 1 + a ) + ( ( b * d ) % c ) ) d = ( e = ( ( 1 + a ) + ( ( b * d ) % c ) ) ) d = ( e = ( ( ) + ( ( 2 * 4 ) % 3 ) ) )

CMSC 104, Version 8/0615L10ArithmeticOps.ppt Another Extended Example (cont’d) d = ( e = ( ( ) + ( ( 2 * 4 ) % 3 ) ) ) d = ( e = ( ( ) + ( ( 8 ) % 3 ) ) ) d = ( e = ( ( ) + ( 8 % 3 ) ) ) d = ( e = ( ( ) + ( 2 ) ) ) d = ( e = ( ( ) + 2 ) ) d = ( e = ( ( 2 ) + 2 ) ) d = ( e = ( ) ) d = ( e = ( 4 ) ) d = ( e = 4 ) d = 4 /* e is now set to 4 and so is d */

CMSC 104, Version 8/0616L10ArithmeticOps.ppt Practice With Evaluating Expressions Given integer variables a, b, c, d, and e, where a = 1, b = 2, c = 3, d = 4, evaluate the following expressions: a + b - c + d a * b / c 1 + a * b % c a + d % b - c e = b = d + c / b - a

CMSC 104, Version 8/0617L10ArithmeticOps.ppt A Sample Project Let’s write a program that computes and displays the volume and surface area of a box. (I’ll help with prompting the user and displaying the results.) Procedure: o Use the pseudocode that we developed in “Algorithms, Part 3 of 3” o Convert the algorithm to code o Clean up the code (spacing, indentation, commenting)

CMSC 104, Version 8/0618L10ArithmeticOps.ppt The Box - Pseudocode Display “Enter the height: “ Read While ( <= 0 ) Display “The height must be > 0” Display “Enter the height: “ Read End_while

CMSC 104, Version 8/0619L10ArithmeticOps.ppt The Box - Pseudocode (con’t) Display “Enter the width: “ Read While ( <= 0 ) Display “The width must be > 0” Display “Enter the width: “ Read End_while

CMSC 104, Version 8/0620L10ArithmeticOps.ppt The Box - Pseudocode (con’t) Display “Enter the depth: “ Read While ( <= 0 ) Display “The depth must be > 0” Display “Enter the depth: “ Read End_while

CMSC 104, Version 8/0621L10ArithmeticOps.ppt The Box - Pseudocode (con’t) = X X = X = 2 X ( + + )

CMSC 104, Version 8/0622L10ArithmeticOps.ppt The Box - Pseudocode (con’t) Display “Height = “, Display “Width = “, Display “Depth = “, Display “Volume = “, Display “Surface Area = “,

CMSC 104, Version 8/0623L10ArithmeticOps.ppt Good Programming Practice It is best not to take the “big bang” approach to coding. Use an incremental approach by writing your code in incomplete, yet working, pieces. For example, for your projects, o Don’t write the whole program at once. o Just write enough to display the user prompt on the screen. o Get that part working first (compile and run). o Next, write the part that gets the value from the user, and then just print it out.

CMSC 104, Version 8/0624L10ArithmeticOps.ppt Good Programming Practice (con’t) o Get that working (compile and run). o Next, change the code so that you use the value in a calculation and print out the answer. o Get that working (compile and run). o Continue this process until you have the final version. o Get the final version working. Bottom line: Always have a working version of your program!

CMSC 104, Version 8/0625L10ArithmeticOps.ppt Using the Incremental Approach Let’s think about how we could have developed the volume and surface area program incrementally.