Download presentation
Presentation is loading. Please wait.
Published byLeah Reed Modified over 11 years ago
1
Lecture 5 -- 1Computer Science I - Martin Hardwick Arithmetic Expressions With Integers rOperators:result is always an integer SymbolNameExampleValue (x = 10, y=3) +addition x + y13 –subtractionx – y 7 *multiplicationx * y30 /quotient x / y 3 %remainderx % y 1 –unary minus –x-10 +unary plus +x 10 rYou can string the operators together to build longer expressions. l use parentheses to specify order of operations l precedence (after sub-expressions in parentheses) –first:unary plus and minus from right to left –second: *, / and % from left to right –third: + and – from left to right
2
Lecture 5 -- 2Computer Science I - Martin Hardwick Arithmetic Expression Examples 12 + 2 * 5 - 8 / 3 + 6 10 2 22 20 26 -2 * 3 * (4 + 5) % 10 + -3 -3 -2 9 -6 -54 -4 -7
3
Lecture 5 -- 3Computer Science I - Martin Hardwick Arithmetic Exprs With Doubles rArithmetic expressions with real numbers (numbers with decimal points) work the same way as with integers, with a few exceptions: l there is no remainder operator (%) l the / operator means divide, computing the answer to many decimal places l the result is a real value rather than an integer value rImportant: Real values are approximate and may contain errors in the last few digits. l about 7 digits of accuracy for type float (one word) l about 14 digits of accuracy for type double (two words) l so programmers today nearly always use doubles rReal values are often represented using scientific notation. l Example: 1.857409 x 10 2
4
Lecture 5 -- 4Computer Science I - Martin Hardwick Mixed Mode Arithmetic Expressions rArithmetic expressions both integers and doubles can get tricky. l if both operands are integers, integer arithmetic is used l if either operand is a double, double arithmetic is used –an integer operand is converted to double for the operation rExample:assume a and b are int variables assume x and y are double variables (a * b + (a - b) * x) / (a + b) int double
5
Lecture 5 -- 5Computer Science I - Martin Hardwick Roots Of A Quadratic Equation //Find roots of Ax 2 + Bx + C = 0 #include using namespace std; int main () { doubleA, B, C, x1, x2, sqrtOfDiscriminant; //get three inputs cout << "Enter the value for A: "; cin >> A; cout << "Enter the value for B: "; cin >> B; cout << "Enter the value for C: "; cin >> C; rThe roots of a quadratic equation Ax 2 + Bx + C = 0 are: rTo compute the roots, we need to ask the user for the values of A, B and C. l the cin statement does this l prompt user to enter data rNote that you can declare more than one variable per line. rThe C++ math library (cmath) contains a square root function. -B ± B 2 - 4AC 2A
6
Lecture 5 -- 6Computer Science I - Martin Hardwick Roots Of A Quadratic Equation (continued) //compute the two solutions sqrtOfDiscriminant = sqrt(B*B- 4*A*C); x1 = (-B+sqrtOfDiscriminant) / (2*A); x2 = (-B-sqrtOfDiscriminant) / (2*A); //display the results cout << "First solution: " << x1 << endl; cout << "Second solution: " << x2 << endl; return 0; } rsqrt is the name of the square root function in the library resource. rWhen a variable appears after the << operator in a cout statement, the value of the variable is displayed. rNote that the parentheses in the computation of x1 and x2 are needed to produce the correct result.
7
Lecture 5 -- 7Computer Science I - Martin Hardwick Common errors rNot initializing a variable before it is used // Author: Martin Hardwick #include using namespace std; int main () { int a, b; a = b + 6; return 0; }
8
Lecture 5 -- 8Computer Science I - Martin Hardwick Increment and Decrement operators rThese operators are an easy way to increase (++) or decrease (--) a value by one. rUsed a lot in For loops int main () { int x, y; x = 5; y = x++; cout << x << y << endl; return 0; } int main () { int x, y; x = 5; y = ++x; cout << x << y << endl; return 0; } Prints 6, 5Prints 6, 6 Post-incrementPre-increment
9
Lecture 5 -- 9Computer Science I - Martin Hardwick Conditional Execution -- Quadratic Formula //Find roots of Ax 2 + Bx + C = 0 #include using namespace std; int main () { doubleA, B, C, x1, x2; doublesqrtOfDiscriminant; //get three inputs cout << "Enter the value for A: "; cin >> A; cout << "Enter the value for B: "; cin >> B; cout << "Enter the value for C: "; cin >> C; rA quadratic equation may have only one root. l the quadratic formula computes this root twice l only want to display the root once rOur program needs some way to check that the two roots are different before displaying both. l this is the purpose of the IF statement in C++ rIF statements are also useful for verifying that the user entered correct data as will be seen in future classes.
10
Lecture 5 -- 10Computer Science I - Martin Hardwick Quadratic Formula (continue) rWe need to create a special case that displays the second root only when it is different from the first. rAn IF statement specifies: l conditions that define the special case l sequence of statements to execute for the special case r!= means not equal rthe braces (i.e., { }) define a block of statements to execute. //compute the two solutions sqrtOfDiscriminant = sqrt( B*B - 4*A*C ); x1 = (-B+sqrtOfDiscriminant)/(2*A); x2 = (-B-sqrtOfDiscriminant)/(2*A); //display the first root cout << "First solution: " << x1 << endl; //display the second root if different if (x1 != x2) { cout << "Second solution: " << x2 << endl; } return 0; }
11
Lecture 5 -- 11Computer Science I - Martin Hardwick IF Statements rSyntax:if ( condition ) { statement1; statement 2;... } rThe condition is any valid logical expression with relational operators and logical operators (more on this later). rThe statements inside the IF statement can be any valid C++ statements including other IF statements. rMeaning: condition statement1; statement2;... true false Handle one special case
12
Lecture 5 -- 12Computer Science I - Martin Hardwick The Condition In An IF Statement rThe condition in an IF statement is any valid logical expression composed of relational and logical operators. rRelational operators: operatordescriptionexampleresult x = 10, y=2 <less thanx < yfalse >greater thanx > y+5true <=less or equalx/2 <= yfalse >=greater or equalx/5 >= ytrue ==equalx-1 == yfalse !=not equalx != ytrue rThe result of a relational operator is either true or false
13
Lecture 5 -- 13Computer Science I - Martin Hardwick The Condition In An IF Statement (continued) rLogical operators: ABorandnot A || BA && B!A falsefalsefalsefalsetrue falsetruetruefalsetrue truefalsetruefalsefalse truetruetruetruefalse rPrecedence for a logical expression: l expressions in parentheses l arithmetic expressions l relational operators l and l or in order from left to right unless otherwise specified
14
Lecture 5 -- 14Computer Science I - Martin Hardwick Example Conditions ( x = 10, y = 20) (x+1 > 0) || (y == x) && (x+y < 25)!x + 1 > 0 || y == x && x + y < 25 (11 > 0) TRUE (20 = = 10) FALSE (30 < 25) FALSE FALSE && FALSE FALSE TRUE || FALSE TRUE (11 > 0) (20 = = 10) (30 < 25) TRUE FALSE
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.