Created
September 20, 2016 02:11
-
-
Save saiprasad1996/1e4f9d1e3aacd12fd8ed52b4c3fe37cd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Program for array concatination with dynamic memory allocation | |
#include<stdio.h> | |
#include<stdlib.h> | |
int main(){ | |
int *arr1,*arr2; | |
int n1,n2; | |
int i,total,counter; | |
printf("Enter the number of elements to be entered in arr1 and arr2 "); | |
scanf("%d %d",&n1,&n2); //taking the number of elements for the arrays from the user | |
arr1=(int *)malloc(n1 * sizeof(int)); | |
arr2=(int *)malloc(n2 * sizeof(int)); | |
printf("\nEnter the elements for arr1 : "); | |
//Taking array input from the user | |
for(i=0;i<n1;i++){ | |
scanf("%d",&arr1[i]); | |
} | |
printf("\nEnter the elements for arr2 : "); | |
for(i=0;i<n2;i++){ | |
scanf("%d",&arr2[i]); | |
} | |
//Now concatination | |
arr1=(int *)realloc(arr1,(n1+n2)*sizeof(int)); | |
total=n1+n2; | |
counter=0; | |
for(i=n1;i<total;i++){ | |
arr1[i]=arr2[counter]; | |
counter++; | |
} | |
printf("\nArray1 after concatination : "); | |
for(i=0;i<total;i++){ | |
printf("%d ",arr1[i]); | |
} | |
getchar(); | |
return 0; | |
} | |
/* | |
OUTPUT : | |
Enter the number of elements to be entered in arr1 and arr2 2 | |
3 | |
Enter the elements for arr1 : 7 | |
8 | |
Enter the elements for arr2 : 9 | |
4 | |
5 | |
Array1 after concatination : 7 8 9 4 5 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment