DOC PREVIEW
Saddleback CS 1B - CS1A Review - Arithmetic in C++

This preview shows page 1-2-3-4-5-6 out of 17 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1 Topic 1 – CS1A Review – P5 - Arithmetic in C++ Chapter 4 in the shrinkwrap Part 1 Announcement  Lab 2 - Arithmetic Topic 1 - P5 - Arithmetic 22 Arithmetic Operators in C++ Expressions are evaluated from left to right according to the order of precedence. Topic 1 - P5 - Arithmetic Symbol Function + Addition - Subtraction * Multiplication / Division % Modulus (remainder from integer division) Order of Precedence () * / % + - Integer Expressions When all of the operands in an expression are integers  i.e. the expression is all integers and the result is placed in an integer or output Order of Precedence () * / % + - Examples: 4 + 8 / 2 = (4 + 8) / 2 = 7 / 3 = 7 % 3 = 12 / 3 * 5 = 10 % 3 – 6 / 2 = 10 % 3.0 = More Examples int avg, num1, num2; num1 = 10; num2 = 15; avg = (num1 + num2)/2; How would these expressions be evaluated? What is the value of avg?3 Floating Point Expressions  When all the operands are floats and the result is placed in a float or output 5 Examples: 5.0 * 2.0 / 4.0 * 3.0 = 5.0 * 2.0 / (4.0 * 2.0) = 5.0 + 2.0 / (4.0 * 2.0) = More Examples float sum, num1, num2; num1 = 10.0; num2 = 15.5; avg = (num1 + num2)/2.0; What is the value of avg? Mixed Mode Arithmetic  Two types of data types to represent numeric values ● int & float ● They store data differently ● Allocate memory differently ◘ i.e. int 6 is stored differently than float 6.0  Mixed mode arithmetic ●  when we combine different data types ● e.g. float & int Topic 1 - P5 - Arithmetic 64 Type Coercion Integer Expressions Floating Point Expressions int = int int float = float float int float Topic 13 - Ch 7 - Arithmetic 7 * / % + - {  TYPE COERCION: When the data type of a value is changed implicitly through mixed-mode arithmetic or or * / + - { Only case where MOD (%) is valid Mixed – Mode Expressions int float float = int float int int = float float { * / + - Coerces to a float (adds a .0) Coerces to an int (truncates decimal) Mixed Mode Arithmetic (2)  RECALL: We store values in an expression using an assignment statement  Example given the declarations: int num1; int num2; int avg; num1 = 2; num2 = 7.75; avg = (num1 + num2) / 2; // adds 7+2 divides by 2 stores 4 in result Topic 1 - P5 - Arithmetic 8 // stores the value 2 in num1 // truncates the value and stores 7 in num2 Note: this value WILL NOT be Rounded THIS IS CALLED TYPE COERCION variable = expression;5 More Examples given the declarations: int num1 int num2; float avg; num1 = 2; num2 = 3.75; avg = (num1 + num2) / 2; // adds 3 + 2 divides by 2 stores the float 2.0 in avg num1 = 2; num2 = 3.75; avg = (num1 + num2) / 2.0; // adds 3 + 2 divides by 2.0  converts to // the float then stores 2.5 in avg 9 Converts to float here Converts to float here This is called type coercion NOTE: The introduction of any float will cause the expression to convert when the float is evaluated stores the value 2 in num1 truncates the value and stores 3 in num2 More examples int inum1, inum2; float fnum3, average; inum1 = 3; inum2 = 7.75; average = (inum1 + inum2) / 20; How will this differ from? average = (inum1 + inum2) / 20.0; Topic 1 - P5 - Arithmetic 10 In this case the result will be 0.0 b/c 20 is an integer the compiler will evaluate these all as integers then store as a float so it will convert to when it assigns the value. In this case the result will be 0.5 b/c 20.0 is a float the compiler will evaluate the addition as integer then it will convert it to float when it divides by 20.0 resulting in 0.5 This is all referred to as mixed mode arithmetic  WARNING: be careful if you are doing this.6 More Examples Given the declarations: int inum1, inum2; float fnum3, average; inum1 = 3; inum2 = 7.75; fnum3 = 5; average = (inum1 + inum2) / 2.0; Topic 1 - P5 - Arithmetic 11 What will the compiler do with these assignment statements? 3 is a valid integer  the value 3 is stored in location inum1 inum2 is an integer so the value 7.75 is truncated (fractional part is cut off)  7 is stored into inum2 Note: the value is not rounded 5 is an integer value  when you insert an integer value into a float it converts it to a float. 5.0 is stored in fnum3 This will add the 2 integers first b/c of the (). Then it takes the total 10 and divides it by 2.0 converting it to floating point  the floating point value of 5.0. is entered into average inum1 inum2 fnum3 average 5.0 Type Casting Assume: int age1, age2, totAge; float avgAge; age1 = 2; age2 = 9; totAge = 2; avgAge = float(age1 + age2) / totAge; ● If would add the values age1 and age2, convert them to the floating point value 11.0 ● then perform the division producing the desired result 5.5. Which of these would produce an accurate result? avgAge = float(age1 + age2) / totAge; avgAge = (age1 + age2) / float(totAge); avgAge = (age1 + age2) / 2.0; avgAge = float( (age1 + age2) / totAge ); 127 Assignment Expression  assignment expression ● Storing an expression which has a value into a variable  When you add a semi-colon this becomes an expression statement 13 variable = expression ; Multiple Assignments  Multiple assignments can be used to set several variables to the same value Example num1 = num2 = num3 = num4 = 0; Topic 1 - P5 - Arithmetic 148 Embedding Assignment Expressions  Assignments can also be embedded Example cout << (num2=10);  Assignments are expressions NOT statements ● They can be used anywhere an expression can be used Topic 1 - P5 - Arithmetic 15 This performs 2 tasks 1. it assigns the value 10 into the variable num2 2. it displays the contents of the variable num2 on the screen Example num2 = 3; num3 = num2 + 5 * (num1 = 7); This statement is evaluated as follows: 1. num1 is assigned the value 7 num3 = num2 + 5 * 7 2. The multiplication is evaluated num3 = num2 + 35 3. The addition is evaluated num3 = 38 Topic 1 - P5 - Arithmetic 16 Two assignment statements were made in the 2nd statement. The value 7 was stored in num1 The value 38 was stored into num3 WARNING Doing this in practice …


View Full Document

Saddleback CS 1B - CS1A Review - Arithmetic in C++

Download CS1A Review - Arithmetic in C++
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view CS1A Review - Arithmetic in C++ and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view CS1A Review - Arithmetic in C++ 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?