Arrays
Array- an array is a collective name given to a group of
similar elements whereas a variable is an entity that may
change during program execution.
An array is a variable that is capable of holding
many values. For example, an array initialization would
be, int number[8];
For which there are 8 storage locations reserved where 8
different values can be stored belonging to same type.
Whereas an ordinary variable initialization variable
would be, int number;
And only one storage location is reserved and can hold
only one value at a time.
Advantages of using arrays:
*Array is a simplest kind of data structure
*it is easy to create, understand & implement arrays
*In arrays direct access to any element is possible.
However, modifications done to one element does not
effect the other element.
*Arrays have the capability of linking together especially
with multiple dimension
*Every array element can be accessed at constant time.
*Array variables are capable of storing more no. Of
values.
One dimensional array:
It is the simplest form of array in which list of
elements are stored in contiguous memory locations and
are accessed using only one subscript.
Declaration of one dimensional array:
Datatype array-name [size]
datatype specifies the data type of array elements
array-name indicates the name of array variable
size specifies the no. Of elements in the array
Eg: int a [10]
Accessing elements in the array:
Once the arrays are declared, every individual
element can be accessed using the subscript which is an
integer expression or integer constant. The subscript
specifies the position of element in the array this is called
index of array element. The array subscript start at 0
and ends at size of array -1.
Array-name [i]....eg: ar [0],ar [1]
Printing one dimensional array:
Array elements can be printed by using
forloop and a printf statement.
Eg: int a [5]={10,20,30,40,50};
For(int i=0;i <=5;i++)
Printf (\not the elements of array are: %d,a [i]);
Initialization of one-dimensional array:
We can initialize array elements at the place of
their declaration itself. The general format of this
initialization is,
Type array-name [size]={list of elements separated
by commas}
Eg: int a [4]={5,10,1,55}
Here ,the size of array is 4,the value 5 is a
assigned to first element i.e..,a [0]=5,the value 10 is
assigned to second element a [1]=10,the value 1 is
assigned to third element a [2]=1 and the value 55 is
assigned to fourth element a [3]=55.
Suppose the list of element specified are less than the
size of array then only that many elements are initialized.
The remaining values are assigned garbage values.
If suppose:
Int xyz []={5,6,7,8,9};
In the above statement, we did not specify the size of an
array. In such cases compiler fixes the size of the array
based on the number of values In the list. In above eg:
since we have 5 values in the list, size of this array is 5
A one-dimensional array program to print array of 20:
#include<stdio.h>
Void main()
{
Int arr [20],i;
For (i=0;i <20;i++)
{
Scanf(%d,&arr [i]);
}
For (i=0;i <20;i++)
{
Printf (%d\n,arr [i]);
}
}
Necessity to give size of an array in array declaration:
Whenever an array is declared, the task of the
compiler is to allocate the base address and withhold
sufficient space in the memory to store all the array
elements. Hence, if the size is given during array
declaration then the compiler will allocate the specified
amount of space in the memory. But if the size is not
given then the compiler will create a big enough space in
the memory that will be wasted if not used.
Two dimensional arrays:
When the data must be stored in the form of a
matrix we use two dimensional arrays
Eg:int a [5][3];
Above declaration represents a two
dimensional array consisting of 5 rows and 3 columns. So
the total no. Of elements which can be stored in this
array are 53 i.e...15
Declaration of two dimensional arrays:
Syntax: type array name[row-size][column-size]
type represents the data type of array elements
array-name is the name of the array
row-size represents no. Of rows in the array
column-size represents no. Of columns in the array
Eg:int xyz [2][5]
Here,xyz is an array consisting of two rows with
each other consisting of 5 integer elements
Float a [5][5]
Here, a is an array consisting of 5 rows with each
row consisting of 5 floating point numbers.
Initialization of two dimensional arrays:
Two dimensional arrays can also be initialized at
the place of declaration itself. The general format of
initializing two dimensional array is,
Type array-name [row-size][column-size]={{list of
elements in row 1},
{List of elements in row 2},
.
.
{List of elements in row n}.
Eg:1.int xyz [2][3]={{2,3,4}, {5,6,7}};
Here 2,3&4 are assigned to 3 columns of 1st row
and 5,6&7 are assigned to 3 columns of 2nd row in order.
2.int xyz [2][3]={{0}, {0}};
Here, 1st elements of each row is initialized to
zero while other elements have some garbage values.