�Dr. S. Sridevy
Associate Professor (CS)
Dr. M. Ramanan
Teaching Assistant (CS)
Dept. of PS&IT, AEC&RI, TNAU, Coimbatore -3.
Concept of Arrays: One-Dimensional - Two Dimensional arrays
COM 102 Theory Schedule 8
Concept of Arrays
S. Sridevy
6/3/2022
Arrays
S. Sridevy
6/3/2022
Why need to use array type?�
int studMark0, studMark1, studMark2, ..., studMark999;
S. Sridevy
6/3/2022
Arrays
int studMark[1000];
S. Sridevy
6/3/2022
Dimensions in Array
S. Sridevy
6/3/2022
One / Single Dimensional Array
Eg: int age[5];
and the computer reserves five storage locations as shown below
S. Sridevy
6/3/2022
One / Single Dimensional Array
S. Sridevy
6/3/2022
One / Single Dimensional Array
The general form of array declaration is
data_type array-name[size];
Eg: 1. int age[50];
2. float height[50];
S. Sridevy
6/3/2022
One / Single Dimensional Array
Example: char name[10];
values to array elements can be assigned as follows
strcpy(name,”Raj Kumar”);
S. Sridevy
6/3/2022
When the compiler sees a character string, it terminates it with an additional null character (‘\0’) at the end
One / Single Dimensional Array
The general form of initialization of arrays is:
static data_type array-name[size] = {list of values};
Eg:1 static int number[3] = {0,0,0};
Eg:2 static float total [5] = {0.0,12.50,-15};
Will initialize the first three elements to 0.0, 12.50 and -15.0 and the remaining two elements to zero.
S. Sridevy
6/3/2022
One / Single Dimensional Array
Eg: 3 static int count[ ] = {1,1,1,1};
If size may be omitted, the compiler allocates enough space for all initialized elements.
Eg: 4 static char name [ ] = {‘T’,’N’,’A’,’U’,’\0’};
S. Sridevy
6/3/2022
Two Dimensional Array
Declaration of Two-Dimensional Array
Two-dimensional arrays are declared as follows
data_type array-name[row_size][column_size];
S. Sridevy
6/3/2022
Two Dimensional Array
S. Sridevy
6/3/2022
Example 1 : int a[3][3];
Two Dimensional Array
Initialization of Two – Dimensional Arrays
The general form of initialization of arrays is:
static data_type array name[row_size][column_size] = {list ofvalues};
Eg: 1) static int number[2][3] = {0,0,0,1,1,1};
Or
static int number[2][3] = { {0,0,0},{1,1,1} };
Or
static int number[2][3] = {
{0,0,0}, {1,1,1}
};
Multi Dimensional Array
The general form of a multidimensional array is
data_type array-name[S1][S2][S3] …. [Sn];