Arithmetic Operations. Review function statement input/output comment #include data type variable identifier constant declaration.

Slides:



Advertisements
Similar presentations
© 2007 Lawrenceville Press Slide 1 Assignment Statement An assignment statement gives a value to a variable. Assignment can take several forms: x = 5;
Advertisements

Computer Programming w/ Eng. Applications
Types and Arithmetic Operators
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Javascript Essentials How do I write it??  Start Homesite  Between the start and end BODY tags type: 
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
1 CS150 Introduction to Computer Science 1 Arithmetic Operators.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
Introduction to Computers and Programming - Class 2 1 Introduction to Computers and Programming Class 2 Introduction to C Professor Avi Rosenfeld.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Variables, Assignment & Math Storing and naming data.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
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.
CHAPTER 4: CONTROL STRUCTURES - SEQUENCING 10/14/2014 PROBLEM SOLVING & ALGORITHM (DCT 1123)
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
CP104 Introduction to Programming Overview of C Lecture 4__ 1 Assignment Statements An assignment statement is to store a value in a variable variable.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
CHAPTER 2 COMPONENTS OF A PROGRAMMING LANGUAGE I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
Data Types Declarations Expressions Data storage C++ Basics.
CHAPTER 2 C++ SYNTAX & SEMANTICS #include using namespace std; int main() { cout
1 CS 1430: Programming in C++ Turn in your Quiz1-1.
Arithmetic Expressions in C++. CSCE 1062 Outline Data declaration {section 2.3} Arithmetic operators in C++ {section 2.6} Mixed data type arithmetic in.
This will all add up in the end. Assignment operator =Simple Assignment operator Arithmetic Operators +Additive operator – Subtraction operator * Multiplication.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
Doing math In java.
C++ for Engineers and Scientists Second Edition
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 3 – Variables and Arithmetic Operations. First Program – volume of a box /************************************************************/ /* Program.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
ICS102 Lecture 1 : Expressions and Assignment King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer.
Arithmetic Operations (L05) * Arithmetic Operations * Variables * Declaration Statement * Software Development Procedure Problem Solving Using C Dr. Ming.
Chapter 02 (Part II) Introduction to C++ Programming.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
What will each of the following lines print? System.out.println("number" ); number645 System.out.println("number" + (6 + 4)+ 5); number105 System.out.println(6.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
CSE 220 – C Programming Expressions.
Variables Mr. Crone.
BASIC ELEMENTS OF A COMPUTER PROGRAM
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
ITEC113 Algorithms and Programming Techniques
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.
Assignment statement and Arithmetic operation 2
Java Programming: From Problem Analysis to Program Design, 4e
C++ for Engineers and Scientists Second Edition
Lecture 3 Expressions Richard Gesick.
Introduction to C++ Programming
Programming Funamental slides
THE COMPUTE STATEMENT Purpose: performs mathematical calculations
Alternate Version of STARTING OUT WITH C++ 4th Edition
CS150 Introduction to Computer Science 1
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.
Chapter 2: Introduction to C++.
Data Types and Expressions
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Data Types and Expressions
Data Types and Expressions
Presentation transcript:

Arithmetic Operations

Review function statement input/output comment #include data type variable identifier constant declaration

Declaration Statements Variable: a memory location to store data Variable value: the content in the location Identifier: the symbolic name of a variable Data Type: the type of data the variable is for A declaration tells the compiler to allocate enough memory to hold a value of this data type and to associate the identifier with this location 3 string name; string firstName, lastName; int num = 10; DataType Identifier, Identifier, … ;

Add Two Numbers In Math S = x + y Or x + y = S 5 How to add two numbers in C++?

Assignment Operator: = num1 + num2 = sum; // Valid in C++? // No Assignment statement MUST be from right to left sum = num1 + num2; // Correct! // Not “equal” Assignment statement: variable = expression; 6

Are the two statements the same? Sum = Num1 + Num2; Sum = num1 + num2; // NO! // C++ is case sensitive! 7

Arithmetic Operators Addition: + to compute Sum Subtraction: - to compute Difference Multiplication: * to compute Product Division: / to compute Quotient Modulus: % to compute the remainder from integer division You MUST use operators to compute in C++! 8

Arithmetic Operators What is ** in C++? result = x ** y; // Not good! What is ^ in C++? result = x ^ y; // Not good! S = x ( y + z); //Is it good in C++? //Missing operator! S = x * ( y + z); // Good! 9

Unary Operator Unary operator: operator that has only one operand validCount ++; // validCount = validCount + 1; totalCount --; // totalCount = totalCount - 1; Binary operator: operator that has two operands num1 + num2

Semantics, Syntax, and Style total = total + 2; Semantics Increase total by 2 Assignment (right to left) Not equal Syntax Statement terminator Case sensitive Style one space before and after any operator meaningful name 11

Precedence Rules From high to low: ( ) *, /, % +, - 12

Examples x = * 2; // result: x = (8 - 4) * 2; // result: y = 8 / 4 * 2; // result: y = 8 / (4 * 2); // result: 13

More Examples z = 8 / 4 ^ 2; // No! z = 8 / 4 ** 4; // No! z = 8 / 4 * 4 // Result: ? X = 5(3 – 7); //Result: ? z = 5 / (2 * 5); // Result: 0.5 or 0? 14

Integer Division vs. Float Division In Math 5 is almost always the same as 5.0 In C++ 5 is almost never the same as 5.0 Why? Store value in computer as bits, bytes. 5 is stored as an integer 5.0 is stored as a float number 15

Integer Division We can only get integers! 7 / 5 Quotient: 1 Remainder: 2 5 / 7 Quotient: 0 Remainder: 5 16

Float Division We get float result! 7.0 / 5.0 Quotient: / 5.0 (Same) 7.0 / 5 (Same) 5.0 / 7 Quotient: … 17 As long as there is a float number in the expression, the result is a float number.

18 The Cast Functions int num1, num2; float quotient; cin >> num1 >> num2; quotient = num1 / num2; // what’s the value of quotient? // How to get float quotient? quotient = float(num1) / num2; // Use cast function float(variable) quotient = float(num1 / num2); // Integer division or float division? // Integer division!

Quotient (Division) Operator: / Expression 7 / 5 5 / 7 14 / 5 5 / / / Result

Remainder (Modular) Operator: % Expression 7 % 5 5 % 7 14 % 5 5 % % % Result

Exercises Expression Result 12 % 20 * 3 (12 % 20) * 3 12 * 3 20 % 12 * 3 12 / 20 * 3 20 / 12 * 3 20 / 12 % 3 20 % 12 % 3 21

Why Integer Division? Get $143 from ATM 143 / 50 Number of $50 bills: % 50 Amount left after $50 bills: / 10 Number of $10 bills: 4 43 % 10 Amount left after $10 bills: 3 Number of $1 bills: 3 22 after class exercise: write this solution into a C++ program

Summary Assignment Operator total = total + num1; // from right to left Arithmetic Operators and Precedence Rules () /, %, * -, + Semantics, Syntax, and Style 23