In this article, you will learn to work with arrays. You will learn to declare, initialize and, access array elements, types of an array with the help of examples.
ARRAY
An array is a collection of a homogenous data elements For example: if you want to store 100 integers in sequence, you can create an array for it.
for example- int data[100];
DECLARATION OF ARRAY-
To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array.
SYNTAX(DECLARATION)
FOR EXAMPLE- int mark[5];
Here, we declared an array, mark of type int and size 5 means it can
hold 5 integer type values.
HOW TO ACCESS ELEMENTS OF AN ARRAY -
We can access elements of an array by its indices.
SUPPOSE WE DECLARE A MARK ARRAY -
HOW TO INITIALIZE AN ARRAY:
EXAMPLE
2)MULTI DIMENSIONAL ARRAY:
For example-
HOW TO ACCESS TWO DIMENSIONAL ARRAY
An array is a collection of a homogenous data elements For example: if you want to store 100 integers in sequence, you can create an array for it.
for example- int data[100];
DECLARATION OF ARRAY-
To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array.
SYNTAX(DECLARATION)
ARRAY DECLARATION REQUIREMENTS
Here, we declared an array, mark of type int and size 5 means it can
hold 5 integer type values.
HOW TO ACCESS ELEMENTS OF AN ARRAY -
We can access elements of an array by its indices.
SUPPOSE WE DECLARE A MARK ARRAY -
HOW TO INITIALIZE AN ARRAY:
There are two method for initialize an array:
1)Directly:
In this method we specified the array directly for e.g-
int mark[5] = {85,90,75,88,65};
Now, compiler will assign the set of values to particular element of the array.
int mark[0]= 85;
int mark[1]= 90;
int mark[2]= 75;
int mark[3]= 88;
int mark[4]= 65;
2)Indirectly:
In this method we specified the array indirectly means we doesn't
provide a size of an array for e.g- int mark[ ]={85,90,75,88,65};
EXAMPLE TO IMPLEMENT ARRAY-
OUTPUT
TYPES OF ARRAY-
1)ONE DIMENSIONAL ARRAY:
a)Array having only one subscript variable is called One-Dimensional array.
b)It is also called as Single Dimensional Array or Linear Array.
SYNTAX (DECLARATION)
2)MULTI DIMENSIONAL ARRAY:
a)Array having more than one subscript variable is called multi
dimensional array.
b)Multi Dimensional Array is also known as Matrix.
SYNTAX
For example-
int A[3,4];
Here, A is a two-dimensional (2d) array.The array can hold 12 elements.
You can think the array as table with 3 row and each row has 4 column.
You can think the array as table with 3 row and each row has 4 column.
HOW TO ACCESS TWO DIMENSIONAL ARRAY
EXAMPLE
OUTPUT
0 Comments