You are on page 1of 31

Operators and arrays

Presented by: C8
37 – Sanket Bhosle
38 – Anurag Bhujbal
39 – Prathamesh Bhujbal
40 – Kshitij Bhure
41 – Dhananjay Bile
42 – Ayush Billade
Operators precedence and
associativity
Operator Precedence Operator Associativity

Operator precedence determines Operator associativity is used when


which operator is performed first in two operators of same precedence
an expression with more than one appear in an expression.
operators with different precedence.

2
Table
Category Operator Associativity
Postfix () [] -> . ++ - - Left to right
Unary + - ! ~ ++ - - (type)* & sizeof Right to left
Multiplicative */% Left to right
Additive +- Left to right
Shift << >> Left to right
Relational < <= > >= Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right

3
Bitwise OR | Left to right

Logical AND && Left to right


Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left
Comma , Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right

4
PRECEDENCE
 Used to decide which operator will be evaluated first.

5*4-6 Precedence of
multiplication is
greater than addition.

(5 * 4) – 6 = 14 5 * (4 – 6) = -10

5
Associativity
 Comes into picture when precedence of operators are same.

Associativity can be
10 / 2 * 5
either:
1) Left to Right
Left to right: (10 / 2) * 5 = 25
2) Right to Left

Right to left: 10 / (2 * 5) = 1

6
Example: 10*2-(10/5)+5*2

Program : The answer of equation is 28


but how ?
Program :
#include<stdio.h>
int main() Let’s see :
{ 10 * 2 – (10 / 5) + 5 * 2
1     int result;
    result=10*2-(10/5)+5*2;
=10 * 2 – 2 + 5 * 2
=(10 * 2) – 2 + 5 * 2
printf("The answer of 10*2-
(10/5)+5*2 is %d",result); =20 – 2 + (5*2)
    return 0; =20 – 2 + 10
}
=18 + 10
Output :
=28
The answer of 10*2-(10/5)+5*2 is 28
Example: (2*2)<(5–3) && (8/2)<(5+4)

Program : The answer of program is 0


but how ?
#include<stdio.h>
int main() Let’s see :
{
    int result;
(2 * 2) < (5 – 3) && (8 /
2 2) < (5 + 4)
    result=(2*2)<(5-3)&&(8/2)<(5+4);
    printf("The answer of = 4 < (5 – 3) && (8 / 2) < (5
(2*2)<(5-3)&&(8/2)<(5+4) is : + 4)
%d",result);
    return 0; = 4 < 2 && (8 / 2) < (5 + 4)
} = 4 < 2 && 4 < (5 + 4)
= 4 < 2 && 4 < 9
Output : = 0 && 4 < 9
The answer of = 0 && 0
(2*2)<(5-3)&&(8/2)<(5+4) is 0
= 0
Arrays
ARRAY BASICS
◇ An array is a Data structure containing a number of
data values , which are of same type.
◇ An array is a collection of elements of the same type
that are referenced by a common name.
◇ Compare to basic data type (int,float,char) it is a
derived data type.
◇ Array is a data structure which we can visualize as:

10
DATA TYPES OF
ARRAY ELEMENTS

G 5 E 1.6 B
11
1D ARRAY
◇ The simplest form of array one can imagine is one art dimensional array

◇ Declaration:
syntax: data _type name of the array[no. of elements]

example: an array of integers can be declared can be as follows


int arr[5]; … (length=5)
arr

◇ Can the array length be in decimal,negative or zero ? –


Length of array is always positive integer.

12
DECLARING THE
LENGTH OF AN
ARRAY
Without predefining it:

13
With predefining it:

14
HOW TO ACCESS AN
ARRAY ELEMENT?
• To access array element just write - array_name[index]

◇ arr

◇ 0 1 2 3 4 . ….n

◇ Accessing first element of an array arr[0]


◇ Accessing second element of an array arr[1]
◇ Accessing third element of an array arr[2]
◇ . .
◇ . .
◇ Accessing nth element of an array arr[n-1]
15 .
METHODS TO WRITE AN
ARRAY [1D]
◇ Method 1:

◇ Method 2:

16
Method 3:

Method 4:

17
POINTS TO REMEMBER
 If the number of elements are lesser than the length specified than the rest of
the locations are automatically filled by value.

 Easy way to initialize an array with all zeros is given by: int arr[10]={0};At
the time of initialization , never leave these curly brackets empty{}

 Never exceed the limit of number of elements specified by the length of an


array.

18
DESIGNATION
INITIALIZATION OF AN
ARRAY
◇ int arr[10]={[0]=1,[5]=2,[6]=3}

◇ This way of initialization is called designated initialization.

◇ int arr[10] ={1,0,0,0,0,2,3,0,0,0}; We want,


◇ 1 in position 0
◇ 2 in position 5
◇ 3 in position 6

19

Types of arrays

20
TWO DIMENSIONAL
ARRAYS
 At times we need to store the data in form of tables or matrices. For this,
we can use the two dimensional arrays.

 This array is specified by using two subscripts where one subscript is


denoted as the row and the other as the column.

 It is also viewed as an array of arrays


REPRESENTATION OF 2-D
ARRAY

22
• Two dimensional array is nothing but array of array.
• syntax :
data_type_ array_name[num_of_rows][num_of_column];

23
THREE DIMENSIONAL (3D)
ARRAY
◇ A 3D array is a multi-dimensional array(array of arrays). A 3D array is a collection
of 2D arrays. It is specified by using three subscripts:Block size, row size and column
size. More dimensions in an array means more data can be stored in that array.
◇  if we add one more dimension here,
i.e, int arr[3][3][3], now it becomes a 3D array.

◇ Declaring of array :
int arr[3][3][3]; //3D array

block(1) 1 2 3 block(2) 10 11 12 block(3) 19 20 21


456 13 14 15 22 23 24
789 16 17 18 25 25 27
3x3 3x3 3x3
24
REPRESENTATION OF 3-D
ARRAY

25

Applications illustrating use of arrays

26
EXAMPLE OF UNORDERED
ARRAYS

27
Ordered sequence

Data stored in array is in ordered manner

For example : Ascending order and descending


order
ORDER
Output :

29
DESCENDING
ORDER
Output:

30
Thank You!

31

You might also like