17
ARRAYS Prakash Khaire Lecturer, B V Patel Institute of BMC & IT

Lecture17 arrays.ppt

Embed Size (px)

DESCRIPTION

arrays in c

Citation preview

Page 1: Lecture17 arrays.ppt

ARRAYS

Prakash KhaireLecturer, B V Patel Institute of BMC & IT

Page 2: Lecture17 arrays.ppt

Arrays

●It is derived data type●Arrays are collection of data that belong to same data type●Arrays are collection of homogeneous data●Array elements can be accessed by its position in the array called as index●Values in an array are identified using array name with subscripts●Also known as subscripted variable●It is fixed-size collection of elements

Page 3: Lecture17 arrays.ppt

●Following are types of array●One-dimensional Array●Two-dimensional Array●Multi-dimensional Array

Page 4: Lecture17 arrays.ppt

One-dimensional Array

●A collections of variables are given one variable name using only one subscript and such a variable is called a single-subscripted variable or one dimensional array●Syntax

data_type ArrayName[size]; data_type : is a valid data type like int, float or char Arrayname : is a valid identifier size : maximum number of elements that can be stored in array

Page 5: Lecture17 arrays.ppt

ArraysArray index starts with zeroThe last index in an array is num – 1 where num is the no of elements in a arrayint a[9] is an array that stores 9 integers

0 1 2 3 4 5 6 7 8

100 102 104 106 108 110 112 114 116

index

elements

Memory address

Page 6: Lecture17 arrays.ppt

Initilization of Array

●After declaring the array, all the elements of array must be initialized other wise they will contain garbage value●At compile time●At run time data_type ArrayName[size] = {list of values} int marks[5] = {55, 63,67,78,59};

Page 7: Lecture17 arrays.ppt

Each value is treated as an element of the array and is stored in the memory as follows

55

63

67

78

59

marks

0

1

2

3

4

Page 8: Lecture17 arrays.ppt

●float marks[5]={45.5,65.0,67.5.77.0,79 .0};●char name[8]={‘P’,’r’,’a’, ’k’, ’a’, ’s’, ’h’, ’\0’};

●int marks[] = {63,68,57,69,77};●char name[] = “Prakash”;

Page 9: Lecture17 arrays.ppt

●int marks[5]={67,66};

67

66

4646

8978

-545

0

1

2

3

4

Page 10: Lecture17 arrays.ppt

Two Dimensional Array

●To store following data of 4 students

201 202 203 204 20509BCA07 45 42 41 56 4609BCA14 46 55 70 49 5609BCA45 65 61 68 42 5609BCA115 46 38 39 45 42

Page 11: Lecture17 arrays.ppt

● StudentDetailsIJ● Where, StudentDetails represents a matrix I represents no. of rows J represents no. of columns

It is collection of rows and columns

Two Dimensional Array

Page 12: Lecture17 arrays.ppt

● Declaration of 2D arraytype array_name[row_size][column_size];

Two Dimensional Array

3,43,33,23,13,02,42,32,22,12,01,41,31,21,11,00,40,30,20,10,0

Row1

column1 column2 column3 column4

Row0

column0

Row2

Row3

Page 13: Lecture17 arrays.ppt

Storage Representation

● Similar to 1D Array, 2D array are also stored in contiguous memory in increasing memory locations.

0,0 0,1 0,2 1,0 1,1 1,2 2,0 2,1 2,2

990 992 994 996 998 1000 1002 1004 1006

Page 14: Lecture17 arrays.ppt

Initilization

● int mat[3][3] = { 1,3,2,4,7,6,5,8,9};

● int mat[3][3] = { {1,3,2},{4,7,6},{5,8,9}

};

Page 15: Lecture17 arrays.ppt

● int mat[ ][3]={ {1,3,2},{4,7,6},{5,8,9}

};

Page 16: Lecture17 arrays.ppt

int mat[3][3] = { {0}, {0}, {0} };

● int mat[3][3] = { {1},

{7,6},{5}

};

int mat[3][3] = { {0}, {0}, {0} };

Page 17: Lecture17 arrays.ppt

● int mat[3][3] = { 0,0,0};