Upload
eshikshak
View
9.561
Download
2
Embed Size (px)
DESCRIPTION
arrays in c
Citation preview
ARRAYS
Prakash KhaireLecturer, B V Patel Institute of BMC & IT
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
●Following are types of array●One-dimensional Array●Two-dimensional Array●Multi-dimensional Array
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
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
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};
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
●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”;
●int marks[5]={67,66};
67
66
4646
8978
-545
0
1
2
3
4
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
● 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
● 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
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
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}
};
● int mat[ ][3]={ {1,3,2},{4,7,6},{5,8,9}
};
int mat[3][3] = { {0}, {0}, {0} };
● int mat[3][3] = { {1},
{7,6},{5}
};
int mat[3][3] = { {0}, {0}, {0} };
● int mat[3][3] = { 0,0,0};