Computer Science 1620 Arithmetic. C++ Math we have seen how to use numbers in our program to represent data however, we can also manipulate this data.

Slides:



Advertisements
Similar presentations
Types and Arithmetic Operators
Advertisements

CSE202: Lecture 2The Ohio State University1 Variables and C++ Data Types.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
Computer Science 1620 Other Data Types. Quick Review: checklist for performing user input: 1) Be sure variable is declared 2) Prompt the user for input.
Friday, December 08, 2006 “Experience is something you don't get until just after you need it.” - Olivier.
1 CS150 Introduction to Computer Science 1 Arithmetic Operators.
© 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5/e Starting Out with C++: Early Objects 5 th Edition Chapter 2 Introduction.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
Data types and variables
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
Basic Elements of C++ Chapter 2.
Introduction to C++ Programming
COMPUTER SCIENCE I C++ INTRODUCTION
Computer Science 1000 Spreadsheets II Permission to redistribute these slides is strictly prohibited without permission.
Chapter 2 Data Types, Declarations, and Displays.
Objectives You should be able to describe: Data Types
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Operaciones y Variables
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Chapter 2 part #4 Operator
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
LESSON 6 – Arithmetic Operators
Input, Output, and Processing
Chapter 2: Using Data.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Operators in Python. Arithmetic operators Some operators in Python will look familiar (+, -, *, /) Others are new to you (%, //, **) All of these do work.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
CHAPTER 4: CONTROL STRUCTURES - SEQUENCING 10/14/2014 PROBLEM SOLVING & ALGORITHM (DCT 1123)
Week 1 Algorithmization and Programming Languages.
C++ Programming: Basic Elements of C++.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
CSE1222: Lecture 2The Ohio State University1. mathExample2.cpp // math example #include using namespace std; int main() { cout
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
CP104 Introduction to Programming Overview of C Lecture 4__ 1 Assignment Statements An assignment statement is to store a value in a variable variable.
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
Computer Science 1620 boolean. Types so far: Integer char, short, int, long Floating Point float, double, long double String sequence of chars.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
CSC 107 – Programming For Science. Announcements.
Data Types Declarations Expressions Data storage C++ Basics.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Two: Fundamental Data Types Slides by Evan Gallagher.
Doing math In java.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
Dr. Sajib Datta Jan 21,  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height =
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
A Sample Program #include using namespace std; int main(void) { cout
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
Fundamental Programming Fundamental Programming Data Processing and Expressions.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Chapter Topics The Basics of a C++ Program Data Types
Chapter 2: Introduction to C++
Basic Elements of C++.
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Basic Elements of C++ Chapter 2.
Arithmetic Operator Operation Example + addition x + y
2.1 Parts of a C++ Program.
Introduction to C++ Programming
THE COMPUTE STATEMENT Purpose: performs mathematical calculations
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Chapter 2: Introduction to C++.
Arithmetic Operations
Presentation transcript:

Computer Science 1620 Arithmetic

C++ Math we have seen how to use numbers in our program to represent data however, we can also manipulate this data we will look at some basic mathematical operators

Operators there are five arithmetic operators OperatorSymbol Addition+ Subtraction- Multiplication* Division/ Modulus%

Arithmetic Expression to use an arithmetic operator, use the following syntax: numeric expressionoperatornumeric expression A numeric expression is any expression whose value is a number. This is also called an operand. Operator in this case refers to one of the 5 arithmetic operators.

Arithmetic Expression Examples: add 3 and 4 subtract 40 from 75 multiply 36 to 97 divide 49 by 7 numeric expression operatornumeric expression *97 49 /7 In short, exactly the infix notation that you are probably used to (just different symbols).

The statement: in C++ is an expression more specifically, an arithmetic expression the value of the expression is: whatever the formula evaluates to in the above example, the value of the expression is

Examples (from text) ExpressionValue – – * 714

Where are arithmetic expressions used? anywhere an expression is valid we have seen two places so far where an expression is valid: in a cout statement in an arithmetic expression

In a cout statement #include using namespace std; int main() { cout << " " << endl; cout << << endl; return 0; } What will the output of this program be?

Why did we get this output? #include using namespace std; int main() { cout << " " << endl; cout << << endl; return 0; }

Why did we get this output? #include using namespace std; int main() { cout << " " << endl; cout << << endl; return 0; } The first expression is a string literal, because of the quotation marks. Remember that the value of a string literal is the text between its quotation marks

Why did we get this output? #include using namespace std; int main() { cout << " " << endl; cout << << endl; return 0; } The second expression is an arithmetic expression. Remember that the value of an arithmetic expression is the value of the formula.

Quick Note: More than one type of expression can be sent in the same cout command must be separated by << operator #include using namespace std; int main() { cout << 3 << "+" << 4 << "=" << 3+4 << endl; return 0; } IntegerString IntegerStringArith. Exp.

Integer Division what is the output of the following program? #include using namespace std; int main() { cout << 5.0 / 2.0 << endl; cout << 5 / 2 << endl; return 0; }

Integer Division 5 / 2 = 2 (the quotient) when two integers are divided, the result is an integer when a fraction (or remainder) occurs, it is simply ignored note that this ONLY APPLIES TO INTEGERS doubles are computed as expected

Examples: EquationResult 14 / 7 -5 / 2 -7 / / /

Modulus the remainder of the division operation in mathematics, if we divide 34 by 5, we get: r 4 Quotient: Remainder: in C++ integer division gives us the quotient modulus gives us the remainder

Modulus #include using namespace std; int main() { cout << 34 / 5 << endl; cout << 34 % 5 << endl; return 0; }

Modulus has many uses in programming extracting digits determining a prime number hashing etc we will see modulus again when we reach conditionals

Embedded Arithmetic Expressions recall the format of an arithmetic expression recall that a numeric expression is an expression whose value is a number since an arithmetic expression's value is a number, we can use it in another arithmetic expression numeric expression operatornumeric expression

Embedded Arithmetic Expressions recall the format of an arithmetic expression recall that a numeric expression is an expression whose value is a number since an arithmetic expression's value is a number, we can use it in another arithmetic expression numeric expression operatornumeric expression numeric expression operatornumeric expression

Embedded Arithmetic Expressions previous slide formally demonstrates that C++ can handle expressions with more than one operator important: this is nothing special, just one binary expression embedded in another each binary expression is executed individually cout << << endl; This expression is evaluated first Its value becomes the operand for the second expression.

Order and Precedence there are two possible orders of computation in the previous expression Order 1: Order 2: Which one does C++ use?

Order and Precedence there are two possible orders of computation in the previous equation Order 1: Rule: arithmetic expressions are evaluated left to right when their operand is the same

Order and Precedence the order of evaluation becomes important for the – and / operators Order 1: Order 2:

Order and Precedence what about when the operators are not the same? * 4 3 * 4 12 Order 1: * Order 2: Which one does C++ use?

Order and Precedence what about when the operators are not the same? * Order 2: RULE: When different operands are used, precedence rules take over. *, /, % are evaluated before any +, - operations

Order and Precedence what about when the operators have the same precedence? Order 1: Order 2: Which one does C++ use?

Order and Precedence what about when the operators have the same precedence? Order 1: RULE: When two arithmetic operands have the same precendence, they are evaluated left to right.

Order and Precedence what happens if I want the addition performed first? that is, parentheses override any precedence rule * 4 (1 + 2) * 4

Examples: EquationResult * 5 (3 + 4) * 5 8 / 2 * 4 8 / (2 * 4) 19 / 20 *

Mixed Mode Expressions when two integers are used in an arith. exp., the value of the operation is an integer as well = 7 when two doubles are used in an arith. exp., the value of the operation is a double as well = 7.0 what happens when an integer and a double are used in an operation? = ????? 7 or 7.0?

Rules for Mixed Mode Arithmetic: (text) if the operator has the same types, then the value of the expression has the same type if one of the operands is a floating point number and the other an integer, then the integer is promoted to a floating point number. The value of the expression is a floating point number the precedence rules from before still apply

Evaluating mixed-mode expressions the rule is applied for each operation individually the type of the value of each expression must be remembered order of operations: usual precedence and left to right rules

Examples: EquationResult 3 / / / * / 5 – /