You are on page 1of 42

TMC 1414

INTRODUCTION TO
PROGRAMMING

Lecture 05: Arithmetic Calculation


CONTENT

 Assignment Operator
 Arithmetic Calculation
 Basic Arithmetic Operators
 Increment and decrement Operators
 Compound assignment Operators

 Type of Arithmetic Expressions


 Explicit type conversions using cast operator

 Mathematical Library Functions


 Random Number Generation in C
ASSIGNMENT
OPERATOR
ASSIGNMENT OPERATOR =

 Assignment operators are used to assign value to a variable.


 The left side operand of the assignment operator is a variable and
right side operand of the assignment operator is a value.
 The value on the right side must be of the same data-type of variable
on the left side otherwise the compiler will raise an error.
 = is not the same as == !!!
ASSIGNMENT OPERATOR
ARITHMETIC
CALCULATION

Basic Arithmetic Operators


• 2 types of arithmetic Operators in C
• Unary Arithmetic Operators
• Binary Arithmetic Operators

6
UNARY ARITHMETIC
OPERATORS
Unary Arithmetic Operators
• Require one operand
• Include unary plus(+), unary minus (-), increment (++), decrement (--)
• Unary plus (+) no effect on the result
• Unary minus (-) reverses the sign of the operand to which it applies (-ve
value)

Example:
second += 100; // second = second + 100
second -=100; //second = second - 100

7
BINARY ARITHMETIC OPERATORS
 Require two operands
 Include +, -, *, / and %
 Can be integer or floating-point numbers (except

modulus-must be integer)
Symbol Operator Name

+ Addition
Additive operator
- Subtraction

* Multiplication

/ Division Multiplicative operator


% Remainder / Modulus

8
BINARY ARITHMETIC OPERATORS

 Assume
int first = 100, second = 7, third;
VALUE BEFORE EXECUTION VALUE AFTER EXECUTION

No. Statement first second third first second third

1 third = first + second; 100 7 ??? 100 7

2 third = first - second; 100 7 ??? 100 7

3 third = first * second; 100 7 ??? 100 7

4 third = first / second; 100 7 ??? 100 7

5 third = first % second; 100 7 ??? 100 7 9


BINARY ARITHMETIC OPERATORS

 Assume
int first = 100, second = 7, third;
VALUE BEFORE EXECUTION VALUE AFTER EXECUTION

No. Statement first second third first second third

1 third = first + second; 100 7 ??? 100 7 107

2 third = first - second; 100 7 ??? 100 7 93

3 third = first * second; 100 7 ??? 100 7 700

4 third = first / second; 100 7 ??? 100 7 14

5 third = first % second; 100 7 ??? 100 7 2


10
INCREMENT & DECREMENT
OPERATORS
 C has two special operators for incrementing or
decrementing a variable by 1:
• ++ (increment)
• --(decrement)
Instead of writing

count = count + 1;

We can more concisely write

count++; //Postfix form of the increment operator

or

++count; //Prefix form of the increment operator

11
INCREMENT & DECREMENT
OPERATORS
count = count – 1;
count--; //postdecrement

or

--count; //predecrement

Consider: Example 1
int count = 3;

printf (“%d”, count++); // output: count = 3;


printf (“%d”, count); // output: count = 4;
Consider: Example 2
int count = 3;

printf (“%d”, ++count); // output: count = 4;


printf (“%d”, count); // output: count = 4;
INCREMENT & DECREMENT
OPERATORS
INCREMENT & DECREMENT
OPERATORS

Rules for Increment and Decrement Operators


1. All increment and decrement operators are unary operators and they require variables
as their operands.
2. The postincrement operator has the side effect of incrementing its operand by one,
after the expression in which it appears is evaluated using the operand’s previous
value. Similarly, the postdecrement operator does not change the value of the operand
while the expression is being evaluated. After the postdecrement operator is applied,
the value of the operand is decremented by one.
3. The preincrement and predecrement operators first generate their side effect; that is,
they increment or decrement their operand by 1. Then, the expression in which they
appear is evaluated.
4. The precedence and associativity of the increment and decrement operators are the
same as those of unary + and unary -..

14
COMPOUND ASSIGNMENT
OPERATORS
 To compute a value for an expression and store it in a variable, you
have to use the assignment operator =. (known as simple assignment
operator).
 C supports compound assignment operators, which are obtained by
combining some operators with the simple assignment operator.

Symbol Compound Assignment Operator


+= Assign sum
-= Assign difference
*= Assign product
/= Assign division
%= Assign remainder

count = count + first SIMILAR TO count += first


COMPOUND ASSIGNMENT
OPERATORS
Example: Assume
int third = 13, fourth = 20;

No. Statement Value of fourth after


1 fourth += third; Execution

2 fourth -= third;
3 fourth *= third;
4 fourth /= third;
5 fourth %= third;
6 fourth += third + 4;
7 fourth -= third + 4;
8 fourth *= third + 4;
9 fourth /= third + 4;
10 fourth %= third + 4;
COMPOUND ASSIGNMENT
OPERATORS
Example: Assume
int third = 13, fourth = 20;

No. Statement Value of fourth after


1 fourth += third; Execution
33
2 fourth -= third; 7
3 fourth *= third; 260
4 fourth /= third; 1
5 fourth %= third; 7
6 fourth += third + 4; 37
7 fourth -= third + 4; 3
8 fourth *= third + 4; 340
9 fourth /= third + 4; 1
10 fourth %= third + 4; 3
ARITHMETIC CALCULATION:
IMPORTANT!
Rules for Assigning a Type to Arithmetic Expressions
that Involve int and double
• If one or more of the operands in an arithmetic expression are of
type double, the result of the expression is also of type double.
• If all operands in an arithmetic expression are of type int, the result
of the expression is also of type int.
• In an arithmetic assignment expression statement, the type of the
entire statement and the type of the value stored in the variable to
the left of the assignment operator are the same as the type of the
variable itself.
C-TYPE CASTING

 Type casting is a way to convert a variable from one data type to another
data type.
 Type conversions can be implicit which is performed by the compiler
automatically, or it can be specified explicitly using cast operator.
 It is considered good programming practice to use the cast operator
whenever type conversions are necessary.
 It is best practice to convert lower data type to higher data type to avoid data
loss.
 Data will be truncated when the higher data type is converted to lower.
For example, if a double is converted to int, data which is presented after
the decimal point will be lost/truncated.
C-TYPE CASTING –
IMPLICIT
CONVERSION
 The usual arithmetic conversions are implicitly
performed to cast their values in a common type
 C uses the rule that in all expressions except assignments

 any implicit type conversions made from a lower size


type to a higher size type as shown below:
C-TYPE CASTING – EXPLICIT
CONVERSION
 Explicit type conversions are requested in C using a cast operator.
 The name of the operator comes from the term typecast;
``typecasting'' is another term for explicit type conversion, and some
languages have ``typecast operators.'' Yet another term for type
conversion is coercion.
 A cast operator consists of a data type in parentheses.

(data type) variable_name


TYPECAS
TING IMPLICIT AND
EXPLICIT
TYPECASTING

EXAMPLE
C-TYPE CASTING: EXPLICIT
TYPECASTING
Example
Assume double first = 4.7;
int second = 27;

Value after Execution

No. Statement Output

1 first = (int) (first + second); 31.0

2 first = (int) first + second; 31.0

3 first = (int) first % second; 4.0

4 first = second % (int) first; 3.0


BUILT-IN
MATHEMATICAL
FUNCTIONS IN C
#include <math.h> - floating data type functions
#include <stdlib.h> - integer data type functions
MATHEMATICAL
LIBRARY FUNCTIONS
• Mathematical library functions are declared in standard header files. If you
are planning to make use of them, you must have appropriate preprocessor
directives in your program.
• The floating-point type function declarations are found in the math.h header
file.
#include<math.h>
• The integer type mathematical functions require the preprocessor directive
#include<stdlib.h>
• To use the function, you have to use the proper function and know about the
purpose, number and type of arguments and the type of values that are
returned.
MATHEMA
TICAL
LIBRARY
FUNCTION
S:
FLOATING
TYPE
FUNCTION
S
MATHEMATICAL LIBRARY
FUNCTIONS
MATHEMATICAL LIBRARY
FUNCTIONS
RANDOM NUMBER
GENERATION
 rand function
 Load <stdlib.h>
 Returns "random" number between 0 and RAND_MAX (at least 32767)
 i = rand();
 Pseudorandom
Preset sequence of "random" numbers
Same sequence for every function call
 Scaling
 To get a random number between 1 and n
 1 + ( rand() % n )
rand() % n returns a number between 0 and n - 1
Add 1 to make random number between 1 and n
 1 + ( rand() % 6)
 number between 1 and 6
RANDOM NUMBER
GENERATION
Rolling a Six-Sided Dice
 A dice-rolling program that simulates a six-sided dice would require
random integers from 1 to 6.
 To demonstrate rand, let’s develop a program to simulate 20 rolls of a
six-sided die and print the value of each roll.
 The function prototype for function rand is in <stdlib.h>.
 We use the remainder operator (%) in conjunction with rand as
follows
 1+ rand() % 6
 to produce integers in the range 1 to 6.
RANDOM
NUMBER
GENERAT
ION:
RANDOM NUMBER
GENERATION: OUTPUT FOR
4 RUNNING
SOLUTION: SEEDING
RANDOM NUMBER
GENERATION
SEEDING RANDOM NUMBER
GENERATION: OUTPUT FOR
4 RUNNING
TEST YOURSELF

Are you ready?


EXERCISE 1

 What is the result of each of the following expressions?

a) 1 + 2 * 4 / 2
b) (1 + 2) * 4 / 2
c) 1 + 2 * (4 / 2)
d) 9 % 2 + 1
e) (1 + (10 – (2 + 2)))
EXERCISE 2

 Convert each of the following


formulas into its C assignment
equivalents: a =
EXERCISE 3

Show the value of x after each of the following statements is


performed:
 x = fabs (7.5);
 x = floor (7.5);
 x = ceil (0.0);
 x = fabs (0.0);
EXERCISE 4
EXERCISE 5: WHAT IS THE
OUTPUT ?
THANK
YOU

You might also like