Social Icons

Tuesday 14 January 2014

ARRAY


INTRODUCTION  OF ARRAY

It is defined as number of memory locations,each of which can store the same data type and which can be references through the same variable name.


Arrays and pointers have a special relationship as arrays use pointers to  reference  memory  locations .

let us consider the  following example
#include<stdio.h>
main()
{
int a=2;
a=4;
printf("%d",a);
}

output:- 4

In the above example a value is printed 4 before that we  assigned  int a=2;
when we declared a=4 then the a value is replaced by new number that is 2
from these we can know that ordinary  variables are capable of storing
one variable at a time.but in application we have to assign more than 
one valueso they have introduced  new concep't called arrays.

declaration of  array

int a[6];

the compiler understand that a is integer type of array and it stores 6 integers
and the compiler gives 2 bites of memory for each integer array element
 example declaration of array in different data types

  • char ch[40];
  • float real[90];
  • long no[9];




Initializing OF Arrays


  • generally arrays are declared within the function
  • size of an array is declared in [n]
  • the values of  the array can be declared in the{}
  • the array should be end with a ;
For example:

int  a[5] = {20,25,82,10,155}; 


  • The size of a array elements will started with 0 and ends with  n-1                                            example 

                        int a[5] = {3,67,77,6,8};

calling array elements takes place as given below
  1. a[0]refers to 1st element i.e 3
  2. a[1]refers to 1st element i.e 67
  3. a[2]refers to 1st element i.e 77
  4. a[3]refers to 1st element i.e 6
  5. a[4]refers to 1st element i.e 8

DIFFERENT TYPES OF ARRAY'S
  • ONE DIMENSIONAL ARRAY
  • TWO DIMENSIONAL ARRAY
  • THREE DIMENSIONAL ARRAY

  1.  ONE DIMENSIONAL ARRAY

Let us understand the one dimensional array with the help of an example.

void main()
{
int array[5],x;
printf(“Enter any 5 values\n”);
for(i=0;i<5;i++)
{
scanf(“%d”,&array[x]);
}
printf(“The entered values are:\n”);
for(i=0;i<10;i++)
{
printf(“%d”,array[x])
}
}

Description:-

int array[5] which is declared in the above program is capable of holding 5 integer values.
  • The first array value can be accessed by using array[0].
  • The Second array value  can be accessed  by using array[1].
  • similarly the third array value can be accessed by using array[3].
  • It continuous up to array[n-1]
When we not declared the sizeof an array.It produces  a garbage value

for(i=0;i<5;i++)
{
 scanf(“%d”,&array[i])
}
The above for loop accepts the value from the user and stores it in an array. The array that was declared before is getting defined here.
Thus,
     First value corresponding to 1st iteration of for loop is stored in the element arr[0]
     Second value corresponding to 2nd iteration of for loop is stored in the element arr[1]
     Third value corresponding to 3rd iteration of for loop is stored in the element arr[2]

for(i=0;i<3;i++)
{
     printf(“%d\t”,array[i])
}
The above for loop displays the value stored in the array.
Thus,
     1st iteration displays value stored in array[0
     2nd iteration displays value stored in array[1]
     3rd iteration displays value stored in array[2]

In the above program, we have used data type int for the array. Hence it is called integer array. We can also declare array as float. But then it will contain float values. An array element can only hold one type of data i.e. either integer or float or character.

Consider this program,
void main(){
     int arr[3];
     arr[0] = 1;
     arr[1] = 2;
     arr[2] = 3;
}
Let us suppose that base address of this array is 6000. So value 1 is stored at memory location 6000. Since it is integer array, 16 bit C compiler will assign 2 bytes of memory for its storage. Hence next value i.e. 2 will be stored at location 6002. Similarly, 3 will be stored at location 6004.

An array can be defined as follows:
int arr[3]={1,2,3};
i.e., arr[0] --> 1
       arr[1] --> 2
       arr[2] --> 3

An array can also be defined as:
int arr[]={1,2,3}


  1. TWO DIMENTIONAL ARRAY:-


Let us understand what two dimensional arrays are. Consider the following matrix.

       1      2      3
A=  4      5      6
       7      8      9

The above mentioned matrix is 3 by 3. The matrix elements can be accessed using the formula A[m,n], where m represents row number and n represents column number.
Thus, the first element i.e. 1 is represented as A[0,0].
Similarly,
A[0,1] = 2
A[0,2] = 3
A[1,0] = 4
A[1,1] = 5 and so on.
The 2-dimensional array follows the similar concept. So we can declare 2-dimensional array for above matrix as A[3][3].

We can define 2-dimensional array as follows.
int A[3][3]={1,2,3,4,5,6,7,8,9}
Element 1 can be referred as A[0][0]
Element 2 can be referred as A[0][1]
Element 3 can be referred as A[0][2]
Element 4 can be referred as A[1][0]
Element 5 can be referred as A[1][1] and so on.

Another way to define 2-D array is:
int A[3][3]={
                    {1,2,3},
                    {4,5,6},
                    {7,8,9}
                   }
The above mentioned method increases the readability of the matrix.

int A[][] and A[3][] are invalid. We cannot skip the column index in 2-D arrays.
int A[][3] and A[3][3] are both valid.



No comments:

Post a Comment