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
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:

Topic 1 CS1A Review P5 Arithmetic in C Chapter 4 in the shrinkwrap Part 1 Announcement Lab 2 Arithmetic Topic 1 P5 Arithmetic 2 1 Arithmetic Operators in C Symbol Function Addition Subtraction Multiplication Division Modulus remainder from integer division Expressions are evaluated from left to right according to the order of precedence Order of Precedence Topic 1 P5 Arithmetic 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 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 How would these expressions be evaluated Order of Precedence num1 10 num2 15 avg num1 num2 2 What is the value of avg 2 Floating Point Expressions When all the operands are floats and the result is placed in a float or output 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 5 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 6 3 Type Coercion Integer Expressions int float float float int int Floating Point Expressions int Only case where MOD is valid float TYPE COERCION When the data type of a value is changed implicitly through mixed mode arithmetic Coerces to a float adds a 0 Mixed Mode Expressions or int float float int or float int int float Coerces to an int truncates decimal float 7 Topic 13 Ch 7 Arithmetic Mixed Mode Arithmetic 2 RECALL We store values in an expression using an assignment statement variable expression Example given the declarations int num1 int num2 int avg Note this value WILL NOT be Rounded THIS IS CALLED TYPE COERCION num1 2 stores the value 2 in num1 num2 7 75 truncates the value and stores 7 in num2 avg num1 num2 2 adds 7 2 divides by 2 stores 4 in result 8 Topic 1 P5 Arithmetic 4 More Examples given the declarations int num1 int num2 float avg NOTE The introduction of any float will cause the expression to convert when the float is evaluated This is called type coercion stores the value 2 in num1 num1 2 truncates the value and stores 3 in num2 num2 3 75 avg num1 num2 2 adds 3 2 divides by 2 stores the float 2 0 in avg Converts to float here num1 2 Converts to float here 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 More examples int inum1 inum2 float fnum3 average inum1 3 inum2 7 75 average inum1 inum2 20 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 How will this differ from average inum1 inum2 20 0 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 Topic 1 P5 Arithmetic 10 5 More Examples Given the declarations int inum1 inum2 float fnum3 average What will the compiler do with these assignment statements inum1 inum2 fnum3 5 0 average inum1 3 3 is a valid integer the value 3 is stored in location inum1 inum2 7 75 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 fnum3 5 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 average inum1 inum2 2 0 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 Topic 1 P5 Arithmetic 11 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 12 6 Assignment Expression assignment expression Storing an expression which has a value into a variable variable expression When you add a semi colon this becomes an expression statement 13 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 14 7 Embedding Assignment Expressions Assignments can also be embedded Example cout num2 10 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 Assignments are expressions NOT statements They can be used anywhere an expression can be used Topic 1 P5 Arithmetic 15 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 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 can cause you needless hours debugging And your friends who help you debug will not appreciate it This makes your code confusing to understand Bad Topic 1 P5 style Arithmetic 16 8 EXAMPLE int in1 int in2 float fn3 in1 in2 fn3 in1 fn3 in2 5 4 8 0 2 cout in1 endl in2 endl fn3 endl in1 fn3 5 4 8 0 2 fn3 20 8 0 2 fn3 2 5 2 2 5 2 5 0 in1 5 in1 fn3 in2 5 4 8 2 cout in1 endl in2 endl fn3 endl Evaluate this one on your own 17 Topic 1 P5 Arithmetic EXAMPLE 2 int in1 in2 float fn3 fn4 in1 in2 fn3 fn4 5 in1 fn3 in2 5 4 8 0 2 if fn4 in1 in2 2 fn3 10 cout fn4 else cout Test val is 10 or less 18 Topic 1 P5 Arithmetic 9 EXERCISE int in1 in2 …


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++ 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?