Array

An Array is a technique of storing more then one value in to a single variable.This variable is basically an array of different contegous memory location can hold a discte value.
Upon the basis of usage, array are of following three types-
  1. One Dimentional
  2. Two Dimentional
  3. Multi Dimentional


One Dimentional Array:-A 1D array is nothing but simple straight array.
Declaration:- Data type arrayname [array-length];
Example int arr[5];
It will reserve a memory like this.


Initialization:-


  1. via assignment:- int a[5];
    a[5]={3,4,6,7,9};

  2. via scanf():-for(i=0;i<=4;i++)
    {
    scanf("%d",&a[i]);
    }



  3. Write a program that input an integer array of 5 element and display is its content.
    via assignment:-
    #include< stdio.h>
    #include< conio.h>
    void main()
    {
    int a[5];
    clrscr();
    a[5]={1,3,5,8,6};
    printf("%d",a[0],a[1],a[2],a[3],a[4]);
    getch();
    }

    via scanf():-
    #include< stdio.h>
    #include< conio.h>
    void main()
    {
    int a[5],i;
    clrscr();
    printf("Enter five number");
    for(i=0;i<=4;i++)
    {
    scanf("%d",&a[i]);
    }
    printf("Following are your element");
    for(i=0;i<=4;i++)
    {
    prointf("%d",a[i]);
    }
    getch();
    }

    Finding maximum in an array.
    #include< stdio.h>
    #include< conio.h>
    void main();
    {
    int a[5],I,max=-32768;
    printf("enter 5 elements :");
    for(i=0;i<5;i++)
    {
    sacnf("%d",&a[i]);
    }
    for(i=0;i<5;i++)
    {
    if(a[i>max])
    {
    max=a[i];
    }
    printf("The maximum is %d",max);
    getch();
    }

    Sorting an array.



    Selection Sorting:-

    #include< stdio.h>
    #include< conio.h>
    void main()
    {
    int a[5],I,j,t;
    printf("Enter 5 elements");
    for(i=o;i<5;i++)
    {
    scanf("%d",a[i]);
    }
    for(i=0;i<5;i++)
    {
    for(j=i+1;j<5;j++)
    {
    if(a[j] {
    t=a[j];
    a[j]=a[i];
    a[i]=t;
    }
    }
    }
    for(i=0;i<5;i++)
    {
    printf("%d",a[i]);
    }
    getch();
    }


    Bubble Sorting.

    #include< stdio.h>
    #include< conio.h>
    void main()
    {
    int a[5],I,j,t;
    printf("Enter 5 elements.");
    for(i=0;i<5;i++)
    {
    scanf("%d",a[i]);
    }
    for(i=0;i<5;i++)
    {
    for(j=i+1;j<5;j++)
    {
    if(a[j]>a[j+1])
    {
    t=a[j];
    a[j]=a[i];
    a[i]=t;
    }
    }
    }
    for(i=0;i<5;i++)
    {
    printf("%d",a[i]);
    }
    getch();
    }

    Inserting an element in an Array:-

    #include< stdio.h>
    #include< conio.h>
    void main()
    {
    int a[15],i,p,num,n;
    printf("Enter the length of array");
    sacnf("%d",&n);
    printf(Enter %d element",n);
    for(i=0;i {
    scanf("%d",a[i]);
    }
    printf("Enter number and position ");
    scanf("%d %d",&num&p);
    for(i=n-1;i>=p-1;i--)
    {
    a[i+1]=a[i];
    }
    a[p-1]=num;
    for(i=0;i {
    printf("%d",a[i]);
    }
    getch();
    }

    Deleting an element in an array.

    #include< stdio.h>
    #include< conio.h>
    void main()
    {
    int a[15],i,p,num,n;
    printf("Enter the length of Array");
    sacnf("%d",&n);
    printf("Enter %d Element",n);
    for(i=0;i {
    scanf(v,a[i]);
    }
    printf("Enter number and position.");
    scanf("%d",&num&p);
    for(i=p-1;i>=n-1;i++)
    {
    a[i]=a[i+1];
    }
    for(i=0;i {
    printf("%d" ,a[i]);
    }
    getch();
    }

    2D Array

    In 2D array Is a special array that can hold a grid like data. These data are represented in terms of rows and colums.

    Declaration of 2D array: int st[5][5];


    The above array allocated 50 bytes (25*2bytes) in the memory array will look like.



    Initialization of a 2D array:-


    1. Using assignment operator.

    2. int arr[3][4]={{3,2,6,8},{1,6,9,3},{7,4,6,8}};
      For an array of M row and N colums the assignment of element will be in M group each of N elements.

      Example
      #include< stdio.h>
      #include< conio.h>
      void main()
      {
      int arr[3][4]={{3,2,6,8},{1,6,9,3},{7,4,6,8}};
      for(i=0;i<3;i++)
      {
      for(j=0;j<4;j++)
      {
      printf("%d",arr[i][j]);
      }
      printf("\n");
      }
      getch();
      }

    3. Filling of a 2D array via scanf() statement.

    4. #include< stdio.h>
      #include< conio.h>
      void main()
      {
      int arr[3][4],i,j;
      printf("Enter element of the Array");
      for(i=0;i<3;i++)
      {
      for(j=o;j<4;j++)
      {
      scanf("%d",arr[i][j]);
      }
      }
      for(i=0;i<3;i++)
      {
      for(j=0;j<4;j++)
      {
      printf("%d",arr[i][j]);
      }
      printf("\n");
      }
      getch();
      }

    Transpose of a matrix.





    #include< stdio.h>
    #include< conio.h>
    void main()
    {
    int a[3][4],b[4][3];
    printf("Enter 12 element");
    for(i=0;i<3;i++)
    {
    for(j=0;j<4;j++)
    {
    scanf("%d",a[i][j]);
    }
    }
    ` for(i=0;i<3;i++)
    {
    for(j=0;j<4;j++)
    {
    b[j][i]=a[i][j]
    }
    }
    printf("The element entered are.");
    for(i=0;i<4;i++)
    {
    for(j=0;j<3;j++)
    {
    printf("%d",b[i][j]);
    }
    printf("\n");
    }
    getch();
    }

    Multiplication of Matrix:-

    If we want to multiply two matrixes, the following two rules must be followed.
    1. Number of rows in first matrix must be equal to number of columns in second matrix.
    2. The result matrix will be of size number of rows of first matrix * number of columns of second matrix.


    Program for multiplication of array:-

    #include< stdio.h>
    #include< conio.h>
    void main()
    {
    int a[10][10],b[10][10],c[10][10],i,j,k,sum=0,r1,r2,c1,c2;
    clrscr();
    printf("Enter row and col. Of first matrix");
    scanf("%d%d",&r1,&c1);
    printf("Enter row and col. Of second matrix");
    scanf("%d%d",&r2&c2);
    if(r1!=c2&&r2!=c1)
    {
    printf("Sorry! Multiplication is not possible");
    }
    else
    {
    printf("Enter element of first matrix");
    for(i=0;i {
    for(j=0;j {
    scanf("%d",a[i][j]);
    }
    }
    printf("Enter element of second matrix");
    for(i=0;i {
    for(j=0;j {
    scanf("%d"&b[i][j]);
    }
    }
    for(i=0;i {
    for(j=0;j {
    for(k=0;k {
    sum=sum+a[i][k]*b[k][j];
    }
    c[i][j]=sum;
    sum=0;
    }
    }
    for(i=0;i {
    for(j=0;j {
    printf("%d",c[i][j]);
    }
    printf("\n");
    }
    getch();
    }