Array in 'C' Language Part II

Applications of an Array :
Let's summarize some important applications of array :

1) Linear Search :
  • Linear search is process of searching for an element in array by comparing respective element with each element of array sequentially.
  • If match is found indicates element is present in array thus search process must stop there. 
  • If match is not found then search must end on last element of array by concluding that element is not present in array.
  • Sample program :

#include <stdio.h>
int main()
{
    int key,num[10],i,n,flag=0;
    //accepting size of array
    printf("\n Enter How many elements in array : ");
    scanf("%d",&n);
//Accepting array elements 
for(i=0;i<n;i++)
{
   printf("\n Enter %d th element :",i);
   scanf("%d",&num[i]);
}
//displaying array
    printf ("\n Elements in array are:\n");
for(i=0;i<n;i++)
   printf(" %d ",num[i]);
printf ("\n Enter element to be search in above array   :");
scanf("%d",&key);
//searching process
for(i=0;i<=n;i++)
{
    if(num[i]==key)
    {
        //changinxg flag since element found
        flag=1;
       //stop search
        break;
     }
}
if(flag==1)
    printf ("\n %d is found in array ",key);
else
     printf("\n %d is not found in array",key);
return 0;
}

Output 1:
Enter How many elements in array : 6
Enter 0 th element : 76
Enter 1 th element : 55
Enter 2 th element : 7
Enter 3 th element : 98
Enter 4 th element : 14
Enter 5 th element : 78
Elements in array are:
   76    55   7   98   14   78
Enter element to be search in above array   : 55
55 is found in array


Output 2 :
Enter How many elements in array : 6
Enter 0 th element : 76
Enter 1 th element : 55
Enter 2 th element : 7
Enter 3 th element : 98
Enter 4 th element : 14
Enter 5 th element : 78
Elements in array are:
   76    55   7   98   14   78
Enter element to be search in above array   : 65
65 is not found in array

2) Storing and displaying Matrix :

  • Matrix is simply a two dimensional array having 'm' rows and 'n' columns in it.
  • We can perform various matrix operations with the help of array.
  • Sample program for storing and displaying matrix is as follow :
#include<stdio.h>
int main() {
    int mat[10][10];
    int row,col,i,j;
    //accepting size of matrix
    printf("\n Enter how many rows and columns in matrix:");
    scanf("%d%d",&row,&col);
    //accepting elements of matrix
    for(i=0;i<row;i++)
    {
        for(j=0;j<col;j++)
        {
            printf("\n Enter element at %d %d",i,j);
            scanf("%d",&mat[i][j]);
        }
    }
 
    //displaying matrix
    printf("\n Matrix is :\n");
    for(i=0;i<row;i++)
    {
        for(j=0;j<col;j++)
         printf("\t%d",mat[i][j]);
    }
    return 0;
}

Output :
Enter how many rows and columns in matrix: 2
2
Enter element at 00 45
Enter element at 01 67
Enter element at 10 78
Enter element at 11 98
Matrix is :
      45      67     78     98


3) Addition of Matrix :

  • Addition of matrix can be performed only if they are of same order that means if number of rows and columns in both matrices is same.
  • Program 

#include<stdio.h>

int main() {

    int mat1[10][10],mat2[10][10],add[10][10];

    int row,col,row2,col2,i,j;

    //accepting size of matrix1

    printf("\n Enter how many rows and columns in matrix1:");

    scanf("%d%d",&row,&col);

    //accepting elements of matrix2

    for(i=0;i<row;i++)

    {
        for(j=0;j<col;j++)

        {
            printf("\n Enter element at %d %d: ",i,j);
            scanf("%d",&mat1[i][j]);

        }

    }

   

    //displaying matrix1

    printf("\n Matrix1 is :\n");

    for(i=0;i<row;i++)

    {

        for(j=0;j<col;j++)
         printf("\t%d",mat1[i][j]);
      printf("\n");
    }
    
    printf("\n Enter how many rows and columns in matrix2:");

    scanf("%d%d",&row2,&col2);

    //accepting elements of matrix2

    for(i=0;i<row2;i++)

    {

        for(j=0;j<col2;j++)

        {

            printf("\n Enter element at %d %d: ",i,j);

            scanf("%d",&mat2[i][j]);

        }

    }

   

    //displaying matrix2

    printf("\n Matrix is :\n");

    for(i=0;i<row2;i++)

    {

        for(j=0;j<col2;j++)

         printf("\t%d",mat2[i][j]);
      printf("\n");
    }
    
    //to add matrices
    if(row!=row2 || col!=col2)
    {
        printf ("\n Addition not possible since orders of matrices is not same");
        return 0;
    }
    
    for(i=0;i<row;i++)

    {
        for(j=0;j<col;j++)
          add[i][j]=mat1[i][j]+mat2[i][j];

    }

   
    //displaying resulting matrix

    printf("\n Resulting Matrix is :\n");

    for(i=0;i<row;i++)

    {

        for(j=0;j<col;j++)
         printf("\t%d",add[i][j]);
        printf("\n");
    }
    return 0;

}



Output 1 :
Enter how many rows and columns in matrix1: 2

2

Enter element at 0 0: 12

Enter element at 0 1: 5

Enter element at 1 0: 6

Enter element at 1 1: 8

Matrix1 is :

12 5

6 8

Enter how many rows and columns in matrix2: 2

3

Enter element at 0 0: 56

Enter element at 0 1: 12

Enter element at 0 2: 43

Enter element at 1 0: 7

Enter element at 1 1: 8

Enter element at 1 2: 9

Matrix is :

56 12 43

7 8 9

Addition not possible since orders of matrices is not same


Output 2:

Enter how many rows and columns in matrix1: 2

2

Enter element at 0 0: 12

Enter element at 0 1: 5

Enter element at 1 0: 6

Enter element at 1 1: 8

Matrix1 is :

12 5

6 8

Enter how many rows and columns in matrix2: 2

2

Enter element at 0 0: 56

Enter element at 0 1: 12

Enter element at 1 1: 7

Enter element at 1 1: 8
Matrix is :

56 12

7 8

Resulting Matrix is:
68 17
13 16


3) Sorting of Array :

  • Sorting is a process of arranging elements in specific order either increasing or decreasing.
  • Let's see sample program to sort elements of array in increasing order.
#include<stdio.h> int main()
{ int num[10]; int n,i,j,temp; //accepting size of matrix printf("\n Enter how many elements in matrix:"); scanf("%d",&n); //accepting elements of matrix for(i=0;i<n;i++) { printf("\n Enter element at %d : ",i); scanf("%d",&num[i]); } //displaying matrix printf("\n Matrix is :\n"); for(i=0;i<n;i++) { printf("\t%d",num[i]); } //Sorting array elements for(j=n;j>=1;j--) { for(i=0;i<j-1;i++) { if(num[i]>num[i+1]) { //exchanging values to sort them temp=num[i]; num[i]=num[i+1]; num[i+1]=temp; } } } //displaying sorted array printf("\n Sorted array is : \n"); for(i=0;i<n;i++) printf("\t %d ",num[i]); return 0;
}

Output :
Enter how many elements in matrix:6 Enter element at 0 : 12 Enter element at 1 : 3 Enter element at 2 : 56 Enter element at 3 : 18 Enter element at 4 : 6 Enter element at 5 : 33 Matrix is : 12 3 56 18 6 33 Sorted array is : 3 6 12 18 33 56



Comments

Popular posts from this blog

Write a program to allocate memory dynamically for n integers. Accept the elements and calculate their sum and average

C program to accept names of n cities and search for city named “Pune”.