Let us see an Array with following points:
- What is an Array?
- Declaration of an Array.
- Types of Array.
- Initialization of an Arrays.
- Solved Case Studies.
Let us see an Array with following points:
- What is an Array?
Array can be defined as follows:
- It is a group of same type of elements.
- It is a set of values of same data type.
- Related data items share the common name .
- Single name can be used to represent collection of same type of items.
E.g Array of int ,Array of float ,Array of char etc.
Array can be defined as follows:
- It is a group of same type of elements.
- It is a set of values of same data type.
- Related data items share the common name .
- Single name can be used to represent collection of same type of items.
E.g Array of int ,Array of float ,Array of char etc.
- Declaration of an Array
Let us see how to declare Array in C Language
Syntax:
data_type Array_name[size_of_array];
This is one dimensional array where
[] are used to specify Array_name is an array and inside bracket we mention the size of array means how many elements are hold by array_name
data_type Array_name[no_of_rows][no_of_column];
This is Two dimensional array
- Types of Array.
The type of an array depends on number of dimensions used.
The types are
- 1-D array(One dimension Array)
- 2-D Array(Two Dimension Array)
- Multi Dimension array
- 1-D array(One dimension Array)
In this data is stored linearly in consecutive locations i.e it reserve consecutive memory locations for specified type of data with size_of_array.
e.g
int roll[10];
It declares an integer array roll with size 10.i.e Array with name roll can store 10 integer elements.
Each element is used with array name and it's subscript/index.
For array index always starts with 0 and ends with (size_of_array-1)
e.g
roll[0] -->This is the first element of an array
roll[1]
.
.
.
roll[9] -->This is the last element of an array.
Memory allocated for this array is calculated as follows:
Assume size of int is 2 bytes
Total consecutive Memory reserved/allocated is 2*10=20 bytes
- 2-D Array(Two Dimension Array)
In this type data is stored in table format by using rows and columns
Syntax:
1) int abc[10][5]; --> abc array has 10 rows and 5 columns
i.e it can store 10*5=50 integer elements into array abc
2) char name[2][10]; -->name array has 2 rows and 10 columns
i.e it can store 2*10=20 character elements into array name
Memory allocation for 2-D array.
Total memory for (rows*column) elements will be allocated.
In above 1st example ,
Assume size of int is 2 bytes
Total consecutive Memory reserved/allocated is 2*50=100 bytes
In above 2nd example ,
Assume size of char is 1 bytes .
Total consecutive Memory reserved/allocated is 1*20=20 bytes.
Total consecutive Memory reserved/allocated is 1*20=20 bytes.
Each element in 2-D is used with array name and it's 2 subscript/index
e.g
abc[0][0] -->this is the first element of an array
abc[0][1]
..
abc[9][4] -->This is the last element of an array
- Multi Dimension array
Similar to 2D this uses multiple dimensions.
e.g
int xyz[2][3][4];
- Initialization of an Arrays
Initialization means assigning initial values to elements in the array i.e at the time of declaration
1-D Array Initialization
type arr-name[size]={comma separated list of values};
e.g
int roll[10]={1,2,3,4,5,6,7,8,9,10};
//it will initialize all 10 elements of roll array
int roll[10]={1,2,3,4};
//it will initialize only 1st 4 elements and remaining to zero(0)
Array Initialization without size
int roll[]={1,2,3,4,5,6};
//It will declare roll array to contain 6 elements with initial vales as per given in list
char name[]={'R','I','Y','A'};
2-D Array Initialization
int roll[2][4]={1,1,1,1,2,2,2,2};
//initialize row by row
//first row to 1 and second row to 2
also written as
int roll[2][4]={{1,1,1,1},{2,2,2,2}};
if value missing in initialization then automatically set to zero.
e,
//first 2 elements of 1st row to 1 and 1st 3 elements of 2nd row to 2 and remaining are set to zerog
int roll[2][4]={{1,1},{2,2,2}};
No comments:
Post a Comment