1 / 19

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.

roxy
Download Presentation

Chap 5 Operators and Expressions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

  3. 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 ;

  4. 5.3 Increment /Decrement Operators • C provides two special operators, ++ and --, to increment and decrement variables in shorthand form . iValue = iValue +1 iValue += 1 Ex : iCount = 10 iCount += 1 iNewCount = iCount; But iNewCount = iCount++ ; ( P.S. They cannot be used on constants or expressions) (Ex. 3++) iValue++ or ++ iValue iNewCount = ++iCount ;

  5. Program 5.1 Pre- and Postincrement Operators int main(void){ int iCount1,iCount2; iCount1 = 5; printf(“The value of iCount1 is : %4d\n”,iCount1); printf(“The value of iCount1++ is : %4d\n”,iCount1++); printf(“The value of iCount1-- is : %4d\n\n”,iCount1--); iCount1 =5; printf(“The value of iCount1 is : %4d\n”,iCount1); printf(“The value of ++iCount1 is : %4d\n”,++iCount1); printf(“The value of --iCount1 is : %4d\n\n”,--iCount1); iCount2 = iCount1++; printf(“The value of \” iCount2 = iCount1++” is : %4d\n”,iCount1); iCount2 =++iCount1; printf(“The value of \” iCount1 = ++iCount1” is : %4d\n”,iCount2); }

  6. 5.4 Arithmetic Expressions and Precedence • Table 5.1 Hierarchy of Operator Evaluation

  7. 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;

  8. Integer Arithmetic iValue=5+18/4; => 5+4 <=Step 1 of evaluation => 9 <=Step 2 of evaluation iVal = 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 10 [2+3*6/4]

  9. 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 .

  10. 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); }

  11. 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); }

  12. 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

  13. 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; /*see example below*/ char cIn; 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 ;

  14. 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);

  15. 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.

  16. 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 ;

  17. 5.7 Subtractive Cancellation

  18. Numerical Example : f(x) = [ ] f(x) = [ ] * [ ] =[ ] Consider numerical evaluation of the formula 1-cos(x) x 2 2 1+cos(x) sin (x) 1-cos(x) 1+cos(x) x *[1+cos(x)] 2 x 2

  19. 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)); } }

More Related