Chap 5 Operators and Expressions

Download Report

Transcript Chap 5 Operators and Expressions

Chap 5 Operators and Expressions
5.1 Arithmetic Operators and Expressions
 Arithmetic Operators : Integers and Floating Point
For example : + - * /
 Modulo Operator : Integers
For example : %
iA % iB = k * iB
Ex : 0 % 4 = 0 , 1 % 4 = 1 , 2% 4 = 2 …. etc
Chap5-1
5.2 Assignment Operators
 Assignment Expressions : A = B ;
Ex : iCount = 1;
iCount = iCount + 1
From an algebraic point of view : iCount = iCount + 1 ;
For computer language : iCount
iCount + 1 ;
1 = iCount ; ( make little sense )
iCount = iCount + 1
Variable = Variable op Expression
Chap5-2

can also be written : Variable op = Expressions
Ex : iCount = iCount + 1 ;
iCount += 1;
Ex : iValue = iValue – 1 ;
iValue -= 1;
Similary , division operations of the type
Ex : fValue = fValue / 2.0 ;
fValue /= 2.0 ;
Chap5-3
5.3 Increment /Decrement Operators
C provides two special operators, ++ and --, to
increment and decrement variables in shorthand form .
iValue = iValue +1
iValue++ or ++ iValue
iValue += 1
Ex : iCount = 10
iCount += 1
iNewCount = ++iCount ;
iNewCount = iCount;
But iNewCount = iCount++ ;
( P.S. They cannot be used on constants or expressions)

(Ex. 3++)
Chap5-4
Program 5.1 Pre- and Postincrement Operators
int main(void){
int iCount1,iCount2;
iCount1 = 5;
printf(“The value of
printf(“The value of
printf(“The value of
iCount1 =5;
printf(“The value of
printf(“The value of
printf(“The value of
iCount2 = iCount1++;
printf(“The value of
iCount2 =++iCount1;
printf(“The value of
}
iCount1 is : %4d\n”,iCount1);
iCount1++ is : %4d\n”,iCount1++);
iCount1-- is : %4d\n\n”,iCount1--);
iCount1 is : %4d\n”,iCount1);
++iCount1 is : %4d\n”,++iCount1);
--iCount1 is : %4d\n\n”,--iCount1);
\” iCount2 = iCount1++” is : %4d\n”,iCount1);
\” iCount1 = ++iCount1” is : %4d\n”,iCount2);
Chap5-5
5.4 Arithmetic Expressions and Precedence

Table 5.1 Hierarchy of Operator Evaluation
Chap5-6
Ex 1:
fValue/2.0+3.0; => 4.0/2.0+3.0 <=Starting expression
=>2.0+3.0
<=Step 1 of evaluation
=>5.0
<=Step 2 of evaluation
3.0+fValue/2.0; =>3.0+4.0/2.0 <=Starting expression
=>3.0+2.0
<=Step 1 of evaluation
=>5.0
<=Step 2 of evaluation
Ex 2:
(3.0+(fValue/2.0)) =>(3.0+(4.0/2.0)) <= Starting expression
=>3.0+2.0
<=Step 1 of evaluation
=>5.0
<=Step 2 of evaluation
fValue =(10.0/2.0)+(3.0*6.0/4.0);
Ex 3:
Fx=fy=0;
Chap5-7
Integer Arithmetic
iValue=5+18/4; => 5+4 <=Step 1 of evaluation
=> 9 <=Step 2 of evaluation
10
iVal = [2+3*6/4]
iVal=10/2+3*6/4 =>5+18/4 <= Step 1 of evaluation
=>5+4 <= Step 2 of evaluation
=>9
<=Step 3 of evaluation
iVal=10/(2+3*6/4) =>10/(5+18/4) <= Step 1 of evaluation
=>10/(2+4)
<= Step 2 of evaluation
=>10/6
<=Step 3 of evaluation
=>1
<=Step 4 of evaluation
Chap5-8
Computer Program : Resistors in Parallel
R1
Combined Resistance
=
R2
1
1
R1 +
1
R2
+
1
R3
R3
Where R1=1.5 ohms , R2=2.5ohms , R3=3.5 ohms .
Chap5-9
Program 5.2 : Resistors in Parallel
#include <stdio.h>
int main(void){
float fR1 = 1.5F;
float fR2 = 2.5F;
float fR3 = 3.5F;
float fCombineResistance;
printf(“Resistor 1 is %8.3f ohms\n”,fR1);
printf(“Resistor 2 is %8.3f ohms\n”,fR2);
printf(“Resistor 3 is %8.3f ohms\n”,fR3);
fCombineResistance = 1.0/(1.0/fR1+1.0/fR3+1.0/fR2);
printf(“Combined Resistance is %8.3f ohms\n”, fCombineResistance);
}
Chap5-10
Computer Program : Horner’s Rule
f(x)=a0+a1*x+a2*x^2+a3*x^3+… + an*x^n
f(x)=a0+x*{a1+x[a2+x(a3…+ an*x)]}
f(x)=2+3*x+4*x^2+5*x^3+6*x^4
Program 5.3 : Using Horner’s Rule to Evaluate a Polynomial
#include <stdio.h>
#include <math.h>
int main(void){
float fA=2.0,fB=3.0, fC=4.0, fD=5.0, fE=6.0,fX1;
printf(“Please enter coefficient fX :”);
fflush(stdout)
scanf(“%f%*c”,&fX);
fX1=fA+fB*fX+fC*fX *fX +fd*fX *fX *fX+fE *fX *fX *fX *fX;
printf(“Sum of Product terms : F(%4.2f) = %4.2f\n”,fX,fX1);
fX1=fA+fX*(fB+fX*(fC +fX *(fd+(fX*fE))));
printf(“Using Horner’s Rule: F(%4.2f) = %4.2f\n”,fX,fX1);
Chap5-11
}
5.5 Mixed Expression and Data Type Conversions
When an arithmetic expression is evaluated, it is important
to ensure that all its components are of a compatible type.
1.If either operand is long double, convert other to long
double ,otherwise
2. If either operand is double, convert the other to double, otherwise
3.If either operand is float, convert the other to float, otherwise
4.Convert char and short to int
5.If either operand is long, convert the other to long
Chap5-12
fValue=iValue;/*Converts the integer to a flout before assigning it. */
iValue=fValue;/*Converts (truncates) the floating point value before*/
/*the assignment is made
*/
int Value;
char cIn;
/*see example below*/
iValue =
cIn; /*convert char to int : no information lost */
cIn
= iValue; /*convert int to char : truncation occurs */
int iCount;
int iValue = 2;
iCount = 3.4 * iValue ;
Chap5-13
Remark 5.2
fCombineResistance = 1.0/(1.0/fR1+1.0/fR2+1.0/fR3);
fCombineResistance = 1/(1/fR1+1/fR2+1/fR3);
Remark 5.3
fX1=fA+fB*fX+fC*pow(fx,2.0)+fD*pow(fx,3.0)+fE*poq(fx,4.0);
printf(“Using Power Math Functions : F(%4.2f)=%4.2f\n”,fX,fX1);
Chap5-14
Explicit Conversion
( type-name ) expression
Ex : fVal = (float) 1 / 2 ;
1. Casts have highest precedence, so the integer 1 is
converted to a float .
2. The / operator has next highest precedence, so type
conversio is done on the expression’s arguments.
The integer 2 is converted to 2.0 .
3. The division is computed, yielding a float 5.0 .
4. 0.5 is assigned to fVal. No type conversion is
necessary since it is already a float.
Chap5-15
Question :
fVal = (double) ( 1/ 2) ;
1. The integer expression ½ is evaluated first. Since
the ( ) have the highest precedence.
2. The result of ( 1/2 ) is an integer 0 . It is converted
to double.
3.The double rvalue is converted to a float lvalue.
4. fVal is assigned the value 0.0.
int
iValue = 3;
char cAChar;
cAChar = (char) iVal ;
Chap5-16
5.7 Subtractive Cancellation
Chap5-17
Numerical Example :
Consider numerical evaluation of the formula
f(x) =
1-cos(x)
[ x2
f(x) = [
1-cos(x)
x2
]
]*[
1+cos(x)
]
1+cos(x)
2
=[
sin (x)
x 2 *[1+cos(x)]
]
Chap5-18
Program 5.5 : Simulative Subtractive Cancellation
#include <stdio.h>
#include <math.h>
float Function1 (float fX){
return (1.0-cos(fX))/fX/fX;
}
float Function2 (float fX){
return sin(fX)*sin(fX)/fX/fX /(1.0+cos(fX));
}
int main(void){
float fX=1.0;
int iCount;
for( iCount=1;iCount<=8;iCount++){
fX=fX/10.0;
printf(“fX = %10.3e : Function1 = %10.8f : Function1 = %10.8f
\n”,fX, Function1(fX), Function2(fX));
}
Chap5-19
}