Page 84 - DCAP201_FUNDAMENTALS_OF_DATA_STRUCTURES
P. 84

Unit 6: Operations on Arrays and Sparse Matrices




          for (c = n - 1; c >= position - 1; c--)                                               Notes
          array[c+1] = array[c];


          array[position-1] = value;


          printf("Resultant array is\n");


          for (c = 0; c <= n; c++)
          printf("%d\n", array[c]);


          return 0;
          }
          Now we will see how to merge two arrays in C. This is shown in the following program.


                 Example: In this example, there are four functions, that is, one for reading the array
          element, second for writing on console and third for sorting of both array and last for merge of
          two array into one.
          #include<stdio.h>
          #include<conio.h>
          void main()
          {
           void read(int *,int);
           void display(int *,int);
           void sort(int *,int);
           void mergelist(int *,int *,int *,int);
           int a[5],b[5],c[10];
           clrscr();
           printf(“Enter the elements for the first array \n”);
           read(a,5);
           printf(“The elements of first array are : \n”);
           display(a,5);
           printf(“Enter the elements for the second array \n”);
           read(b,5);
           printf(“The elements of second array are : \n”);
           display(b,5);
           sort(a,5);
           printf(“The sorted first array in decending order are :\n”);
           display(a,5);
           sort(b,5);
                 printf(“The sorted second array in decending order are :\n”);
           display(b,5);
           mergelist(a,b,c,5);




                                           LOVELY PROFESSIONAL UNIVERSITY                                   77
   79   80   81   82   83   84   85   86   87   88   89