1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators
//***************************************************************************** // File name: 02Rectangle.cpp // Author: Chadd Williams // Date: 09/08/06 // Assignment: Lab02 Challenge // Purpose: Calculate and display the area and perimeter of a rectangle based on the // length and width entered by a user. //***************************************************************************** #include "stdafx.h" #include using namespace std; // The main function int main() { /*const*/ int LENGTH /* = 8*/; /*const*/ int WIDTH /* = 3*/; int area; int perimeter; cout << "Please enter the width of the rectangle: "; cin >> WIDTH; cout << "Please enter the length of the rectangle: "; cin >> LENGTH; perimeter = (2 * LENGTH) + (2 * WIDTH); area = LENGTH * WIDTH; cout << "The area of the rectangle is: " << area << ".\n” ; cout << “It was calculated with the formula: area = LENGTH * WIDTH."<<endl; cout << "The perimeter of the rectangle is: " << perimeter << ".” ; cout << “\nIt was calculated with the formula: “;
3 9/08/06CS150 Introduction to Computer Science 1 Today Arithmetic Operators & Expressions o sections 2.15 & 3.2 o Computation o Precedence o Algebra vs C++ o Exponents
4 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators Operators allow us to manipulate data o Unary: operator operand o Binary: operand operator operand OperatorMeaningTypeExample -NegationUnary - 5 =AssignmentBinary rate = 0.05 *MultiplicationBinary cost * rate /DivisionBinary cost / 2 %ModulusBinary cost % 2 +AdditionBinary cost + tax -SubtractionBinary total - tax (left hand side) (right hand side)
5 9/08/06CS150 Introduction to Computer Science 1 Division grade = 100 / 20; o grade = ? grade = 100 / 30; o grade = ?
6 9/08/06CS150 Introduction to Computer Science 1 Division grade = 100 / 40; grade is 2 o If both operands of the division operator are integers, then integer division is performed. the data type of grade is not considered, why? o We say the integer is truncated. Everything after the decimal point is dropped. No rounding. grade = / 40; o grade is 2.5 o What data type should grade be declared as?
7 9/08/06CS150 Introduction to Computer Science 1 Modulus Modulus is the remainder after integer division grade = 100 % 20; o grade = ? grade = 100 % 30; o grade = ? rem = x % n; o What are the possible values for rem ?
8 9/08/06CS150 Introduction to Computer Science 1 Practice What value is assigned to x? o x = 8 + 3; o x = 8 - 3; o x = 8 * 3; o x = 8 % 3; o x = 8 / 3;
9 9/08/06CS150 Introduction to Computer Science 1 Mathematical Expressions Complex mathematical expressions are created by using multiple operators and grouping symbols o expression: programming statement that has value o sum = ; o number = 3; expression In these two examples, we assign the value of an expression to a variable
10 9/08/06CS150 Introduction to Computer Science 1 Examples result = x; result = 4 + result; result = 15 / 3; result = 22 * number; result = a + b % c; result = a + b + d / c – q + 42; cout << “The value: “<< (sum / 2) <<endl;
11 9/08/06CS150 Introduction to Computer Science 1 Operator Precedence result = a + b + d; result = / 3; o result = ? Rules on how to evaluate an arithmetic expression o arithmetic expressions are evaluated left to right o when there are two operators, do them in order of precedence
12 9/08/06CS150 Introduction to Computer Science 1 Operator Precedence Precedence of Arithmetic Operators (Highest to Lowest) (unary negation) - * / % + - If two operators have the same precedence, evaluate them from left to right as they appear in the expression
13 9/08/06CS150 Introduction to Computer Science 1 Practice * 3 10 / 2 -1 * % * 9 / 3 * 4 - 9
14 9/08/06CS150 Introduction to Computer Science 1 Associativity The order in which an operator works with its operands o left to right or right to left OperatorAssociativity (unary negation) –Right to left * / %Left to right + -Left to right
15 9/08/06CS150 Introduction to Computer Science 1 Grouping! To override precedence we use grouping symbols, ( ) o average = ( a + b +c ) / 3; (3 + 12) * % (3 + 9) * 9 / ((3 * 4) – 9) o Work from the inside ( ) outward
16 9/08/06CS150 Introduction to Computer Science 1 Algebraic Expressions 4x + 16 o This means: 4 * x + 16 o In C++, we must always write the * int result; int sum; result = 4sum + 8; // syntax error result = 4 * sum + 8;
17 9/08/06CS150 Introduction to Computer Science 1 Exponents The exponent operator was missing from the list!x 2 y n C++ does not provide an exponent operator as part of the language Use pow() in the cmath library #include double area; area = pow(4, 2); // area = 4 2
18 9/08/06CS150 Introduction to Computer Science 1 pow() pow() is not an operator o it is a function o like main() o double pow(double x, double y) o it takes as arguments two doubles x and y o it produces a double
19 9/08/06CS150 Introduction to Computer Science 1 Practice // Where are the errors? #include double value, exp, result; int number; result = pow(value,exp); 14 = pow(value,2); number = pow(value,exp); result = 14 + pow(value,99);
20 9/08/06CS150 Introduction to Computer Science 1 Advanced Input & Output (3.1 & 3.8) int value, value2; cout << “Please enter an integer: “; cin >> value; cout << “Please enter two integers”; cout << “ separated by some spaces: “; cin >> value >> value2; cout << value2 << value <<endl; Please enter two integers separated by some spaces:
21 9/08/06CS150 Introduction to Computer Science 1 Advanced Input int value; float dec; char letter; cout << “Please enter one integer, “; cout << “one float and one character: ”; cin >> value >> dec >> letter; cout << value << ‘ ‘ << dec << “ “ ; cout << letter << endl; Please enter one integer, one float and one character: c c
22 9/08/06CS150 Introduction to Computer Science 1 Advanced Output How can we force output to look a particular way? o Precision of numbers o Spacing around output Here are some floating point numbers: Here is a table of data: 4 cat
23 9/08/06CS150 Introduction to Computer Science 1 Precision of numbers #include #include //New Library! using namespace std; int main() { double number = ; cout << number << endl; // default output cout << fixed << setprecision(1) << number << endl; cout << fixed << setprecision(2) << number << endl; cout << fixed << setprecision(3) << number << endl; cout << fixed << setprecision(4) << number << endl; return 0; } These numbers are rounded! Explore on your own what happens if number is an integer.
24 9/08/06CS150 Introduction to Computer Science 1 Spacing around output #include #include //New Library! #include using namespace std; int main() { double number = ; string name = “cs150”; int integer = 42; cout << setw(6) << name << setw(6) << integer << endl; cout << setw(6) << fixed << setprecision(3) << number; cout << setw(4) << integer <<endl; return 0; } cs A represents a blank space
25 9/08/06CS150 Introduction to Computer Science 1 Summary Today we have looked at: o Arithmetic Operators & Expressions Next time we will: o ??? Completed sections 2.15 & 3.2