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 – /