Programming World


Array




  1. Array/scripted variable :

  1. An array is a collection of similar element having same datatype.
  2. The first element in the array is numbered from zero(0)
    so that last element is one less than the size of the array.
  3. Before using an array its type and dimension (size)must be declare.
  4. Array elements are always contineous memory location.

NOTE:

An array is collection of similar elements.These similar elements could be all int's,all floats,or all char's etc. Array of characters is called a String. where as an array of int's or floats is called simply an array.


Types of array -
    One diamensional array :
        Syntax :
  1. Array declaration-
datatype arrayname[size]
Example: int marks[30];
  Here 
       int   :   specifies the type of variable.
       marks :  specifies name of variable(i.e array).
       30    : specifies how many elements of the types int will be in array it is also known as diamension.
        [ ]  : bracket tells the compiler that we are dealing with an array.
Array initialization-(at the time of declaration)

Example : 
          int num[6]={ 2,4,5,6,7,8};
          int n[]={2,4,5,6,7,8};
          float press[]={1.1, 1.2, 1.3, 5.6};
          char name[4]={'a', 'b', 'f','\0'};
          char name[5]="bca";

program for one diamensional array:
  1. program for accept your name ,address and rollno.
  
    void main()
       {
          int rollno;
	   char name[20];
	  char add[30];
           clrscr();
         
          printf("enter your rollno=");
	  scanf("%d",&rollno);

          printf("Enter your name=");
	  scanf("%s",name);

	  printf("Enter address= ");
	  scanf("%s",add);

          printf("roll no=%d\n",rollno);
	   printf("\n NAME=%s"\n address=%s",name,add);

	  getch();
     }

   


Two diamensional array :
  1. It is also possible for array to have two or more diamension.The two diamensional array is also called as Matrix.
  2. It is a collection of number of one diamensional arrays placed one below the other.
  3. 2-D array arranged in Rows and columns from where counting of rows and column begin with zero.



Example:
              [ 4 ] [ 2] matrix
             row      column

 
                column 0     column 1 

        row 0   [0] [0]      [0] [1]
        row 1   [1] [0]      [1] [1]
	row 2   [2] [0]      [2] [1]
        row 3   [3] [0]      [3] [1]

Initialization of 2-D array:

Method 1-
           int stud[4][2]={
                            {11,60},
			    {34,56},
			    {45,50},
			    {114,75}
			  };
Method 2-

int stud[4][2]={11,60,34,56,45,50,114,75};


Program to display 2*2 matrix
       void main()
       {
          int a[2][2];
           int i,j;
           clrscr();
                 printf("enter 4 elements for matrix");
		   for(i=0;i<2;i++)
		     {
 			for(j=0;j<2;j++)
		          {
 			     printf("\n enter a[%d][%d]="i,j);
			     scanf("%d"&a[i][j]);
			  }
		     }
		printf("\n matrix :\n");
		for(i=0;i<2;i++)
		    {
			for(j=0;j<2;j++)
			  {
			     printf("%d\t",a[i][j]);
			  }
			printf("\n");
		    }
           getch();
       }


         output : enter 4 elements for matrix
        	  enter a[0][0]=1
	 	  enter a[1][0]=2
         	  enter a[1][0]=3
         	  enter a[1][1]=4

         	matrix :
       			  1   2
        		  3   4

NOTE

While initializing a 2-D array, it is necessary to mention second diamention (i.e columns),whereas first diamension(i.e rows) is optional.
Example : 
         int arr[2][3]={10,20,30,40,50,60};
             or
         int arr[][3]={10,20,30,40,50,60};

More options

Forms of function
Pointer
File handling
function