ICS 103: Computer Programming in C

Handout-04

Topic: Arithmetic Expressions.

 

Instructor: M. Waheed Aslam.

 

Objective:

·         To know about basic data types of C.

·         To know about difference in / and % operators.

·         To know how to change type of variable at any place using type casting.

·         To know rules for Evaluation of Arithmetic Expression.

·         To know how to use +=, -=, *=, /=, %= compound assignment statements.

·         To know how to write mathematical formula in C.

 

Data Types revisited:

Why we need int data type as double can be used for other type variables like int?

·       On many computers, operations involving integers are faster than those involving numbers of type double

·       Less storage space required to store integer value than double.

·       Operations with integers are always precise

·       Some loss of accuracy, or round-off error, may occur when dealing with type double numbers.

 

 

Note: ANSI standard specifies that the minimum range for positive values of type double is from 10-37 to 1037.

 

Also, note that actual internal representation is computer dependent.

 

 

Homework: Run a program to determine the exact ranges for int and double in the implementation you are using.

 

Arithmetic Expressions:

Use of / (Division Operator) and % (Mod Operator or Reminder Operator):

 

·       When an integer is divided by another integer the result is always an integer even if the answer consists of fractional value. For example 5/2 gives the answer as 2 though the actual answer is 2.5 because integer cannot have fraction.

 

·       To avoid this inconsistency we use conversion operation called a cast.

 

·       Placing the name of the desired data type in parentheses immediately before the value to be converted causes the value to be changed to the desired data format before it is used in computation.

 

·       So, to get the correct answer for the previous case of 5/2 we write (double) 5/(double) 2 or (float) 5/(float) 2.

 

·       We can also write (double) 5/2 or (float) 5/2 because changing one operand will automatically change the other operand. This causes the value of the answer to have fraction value and remember this casting does not change 5 and 2 to float or double, they will remain as integers. They only change the result.

 

·       Similarly, to find the remainder, the operator % is used. For example, to find the remainder when 7 is divided by 3 we write 7 % 3 which gives an answer of 1.

 

 

 

 

Example:

·       Let x, y, z be integer variables and c be a float variable. Also let x = 9, y = 11, z = 16. Then c = x / 5 + y / 2 + z / 5 will give the answer as 9 (Which is = 1+ 5+ 3) and not 10.5 (which comes from 1.8+5.5+3.2).

·       But if we write c = (float) x / (float) 5 + (float) y / (float) 2 + (float) z / (float) 5 

OR

                             c = (float) x / 5 + y / (float) 2 + z / (float) 5

 

we will get the correct answer of 10.5 and it does not change the x, y, z to

be of float type.  They will remain as integers.

 

·       Consider the case where z is of type float. Then there is no need for placing (float) before z and before 5 because if any one of the operand is float the answer will also be of float type.

 

Consider another example:

                             int     i=7, x=11 , y=13 ;

                             float z=18, a , b ;

                            

                             a = x / 3 + y / 2.5 – z / 3;

                             b = i / 2.5 + x / 4 + y / 1.2;

can be written to get the correct answer as

                             a = (float) x / 3 + y / 2.5 – z / 3;

                             b =  i / 2.5 + (float) x /  4 + y / 1.2;

 

Note that there is no need of (float) cast for y / 2.5 and i / 2.5 because if any one of the operands is float the other is automatically converted to float but only during the operation and not always

 

Rules for Evaluation of Arithmetic expressions:

 

1)  All parenthesized expressions must be evaluated separately. Nested parenthesized expressions must be evaluated inside out, with the  Inner-most expression evaluated first – Called Parenthesis rule.

 2) The operator precedence rule:

     Operators in the same expression are evaluated in the following order:

 

            Unary + and – are evaluated first.         

 

             *,  /,   %  are evaluated next .

 

               binary operator  + and – are evaluated last .

 

3)   The associativity rule:

      Unary operators in the same subexpression and at the same precedence level 

            (such as + and -) are evaluated right to left (right associativity).

 

      Binary operators in the same subexpression and at the same precedence level  

            are evaluated left to right (left associativity).

 

Example:

Consider the expression:

                                      -a + (c + b * ( c + a ) / c – b / a ) + a – b / 2

·       The innermost parenthesis (c + a ) is evaluated first.

·       Then the next innermost parenthesis ( c + b * ( c + a ) / c – b / a ) is evaluated.

·       Thus, In this subexpression, b * ( c + a ) is evaluated first.

·       Then b * ( c + a ) / c is evaluated.

·       Then b / a is evaluated.

·       Then the whole expression is evaluated.

·       Then –a is evaluated,

·       Then b / 2 is evaluated, and finally

·       The whole expression result is evaluated.

 

Compound assignment statements:

·       C evaluates the assignment statements in two steps: The right-side expression is evaluated first, and then C stores the result at the address of the variable on the left.

·       So, the statement          x = x * 15 can be written in short form as x *= 15.

·       Remember that the variable on left is applied to the entire expression on the right; that is x *= 15 + y; is equivalent to   x = x * (15 + y) ;

·       Compound assignment can be used with other arithmetic operators such as - , +, / , %.

 

Writing Mathematical Formulas in C:

 

The formulas written in mathematics are different from what is written in C language.

For example, the formula: x = ab + cd in mathematics

 

is written in C language as x = a*b + c*d   where * indicates multiplication.

a+b

c+d

 
 

Another example where division is considered x =    

 

is written in C language as x = (a+b) / (c+d) where / is division.

                                                  

 

Consider some more examples of mathematical formulas and their C language  equivalents:

 

Mathematical Formula                                    C Expression

                      x2 – 5bc                                         x * x – 5 * b * c

 

                         2a                                               2 * a / (4 * b + 5 * c)

                      4b + 5c

 

In all previous examples, check for the use of parentheses, particularly when

using the division.

 

As the rule of evaluation of arithmetic expression says that the expression

inside the parentheses is evaluated first, we should pay attention to its use.

 

Solved Problem #1:

 

/*******************************************************

Find output of the following manually:

********************************************************/

#include<stdio.h>

main()

{

int x=2;

double a=2.0, b=7.0, c=1.0,y=3.0,z=5.0, result_1, result_2; // variable declarations

 

result_1 = y/a;     // calculations

result_1 += 11.0;

result_2 =(int) z %(int) y;

 

printf (" The result_1 is : %lf \n",result_1); // print

printf (" The result_2 is : %lf" , result_2);

} // end of main

 

Output:

 

 

Solved  Problem #2:       

Write a program to find the values of a, b which are double and m which is integer.

Let x, y, z be integer values to be read.

The program should read the x, y, z variables and print the a, b, m variables where:

                   a = x + z + 5  +  2x + 4y  +  4y      

                                         y                 3            3z

 

b =          1         +                     x + y + z                       

                                      1 + x + y                z + 3x + 2y                 

 

                             m = 2 (x + y) + 6 (y – z)

/* C - Program for above problem */

#include<stdio.h>

main()

{double a, b ;  // variable declarations

  int  m, x, y, z;

  printf("Please Input values of x,y,z : ");

  scanf(" %d %d %d",&x,&y,&z);  // input

 

  a=(x+y+5)/y + (2*x+4*y)/3 + (4*y)/(3*z);  // calculation

  b=1/(1+x+y) + (x+y+z)/(z+3*x+2*y);

  m=2*(x+y)+6*(y-z);

  printf("The value of\t a = %9.2lf \n" ,  a); // print statements

  printf("The value of\t b = %6.3lf \n" ,  b);

  printf("The value of\t m = %d",  m );

  }

 Sample Output:

 

Now, in the above program if you declare x, y and z double then you will get following output (and make changes in %d  etc accordingly) :