Arrays are defined as collection of similar type of data items stored at contigous memory locations.
Array is the simplest data structure where each data element can be randomly accessed by using its index number.
int arr [10]; char arr [10]; float air [s]
void main ()
{
int marks_1 = 56, marks_2=78, marks_3=89;
float avg = (marks_1 + marks_2 + murks_3)/3;
printf (arg);
}
void main()
{
int marks[3] = { 56, 78, 29};
int i;
float arg;
for (i=0;i<3;i++){
arg= arg + marks [i];
}
printf (arg);
}
Algorithm | Average case | Worst case |
---|---|---|
Access | O(1) | O(1) |
Search | O(n) | O(n) |
Insertion | O(n) | O(n) |
Deletion | O(n) | O(n) |
In Array space complexity for worst case is O(n).
Memory Allocation of the Array :-
Each element in Array represented by indexing.Indexing of array can be defined in three ways:
1. 0 (zero Based indexing) :- The first element of the will be arr [0].
2. 1 (one-based indexing) :- The first element of array will be an [1]
3. n ( n-based indexing) :- The first element of array can reside at any random îndex number.
(fig: Int arr [5])
To access any random element of on array we need the following information:
1. Base address of the array,
2. size of an element in bytes.
3. which type of indexing, array follows.
Address of any element of 1D orray can be calculate
Byte address of element A[i] = base address +size*(first-index)
Example: In an array, A[-10.... +2] Base address (BA)=999, size of an element = 2 bytes, find location of A[-1].
The name of the array represents the starting address or the address of the first element of the array.
int summation (int []);
void main()
{
int arr[5] = {0,1,2,3.4};
int sum = summation (arr);
printf ("%d", sum);
}
int summation(int arr[]){
int sum=0,i;
for(i=0;i<5;i++){
sum=sum+arr[i];
}
return sum;
}
2D array can be defined as an array of arrays. The 2D array is organized as matrices which can be represented as collection of rows and coloumns.
The syntax for declaration of two dimensions array is as follows:
int arr [max-rows] [max-columns];
However, it produces the data structure which looks like following:
Due to fact that elements of 2D arrays can be random accessed.
int x=a[i][j];
where i, j are the rows and coloumns respectively.
The Syntax to declare and initialize the 2D array is given as follows:
int arr[2][2] = {0,1,2,3};
Number of elements in 2D arrays = number of rows * number of coloumns.
The size of a two dimensional array is equal to the multiplication of number of rows and number of coloumns present in the array.
A 3x3 two dimensional array is a shown-
There are two main techniques of storing 2D array elements into memory.
1. Row major ordering :-
In row major ordering, all the rows of 2D array are stored into memory contiguously.
2.Column major ordering :
According to column mejor ordering, all the coloumns of 2D aray are stored into the memory contiguously.
1). By row major order :- If array is declared a[m][n] where m is the number of rows while n is number of cloumns then address of an element a[i][j] is calculated as,
Address (a[i][j]) = B. A + (i*n+j) *size.
BA Base Address.
2). By coloumn major order :-
Address (a[i][j] = (j*m) +i)* size+BA
BA Base Address.