Programming Techniques I SCJ1013 Arithmetic Expression
Transcript of Programming Techniques I SCJ1013 Arithmetic Expression
![Page 1: Programming Techniques I SCJ1013 Arithmetic Expression](https://reader030.fdocuments.in/reader030/viewer/2022012808/61bded8baeafde68f372274c/html5/thumbnails/1.jpg)
Arithmetic Expression
Programming Techniques ISCJ1013
Dr Masitah Ghazali
![Page 2: Programming Techniques I SCJ1013 Arithmetic Expression](https://reader030.fdocuments.in/reader030/viewer/2022012808/61bded8baeafde68f372274c/html5/thumbnails/2.jpg)
The cin Object
![Page 3: Programming Techniques I SCJ1013 Arithmetic Expression](https://reader030.fdocuments.in/reader030/viewer/2022012808/61bded8baeafde68f372274c/html5/thumbnails/3.jpg)
The cin Object
• Standard input object• Like cout, requires iostream file• Used to read input from keyboard• Information retrieved from cin with >>• Input is stored in one or more variables
• Standard input object• Like cout, requires iostream file• Used to read input from keyboard• Information retrieved from cin with >>• Input is stored in one or more variables
![Page 4: Programming Techniques I SCJ1013 Arithmetic Expression](https://reader030.fdocuments.in/reader030/viewer/2022012808/61bded8baeafde68f372274c/html5/thumbnails/4.jpg)
![Page 5: Programming Techniques I SCJ1013 Arithmetic Expression](https://reader030.fdocuments.in/reader030/viewer/2022012808/61bded8baeafde68f372274c/html5/thumbnails/5.jpg)
The cin Object
• cin converts data to the type that matchesthe variable:
int height;cout << "How tall is the room? ";cin >> height;
• cin converts data to the type that matchesthe variable:
int height;cout << "How tall is the room? ";cin >> height;
![Page 6: Programming Techniques I SCJ1013 Arithmetic Expression](https://reader030.fdocuments.in/reader030/viewer/2022012808/61bded8baeafde68f372274c/html5/thumbnails/6.jpg)
Displaying a Prompt
• A prompt is a message that instructs the userto enter data.
• You should always use cout to display aprompt before each cin statement.
cout << "How tall is the room? ";cin >> height;
• A prompt is a message that instructs the userto enter data.
• You should always use cout to display aprompt before each cin statement.
cout << "How tall is the room? ";cin >> height;
![Page 7: Programming Techniques I SCJ1013 Arithmetic Expression](https://reader030.fdocuments.in/reader030/viewer/2022012808/61bded8baeafde68f372274c/html5/thumbnails/7.jpg)
The cin Object
• Can be used to input more than one value:cin >> height >> width;
• Multiple values from keyboard must be separatedby spaces
• Order is important: first value entered goes to firstvariable, etc.
• Can be used to input more than one value:cin >> height >> width;
• Multiple values from keyboard must be separatedby spaces
• Order is important: first value entered goes to firstvariable, etc.
![Page 8: Programming Techniques I SCJ1013 Arithmetic Expression](https://reader030.fdocuments.in/reader030/viewer/2022012808/61bded8baeafde68f372274c/html5/thumbnails/8.jpg)
![Page 9: Programming Techniques I SCJ1013 Arithmetic Expression](https://reader030.fdocuments.in/reader030/viewer/2022012808/61bded8baeafde68f372274c/html5/thumbnails/9.jpg)
Reading Strings with cin
• Can be used to read in a string• Must first declare an array to hold characters in
string:char myName[21];
• myName is a name of an array, 21 is the number ofcharacters that can be stored (the size of the array),including the NULL character at the end
• Can be used with cin to assign a value:cin >> myName;
• Can be used to read in a string• Must first declare an array to hold characters in
string:char myName[21];
• myName is a name of an array, 21 is the number ofcharacters that can be stored (the size of the array),including the NULL character at the end
• Can be used with cin to assign a value:cin >> myName;
![Page 10: Programming Techniques I SCJ1013 Arithmetic Expression](https://reader030.fdocuments.in/reader030/viewer/2022012808/61bded8baeafde68f372274c/html5/thumbnails/10.jpg)
![Page 11: Programming Techniques I SCJ1013 Arithmetic Expression](https://reader030.fdocuments.in/reader030/viewer/2022012808/61bded8baeafde68f372274c/html5/thumbnails/11.jpg)
Exercise Week5_1• Refer to Exercise 3 No. 1 in pg. 79.
• Solve the problem.• Add array of characters to the output.
Sample of output:Enter an integer: 7Enter a decimal number : 2.25Enter a single character : REnter an array of characters: Programming
• Refer to Exercise 3 No. 1 in pg. 79.
• Solve the problem.• Add array of characters to the output.
Sample of output:Enter an integer: 7Enter a decimal number : 2.25Enter a single character : REnter an array of characters: Programming
![Page 12: Programming Techniques I SCJ1013 Arithmetic Expression](https://reader030.fdocuments.in/reader030/viewer/2022012808/61bded8baeafde68f372274c/html5/thumbnails/12.jpg)
Mathematical Expressions
![Page 13: Programming Techniques I SCJ1013 Arithmetic Expression](https://reader030.fdocuments.in/reader030/viewer/2022012808/61bded8baeafde68f372274c/html5/thumbnails/13.jpg)
Mathematical Expressions
• Can create complex expressions usingmultiple mathematical operators
• An expression can be a literal, a variable, or amathematical combination of constants andvariables
• Can be used in assignment, cout, otherstatements:area = 2 * PI * radius;cout << "border is: " << 2*(l+w);
• Can create complex expressions usingmultiple mathematical operators
• An expression can be a literal, a variable, or amathematical combination of constants andvariables
• Can be used in assignment, cout, otherstatements:area = 2 * PI * radius;cout << "border is: " << 2*(l+w);
![Page 14: Programming Techniques I SCJ1013 Arithmetic Expression](https://reader030.fdocuments.in/reader030/viewer/2022012808/61bded8baeafde68f372274c/html5/thumbnails/14.jpg)
Order of Operations
In an expression with more than oneoperator, evaluation is in this order:
()- (unary negation), in order, left to right* / %, in order, left to right+ -, in order, left to right
In the expression 2 + 2 * 2 – 2
In an expression with more than oneoperator, evaluation is in this order:
()- (unary negation), in order, left to right* / %, in order, left to right+ -, in order, left to right
In the expression 2 + 2 * 2 – 2evaluatefirst
evaluatesecond
evaluatethird
![Page 15: Programming Techniques I SCJ1013 Arithmetic Expression](https://reader030.fdocuments.in/reader030/viewer/2022012808/61bded8baeafde68f372274c/html5/thumbnails/15.jpg)
Exampleint z, y=-5;z= 8 - 3 + 9 / 2 + 2 * - y;z= 8 - (3 + 9 / 2) + 2 * - y;// try this
![Page 16: Programming Techniques I SCJ1013 Arithmetic Expression](https://reader030.fdocuments.in/reader030/viewer/2022012808/61bded8baeafde68f372274c/html5/thumbnails/16.jpg)
Order of Operations
Show prove for the following expression
![Page 17: Programming Techniques I SCJ1013 Arithmetic Expression](https://reader030.fdocuments.in/reader030/viewer/2022012808/61bded8baeafde68f372274c/html5/thumbnails/17.jpg)
Associativity of Operators
• - (unary negation) associates right to left• *, /, %, +, - associate left to right• parentheses ( ) can be used to override the
order of operations:2 + 2 * 2 – 2 = 4(2 + 2) * 2 – 2 = 62 + 2 * (2 – 2) = 2(2 + 2) * (2 – 2) = 0
• - (unary negation) associates right to left• *, /, %, +, - associate left to right• parentheses ( ) can be used to override the
order of operations:2 + 2 * 2 – 2 = 4(2 + 2) * 2 – 2 = 62 + 2 * (2 – 2) = 2(2 + 2) * (2 – 2) = 0
![Page 18: Programming Techniques I SCJ1013 Arithmetic Expression](https://reader030.fdocuments.in/reader030/viewer/2022012808/61bded8baeafde68f372274c/html5/thumbnails/18.jpg)
Grouping with Parentheses
![Page 19: Programming Techniques I SCJ1013 Arithmetic Expression](https://reader030.fdocuments.in/reader030/viewer/2022012808/61bded8baeafde68f372274c/html5/thumbnails/19.jpg)
Algebraic Expressions
• Multiplication requires an operator:Area=lw is written as Area = l * w;
• There is no exponentiation operator:Area=s2 is written as Area = pow(s, 2);
• Parentheses may be needed to maintain orderof operations:
is written asm = (y2-y1) /(x2-x1);
• Multiplication requires an operator:Area=lw is written as Area = l * w;
• There is no exponentiation operator:Area=s2 is written as Area = pow(s, 2);
• Parentheses may be needed to maintain orderof operations:
is written asm = (y2-y1) /(x2-x1);12
12
xxyym
![Page 20: Programming Techniques I SCJ1013 Arithmetic Expression](https://reader030.fdocuments.in/reader030/viewer/2022012808/61bded8baeafde68f372274c/html5/thumbnails/20.jpg)
Algebraic Expressions
![Page 21: Programming Techniques I SCJ1013 Arithmetic Expression](https://reader030.fdocuments.in/reader030/viewer/2022012808/61bded8baeafde68f372274c/html5/thumbnails/21.jpg)
Postfix expression
![Page 22: Programming Techniques I SCJ1013 Arithmetic Expression](https://reader030.fdocuments.in/reader030/viewer/2022012808/61bded8baeafde68f372274c/html5/thumbnails/22.jpg)
Prefix expression
![Page 23: Programming Techniques I SCJ1013 Arithmetic Expression](https://reader030.fdocuments.in/reader030/viewer/2022012808/61bded8baeafde68f372274c/html5/thumbnails/23.jpg)
Exercise Week5_2 [Lab5, Exe1,No7, pg60]
• Write the formula in C++ statement.
![Page 24: Programming Techniques I SCJ1013 Arithmetic Expression](https://reader030.fdocuments.in/reader030/viewer/2022012808/61bded8baeafde68f372274c/html5/thumbnails/24.jpg)
When You Mix Apples and Oranges: Type Conversion
![Page 25: Programming Techniques I SCJ1013 Arithmetic Expression](https://reader030.fdocuments.in/reader030/viewer/2022012808/61bded8baeafde68f372274c/html5/thumbnails/25.jpg)
When You Mix Apples andOranges: Type Conversion
• Operations are performed between operandsof the same type.
• If not of the same type, C++ will convert one tobe the type of the other
• This can impact the results of calculations.
• Operations are performed between operandsof the same type.
• If not of the same type, C++ will convert one tobe the type of the other
• This can impact the results of calculations.
![Page 26: Programming Techniques I SCJ1013 Arithmetic Expression](https://reader030.fdocuments.in/reader030/viewer/2022012808/61bded8baeafde68f372274c/html5/thumbnails/26.jpg)
Hierarchy of Types
Highest:
Lowest:Ranked by largest number they can hold
long doubledoublefloatunsigned longlongunsigned intint
Highest:
Lowest:Ranked by largest number they can hold
long doubledoublefloatunsigned longlongunsigned intint
![Page 27: Programming Techniques I SCJ1013 Arithmetic Expression](https://reader030.fdocuments.in/reader030/viewer/2022012808/61bded8baeafde68f372274c/html5/thumbnails/27.jpg)
Type Conversion
• Type Conversion: automatic conversion of anoperand to another data type
• Promotion: convert to a higher type
• Demotion: convert to a lower type
• Type Conversion: automatic conversion of anoperand to another data type
• Promotion: convert to a higher type
• Demotion: convert to a lower type
![Page 28: Programming Techniques I SCJ1013 Arithmetic Expression](https://reader030.fdocuments.in/reader030/viewer/2022012808/61bded8baeafde68f372274c/html5/thumbnails/28.jpg)
Conversion Rules1) char, short, unsigned short automatically
promoted to int– Forarithmetic operationchar c=‘A’; cout<<6+c; // int
2) When operating on values of different data types, the lowerone is promoted to the type of the higher one.
int i=25; cout<<6.1+i; // float
3) When using the = operator, the type of expression on rightwill be converted to type of variable on left
int x, y =25; float z=2.5;x=y+z; //int
1) char, short, unsigned short automaticallypromoted to int
– Forarithmetic operationchar c=‘A’; cout<<6+c; // int
2) When operating on values of different data types, the lowerone is promoted to the type of the higher one.
int i=25; cout<<6.1+i; // float
3) When using the = operator, the type of expression on rightwill be converted to type of variable on left
int x, y =25; float z=2.5;x=y+z; //int
![Page 29: Programming Techniques I SCJ1013 Arithmetic Expression](https://reader030.fdocuments.in/reader030/viewer/2022012808/61bded8baeafde68f372274c/html5/thumbnails/29.jpg)
Exercise Week5_3 [Lab5, Exe1,No8, pg61]
• Given the following program, apply theCoercion rules & identify the outputint main(){char upperb='B';int j=2, k=3;double r=24.5, s=3.0, t;
cout<<"Rule 1 = "<<r+j;cout<<"Rule 2 = "<<upperb+j; //'B'=66t=r+j;cout<<"Rule 3 = "<<t;
return 0;}
• Given the following program, apply theCoercion rules & identify the outputint main(){char upperb='B';int j=2, k=3;double r=24.5, s=3.0, t;
cout<<"Rule 1 = "<<r+j;cout<<"Rule 2 = "<<upperb+j; //'B'=66t=r+j;cout<<"Rule 3 = "<<t;
return 0;}
![Page 30: Programming Techniques I SCJ1013 Arithmetic Expression](https://reader030.fdocuments.in/reader030/viewer/2022012808/61bded8baeafde68f372274c/html5/thumbnails/30.jpg)
Overflow and Underflow
![Page 31: Programming Techniques I SCJ1013 Arithmetic Expression](https://reader030.fdocuments.in/reader030/viewer/2022012808/61bded8baeafde68f372274c/html5/thumbnails/31.jpg)
Overflow and Underflow
• Occurs when assigning a value that is too large(overflow) or too small (underflow) to be heldin a variable
• Variable contains value that is ‘wrappedaround’ set of possible values
• Different systems may display a warning/errormessage, stop the program, or continueexecution using the incorrect value
• Occurs when assigning a value that is too large(overflow) or too small (underflow) to be heldin a variable
• Variable contains value that is ‘wrappedaround’ set of possible values
• Different systems may display a warning/errormessage, stop the program, or continueexecution using the incorrect value
![Page 32: Programming Techniques I SCJ1013 Arithmetic Expression](https://reader030.fdocuments.in/reader030/viewer/2022012808/61bded8baeafde68f372274c/html5/thumbnails/32.jpg)
#include <iostream>using namespace std;int main(){short testVar = 32767; //short maxcout << testVar << endl;testVar = testVar + 1;cout << testVar << endl;testVar = testVar - 1;cout << testVar << endl;return 0;}
Program Output32767-3276832767
Overflow and Underflow#include <iostream>using namespace std;int main(){short testVar = 32767; //short maxcout << testVar << endl;testVar = testVar + 1;cout << testVar << endl;testVar = testVar - 1;cout << testVar << endl;return 0;}
Program Output32767-3276832767
![Page 33: Programming Techniques I SCJ1013 Arithmetic Expression](https://reader030.fdocuments.in/reader030/viewer/2022012808/61bded8baeafde68f372274c/html5/thumbnails/33.jpg)
Type Casting
![Page 34: Programming Techniques I SCJ1013 Arithmetic Expression](https://reader030.fdocuments.in/reader030/viewer/2022012808/61bded8baeafde68f372274c/html5/thumbnails/34.jpg)
Type Casting
• Used for manual data type conversion• Useful for floating point division using ints:double m;m = static_cast<double>(y2-y1)
/(x2-x1);• Useful to see int value of a char variable:char ch = 'C';cout << ch << " is "
<< static_cast<int>(ch);
• Used for manual data type conversion• Useful for floating point division using ints:double m;m = static_cast<double>(y2-y1)
/(x2-x1);• Useful to see int value of a char variable:char ch = 'C';cout << ch << " is "
<< static_cast<int>(ch);
![Page 35: Programming Techniques I SCJ1013 Arithmetic Expression](https://reader030.fdocuments.in/reader030/viewer/2022012808/61bded8baeafde68f372274c/html5/thumbnails/35.jpg)
Type Casting - example
![Page 36: Programming Techniques I SCJ1013 Arithmetic Expression](https://reader030.fdocuments.in/reader030/viewer/2022012808/61bded8baeafde68f372274c/html5/thumbnails/36.jpg)
C-Style and Prestandard Type CastExpressions
• C-Style cast: data type name in ()cout << ch << " is " << (int)ch;
• Prestandard C++ cast: value in ()cout << ch << " is " << int(ch);• Both are still supported in C++, althoughstatic_cast is preferred
• C-Style cast: data type name in ()cout << ch << " is " << (int)ch;
• Prestandard C++ cast: value in ()cout << ch << " is " << int(ch);• Both are still supported in C++, althoughstatic_cast is preferred
![Page 37: Programming Techniques I SCJ1013 Arithmetic Expression](https://reader030.fdocuments.in/reader030/viewer/2022012808/61bded8baeafde68f372274c/html5/thumbnails/37.jpg)
Exercise Week5_4
• Correct the error of the program using typecastingint main(){char upperb='B';int j=2, k=3;double r=24.5, s=3.0, t;
t=r- static_cast<int>(s*3)%(2+j)/k;
cout<<"t= "<<t;return 0;}
• Correct the error of the program using typecastingint main(){char upperb='B';int j=2, k=3;double r=24.5, s=3.0, t;
t=r- static_cast<int>(s*3)%(2+j)/k;
cout<<"t= "<<t;return 0;}
![Page 38: Programming Techniques I SCJ1013 Arithmetic Expression](https://reader030.fdocuments.in/reader030/viewer/2022012808/61bded8baeafde68f372274c/html5/thumbnails/38.jpg)
Named Constants
![Page 39: Programming Techniques I SCJ1013 Arithmetic Expression](https://reader030.fdocuments.in/reader030/viewer/2022012808/61bded8baeafde68f372274c/html5/thumbnails/39.jpg)
Named Constants
• Named constant (constant variable): variablewhose content cannot be changed duringprogram execution
• Used for representing constant values withdescriptive names:const double TAX_RATE = 0.0675;const int NUM_STATES = 50;
• Often named in uppercase letters
• Named constant (constant variable): variablewhose content cannot be changed duringprogram execution
• Used for representing constant values withdescriptive names:const double TAX_RATE = 0.0675;const int NUM_STATES = 50;
• Often named in uppercase letters
![Page 40: Programming Techniques I SCJ1013 Arithmetic Expression](https://reader030.fdocuments.in/reader030/viewer/2022012808/61bded8baeafde68f372274c/html5/thumbnails/40.jpg)
Named Constants - example
![Page 41: Programming Techniques I SCJ1013 Arithmetic Expression](https://reader030.fdocuments.in/reader030/viewer/2022012808/61bded8baeafde68f372274c/html5/thumbnails/41.jpg)
Constants and Array Sizes
• It is a common practice to use a namedconstant to indicate the size of an array:
const int SIZE = 21;char name[SIZE];
• It is a common practice to use a namedconstant to indicate the size of an array:
const int SIZE = 21;char name[SIZE];
![Page 42: Programming Techniques I SCJ1013 Arithmetic Expression](https://reader030.fdocuments.in/reader030/viewer/2022012808/61bded8baeafde68f372274c/html5/thumbnails/42.jpg)
const vs. #define
• #define – C-style of naming constants:#define NUM_STATES 50
– Note no ; at end
• Interpreted by pre-processor rather thancompiler
• Does not occupy memory location likeconst
• #define – C-style of naming constants:#define NUM_STATES 50
– Note no ; at end
• Interpreted by pre-processor rather thancompiler
• Does not occupy memory location likeconst
![Page 43: Programming Techniques I SCJ1013 Arithmetic Expression](https://reader030.fdocuments.in/reader030/viewer/2022012808/61bded8baeafde68f372274c/html5/thumbnails/43.jpg)
Exercise Week5_5
• Refer to Lab 6 Exe. 3 No. 3 in pg. 80.
• Solve the problems using constant values torepresent the conversion factors.
• Refer to Lab 6 Exe. 3 No. 3 in pg. 80.
• Solve the problems using constant values torepresent the conversion factors.
![Page 44: Programming Techniques I SCJ1013 Arithmetic Expression](https://reader030.fdocuments.in/reader030/viewer/2022012808/61bded8baeafde68f372274c/html5/thumbnails/44.jpg)
Multiple Assignment andCombined AssignmentMultiple Assignment andCombined Assignment
![Page 45: Programming Techniques I SCJ1013 Arithmetic Expression](https://reader030.fdocuments.in/reader030/viewer/2022012808/61bded8baeafde68f372274c/html5/thumbnails/45.jpg)
Multiple Assignment andCombined Assignment
• The = can be used to assign a value tomultiple variables:x = y = z = 5;
• Value of = is the value that is assigned• Associates right to left:
x = (y = (z = 5));
• The = can be used to assign a value tomultiple variables:x = y = z = 5;
• Value of = is the value that is assigned• Associates right to left:
x = (y = (z = 5));valueis 5
valueis 5
valueis 5
![Page 46: Programming Techniques I SCJ1013 Arithmetic Expression](https://reader030.fdocuments.in/reader030/viewer/2022012808/61bded8baeafde68f372274c/html5/thumbnails/46.jpg)
Combined Assignment
• Look at the following statement:
sum = sum + 1;
This adds 1 to the variable sum.
• Look at the following statement:
sum = sum + 1;
This adds 1 to the variable sum.
![Page 47: Programming Techniques I SCJ1013 Arithmetic Expression](https://reader030.fdocuments.in/reader030/viewer/2022012808/61bded8baeafde68f372274c/html5/thumbnails/47.jpg)
Other Similar Statements
![Page 48: Programming Techniques I SCJ1013 Arithmetic Expression](https://reader030.fdocuments.in/reader030/viewer/2022012808/61bded8baeafde68f372274c/html5/thumbnails/48.jpg)
Combined Assignment
• The combined assignment operators providea shorthand for these types of statements.
• The statementsum = sum + 1;is equivalent tosum += 1;
• The combined assignment operators providea shorthand for these types of statements.
• The statementsum = sum + 1;is equivalent tosum += 1;
![Page 49: Programming Techniques I SCJ1013 Arithmetic Expression](https://reader030.fdocuments.in/reader030/viewer/2022012808/61bded8baeafde68f372274c/html5/thumbnails/49.jpg)
Combined Assignment Operators
Try:d -= 5 * 3 + a++;
Try:d -= 5 * 3 + a++;
![Page 50: Programming Techniques I SCJ1013 Arithmetic Expression](https://reader030.fdocuments.in/reader030/viewer/2022012808/61bded8baeafde68f372274c/html5/thumbnails/50.jpg)
Thank You
Q & A