Write a Modular C Programming Code for Merging of Two 1D Array?
CODE:
#include<stdio.h> void readArray(int[],int); void merge(int[],int[], int , int ); void readArray(int[],int ); int main() { int n1,n2; int arr1[100], arr2[100]; printf("Enter the size of 1st Array: "); scanf("%d",&n1); printf("Enter the elements of 1st array: \n"); readArray(arr1,n1); printf("Enter the size of 2nd Array: "); scanf("%d",&n2); printf("Enter the elements of 2nd array: \n"); readArray(arr2,n2); merge(arr1,arr2,n1,n2); } void readArray(int arr[],int n) { int i; for(i=0;i<n;i++) { scanf("%d",&arr[i]); } } void displayArray(int arr[],int n) { int i; for(i=0;i<n+1;i++) { printf("%d ",arr[i]); } } void merge(int arr1[], int arr2[], int n1, int n2) { int n3, i, j; n3 = n1 + n2; int c[n3]; for(i = 0; i < n1; i++) { c[i] = arr1[i]; } for(i = 0, j = n1; j < n3 && i < n2; i++, j++) { c[j] = arr2[i]; } printf("\n arr[%d] Array Elements After Merging \n", n3); for(i = 0; i < n3; i++) { printf(" %d \t ",c[i]); } return 0; }
OUTPUT
Enter the size of 1st Array: 5 Enter the elements of 1st array: 1 2 3 4 5 Enter the size of 2nd Array: 5 Enter the elements of 2nd array: 6 7 8 9 10 arr[10] Array Elements After Merging 1 2 3 4 5 6 7 8 9 10 Process returned 0 (0x0) execution time : 17.930 s Press any key to continue.
Please find some more codes of 1D Arrays, 2D Arrays, Pointers on the below page:
Top 100+ C Programming codes – KLE Technological University