Unit 3, Lesson 3 Math Operations, Assignment Operators, and Arithmetic Operators Mr. Dave Clausen La Cañada High School

Slides:



Advertisements
Similar presentations
Chapter 2 Part B CISS 241. Take a few moments and work with another student to develop an algorithm for a program that will add two whole numbers (integers)
Advertisements

CS 6301 Lecture 2: First Program1. CS Topics of this lecture Introduce first program  Explore inputs and outputs of a program Arithmetic using.
Lecture 1 The Basics (Review of Familiar Topics).
Expressions An expression is a sequence of operands and operators that reduces to a single value expression operator operand An operator is a language-specific.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Basic Elements of C++ Chapter 2.
Introduction to C++ Programming
 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
Variable Declaration  It is possible to declare multiple variables of the same data type on the same line.  Ex. double hours, rate, total;  Variables.
Chapter 3: Arithmetic,Variables, Input, Constants, & Library Functions Mr. Dave Clausen La Cañada High School.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
CHAPTER 4: CONTROL STRUCTURES - SEQUENCING 10/14/2014 PROBLEM SOLVING & ALGORITHM (DCT 1123)
C++ Programming: Basic Elements of C++.
CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.
CPS120: Introduction to Computer Science Operations Lecture 9.
Chapter 2 Overview of C++. 2 Overview  2.1 Language Elements  2.2 Reserved Words & Identifiers  2.3 Data Types & Declarations  2.4 Input/Output 
CSC 107 – Programming For Science. The Week’s Goal.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Lecture #6 OPERATORS AND ITS TYPES By Shahid Naseem (Lecturer)
Chapter 3: Assignment, Formatting, and Interactive Input.
CSC 107 – Programming For Science. Announcements.
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
Mathematical Calculations in Java Mrs. C. Furman.
Unit 3 Lesson 6 Input and Output Streams with modifications by Mr. Dave Clausen.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Cosc175/operators1 Algorithms computer as the tool process – algorithm –Arithmetic: addition,subtraction,multiplication,division –Save information for.
Arithmetic Expressions Addition (+) Subtraction (-) Multiplication (*) Division (/) –Integer –Real Number Mod Operator (%) Same as regular Depends on the.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Chapter 3 – Variables and Arithmetic Operations. First Program – volume of a box /************************************************************/ /* Program.
CS201 Introduction to Sabancı University 1 Chapter 2 Writing and Understanding C++ l Writing programs in any language requires understanding.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
Chapter 02 (Part II) Introduction to C++ Programming.
A Sample Program #include using namespace std; int main(void) { cout
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
Chapter 3 Math Operations. Objectives Use the assignment and arithmetic operators. Use operators in output statements. Explain the problem with division.
Bill Tucker Austin Community College COSC 1315
Chapter Topics The Basics of a C++ Program Data Types
Data Types and Expressions
Data Types and Expressions
Expressions.
INSPIRING CREATIVE AND INNOVATIVE MINDS
Basic Elements of C++.
Chapter 2 Assignment and Interactive Input
Introduction to C++ October 2, 2017.
Basic Elements of C++ Chapter 2.
Arithmetic Operator Operation Example + addition x + y
Building Java Programs Chapter 2
Mr. Dave Clausen La Cañada High School
Lecture 3 Expressions Richard Gesick.
Introduction to C++ Programming
CS150 Introduction to Computer Science 1
Algorithms computer as the tool process – algorithm
CS150 Introduction to Computer Science 1
Data Types and Expressions
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Primitive Types and Expressions
C++ for Engineers and Scientists Second Edition
Chapter 1 c++ structure C++ Input / Output
Data Types and Expressions
Data Types and Expressions
Presentation transcript:

Unit 3, Lesson 3 Math Operations, Assignment Operators, and Arithmetic Operators Mr. Dave Clausen La Cañada High School

Mr. Dave Clausen2 Assignment Operator You have used the assignment operator (=) to initialize variables, so you already know most of what there is to know about the assignment operatorYou have used the assignment operator (=) to initialize variables, so you already know most of what there is to know about the assignment operator The assignment operator changes the value of the variable to the left of the operator.The assignment operator changes the value of the variable to the left of the operator. For example:For example: i = 25; //changes the value of i to 25

Mr. Dave Clausen3 Code List 3-1 #include #include //iassign.cpp iassign.txt iassign.cppiassign.txtiassign.cppiassign.txt //declaring and initializing variables in two steps int main () { int i;// declare i as an integer int i;// declare i as an integer i=1000; // assign the value 1000 to i i=1000; // assign the value 1000 to i cout << i << endl; cout << i << endl; i=25;// assign the value 25 to i i=25;// assign the value 25 to i cout << i << endl; cout << i << endl; return 0; return 0;}

Mr. Dave Clausen4 Code List 3-2 #include #include //multint.cpp multint.txt multint.cppmultint.txtmultint.cppmultint.txt //declaring multiple variables in one step //initializing multiple variables in one step int main () { int i, j, k; // declare i, j, and k as integers int i, j, k; // declare i, j, and k as integers i =j=k=10; //initialize all of the variables to 10 i =j=k=10; //initialize all of the variables to 10 cout << i << ‘\n’; cout << i << ‘\n’; cout << j << ‘\n’; cout << j << ‘\n’; cout << k << ‘\n’; cout << k << ‘\n’; return 0; return 0;}

Mr. Dave Clausen5 Declare & Initialize Constants MUST be declared and initialized in the same statement.Constants MUST be declared and initialized in the same statement. Variables MAY be declared and initialized in the same statement.Variables MAY be declared and initialized in the same statement. For example:For example: int number = 10; double result = 0.0; char letter = ‘ ‘; // a space

Mr. Dave Clausen6 Arithmetic Operators A specific set of arithmetic operators is used to perform calculations in C++A specific set of arithmetic operators is used to perform calculations in C++ These arithmetic operators, shown in Table 3-1, may be somewhat familiar to youThese arithmetic operators, shown in Table 3-1, may be somewhat familiar to you Addition and subtraction and performed with the familiar + and - operatorsAddition and subtraction and performed with the familiar + and - operators

Mr. Dave Clausen7 Table 3-1 The arithmetic operators are used with two operands, as in the examples in Table 3-1

Mr. Dave Clausen8 Using Arithmetic Operators Arithmetic operators are most often used on the right of an assignment operator as shown in the examples in Table 3-2

Mr. Dave Clausen9 Assignment Operator vs. The Equal Sign The assignment operator (=) functions differently in C++ from the way the equal sign functions in Algebra.The assignment operator (=) functions differently in C++ from the way the equal sign functions in Algebra. Look at the following statement:Look at the following statement: x = x + 10; This statement is False in Algebra.This statement is False in Algebra. In C++, the old value of x is incremented by 10 and then assigned to the new value of x.In C++, the old value of x is incremented by 10 and then assigned to the new value of x.

Mr. Dave Clausen10 Code List 3-3 // assign.cpp assign.txt assign.cppassign.txtassign.cppassign.txt #include #include int main () { int i = 2; //declare and initialize in one step int j = 3; //declare and initialize in one step int k = 4; //declare and initialize in one step int l; float a = 0.5; //declare and initialize in one step float b = 3.0; //declare and initialize in one step float c; 1 = i + 2; cout << 1 << ‘\n’; 1 = 1- j; cout << 1 << ‘\n’; 1 = i * j * k; cout << 1 << ‘\n’; 1 = k / i; cout << 1<< ‘\n’; c = b * a; c = b * a; cout << c << '\n'; cout << c << '\n'; c = b / a; c = b / a; cout << c << '\n'; cout << c << '\n'; getch(); getch(); return 0; return 0;}

Mr. Dave Clausen11 Arithmetic operators are used to create expressions Expression Result of expression cost = price + tax cost is assigned the value of price plus tax owed = total – discount owed is assigned the value of total minus discount area = l * w area is assigned the value of l times w one_eighth = 1 / 8 one_eighth is assigned the value of 1 divided by 8 r = 5 % 2 r is assigned the integer remainder of 5 divided by 2 by using the modulus operator x = -y x is assigned the value of -y

Mr. Dave Clausen12 Assignment Statements A Method of putting values into memory locationsA Method of putting values into memory locations  variable name = value;  a variable name = expression; Assignment is made from right to leftAssignment is made from right to left Constants can’t be on left side of statementConstants can’t be on left side of statement Expression is a constant or variable or combination thereofExpression is a constant or variable or combination thereof

Mr. Dave Clausen13 Assignment Statements Values on right side not normally changedValues on right side not normally changed Variable and expression must be of compatible data types (more later)Variable and expression must be of compatible data types (more later) Previous value of variable discarded to make room for the new valuePrevious value of variable discarded to make room for the new value For now char, int, and double are compatible with each otherFor now char, int, and double are compatible with each other

Mr. Dave Clausen14 Assignment Examples score1 = 72.3;score1 = 72.3; score2 = 89.4;score2 = 89.4; score3 = 95.6;score3 = 95.6; average = (score1 + score2 + score3) / 3.0average = (score1 + score2 + score3) / 3.0  why not divide by 3 instead of 3.0?

Mr. Dave Clausen15 The Modulus operator The Modulus Operator, which can be used only for integer division, returns the remainder rather than the quotient.The Modulus Operator, which can be used only for integer division, returns the remainder rather than the quotient. As shown in figure 3-1, integer division is similar to the way you divide manuallyAs shown in figure 3-1, integer division is similar to the way you divide manually

Mr. Dave Clausen16 Code List 3-4 // remain.cpp remain.txt remain.cppremain.txtremain.cppremain.txt // Calculations using assignment statements #include #include int main () { int dividend, divisor, quotient, remainder; / / Input cout << “Enter the dividend “; cin >> dividend; cout << “Enter the divisor “; cin >> divisor; / / Calculations quotient = dividend / divisor; remainder = dividend % divisor; / / Output cout << “the quotient is “ << quotient; cout << “ with a remainder of “ << remainder << ‘\n’; return 0; }

Mr. Dave Clausen17 Code List 3-5 // remain2.cpp remain2.txt remain2.cppremain2.txtremain2.cppremain2.txt //calculations in cout statements - while it can be done, please DON’T DO THIS #include #include int main() { int dividend, divisor; // Input cout << “enter the dividend”; cin >> dividend; cout << “Enter the divisor”; cin >> divisor; // Calculations in the Output Don’t Do This For My Class cout << “the quotient is “ << dividend/divisor; cout << “with a remainder of “ << dividend % divisor << ‘\n’; return 0; }

Mr. Dave Clausen18 Incrementing and Decrementing Adding or subtracting 1 from a variable is very common in programs. Adding 1 to a variable is called incrementing, and subtracting 1 from a variable is called decrementing.Adding or subtracting 1 from a variable is very common in programs. Adding 1 to a variable is called incrementing, and subtracting 1 from a variable is called decrementing.

Mr. Dave Clausen19 The ++ and -- Operators C++ provides operators for incrementing and decrementing. In C++, you can increment an integer variable using the ++ operator, and decrement using the -- operator, as shown in Table 3-3C++ provides operators for incrementing and decrementing. In C++, you can increment an integer variable using the ++ operator, and decrement using the -- operator, as shown in Table 3-3

Mr. Dave Clausen20 Code List 3-6 // inc_dec.cpp inc_dec.txtinc_dec.cppinc_dec.txt #include int main() { int j;// declare j as int j=1;// initialize j to 1 cout << “j=“ << j << ‘\n’; j++;//increment j cout << “j=“ << j << ‘\n’; j--;//decrement j cout << “j=“ << j << ‘\n’; return 0; }

Mr. Dave Clausen21 Variations of Increment and Decrement At first glance, the ++ and – operators seem very simple. But there are two ways that each of these operators can be used. The operators can be placed either before or after the variable. The location of the operators affects the way they work. c++ or ++c c- - or - - c We will use c++ and c- - for this class

Mr. Dave Clausen22 Order of Operations You may recall from your math classes the rules related to the order in which operations are performed. These rules are called the order of operations. The C++ compiler uses a similar set of rules for its calculations. Calculations are processed in the following orderYou may recall from your math classes the rules related to the order in which operations are performed. These rules are called the order of operations. The C++ compiler uses a similar set of rules for its calculations. Calculations are processed in the following order Minus sign used to change sign (-)Minus sign used to change sign (-) Multiplication and division (*/%)Multiplication and division (*/%) Addition and subtraction (+ -)Addition and subtraction (+ -) C++ lets you use parentheses to change the order of operations. For example, consider the two statements in Figure 3-2.

Mr. Dave Clausen23 Code List 3-7 / / order.cpp order.txt order.cpporder.txt order.cpporder.txt #inlcude #inlcude int main () { int answer answer = * 2 + 3; cout << answer << ‘\n’; answer = (1 + 2) * (2 + 3); cout << answer << ‘\n’; answer = * (2 + 3); cout << answer << ‘\n’; answer = (1 +2) * ; cout << answer << ‘\n’; return 0; }

Mr. Dave Clausen24 Integer Arithmetic +Addition+Addition -Subtraction-Subtraction *Multiplication*Multiplication /Quotient (Integer Division)/Quotient (Integer Division) %Remainder (Modulus)%Remainder (Modulus)

Mr. Dave Clausen25 Integer Order Of Operations Expressions within parenthesesExpressions within parentheses  nested parentheses: from inside out * (multiplication), % (modulus), / (division)* (multiplication), % (modulus), / (division)  from left to right + (addition), - (subtraction)+ (addition), - (subtraction)  from left to right

Mr. Dave Clausen26 Integer Arithmetic (Examples) (3-4)*5 = 3 * (-2) = 3 * (-2) = 17 / 3 = 17 / 3 = 17 % 3 = 17 % 3 = 17 / (-3) = 17 / (-3) = -17 % 7 = -17 % 7 =-42+50%17=

Mr. Dave Clausen27 Real Number Arithmetic Type double:Type double: +Addition+Addition -Subtraction-Subtraction *Multiplication*Multiplication /Division/Division

Mr. Dave Clausen28 Real Number Order Of Operations Expressions within parenthesesExpressions within parentheses  nested parentheses: from inside out * (multiplication), / (division)* (multiplication), / (division)  from left to right + (addition), - (subtraction)+ (addition), - (subtraction)  from left to right

Mr. Dave Clausen29 Real Number Arithmetic (Examples) 2.0 * ( ) = 2.0 * = / ( ) = 3.1 * 2.0 = / =

Mr. Dave Clausen30 Compound Assignments “Short hand” notation for frequently used assignments (We will not use these for readability of our programs.)“Short hand” notation for frequently used assignments (We will not use these for readability of our programs.) Short hand x += y x -= y x *= y x /= y x %= y Longer form x = x + y x = x - y x = x * y x = x / y x = x % y

Mr. Dave Clausen31 Sample Program Here is a program that prints data about the cost of three textbooks and calculates the average price of the books: Books.cpp Books.txt

Mr. Dave Clausen32 Input Cin (pronounced see-in)Cin (pronounced see-in)  gets data from keyboard, the standard input stream  extractor operator >> obtain input from standard input stream and direct it to a variable (extract from stream to variable)obtain input from standard input stream and direct it to a variable (extract from stream to variable)  inserter operator << insert data into standard output streaminsert data into standard output stream  EGG ILL Extractor Greater Greater, Inserter Less LessExtractor Greater Greater, Inserter Less Less

Mr. Dave Clausen33 Input Data read in from keyboard must match the type of variable used to store dataData read in from keyboard must match the type of variable used to store data Interactive InputInteractive Input  enter values from keyboard while the program is running  cin causes the program to stop and wait for the user to enter data from the keyboard  prompt the user for the data (user friendly)

Mr. Dave Clausen34 Input: Sample Programs No prompt for any of the data values: input.cpp input.cpp input.txt input.txt input.cppinput.txt One prompt for each data value (preferred) triples.cpp triples.cpp triples.txt triples.txt triples.cpptriples.txt