Advertisement
Guest User

Untitled

a guest
Jul 27th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int *common(int a[], int b[], int size, int *plen)
  5. {
  6. int i=0,j=0,count=0;
  7. int *arr;
  8. int k=0;
  9. *plen=0;
  10.  
  11.  
  12. for (i=0;i<size;i++)
  13. {
  14. for (j=0;j<size;j++)
  15. {
  16. if (a[i]==b[j]){
  17. (*plen)++;
  18. }
  19. }
  20. }
  21. if (*plen==0)
  22. return NULL;
  23.  
  24. arr=(int*)malloc((*plen)*sizeof(int));
  25. if (arr==NULL)
  26. {
  27. printf("error arr\n");
  28. return 0;
  29. }
  30.  
  31. for (i=0;i<size;i++){
  32. for (j=0;j<size;j++){
  33. if (a[i]==b[j]){
  34. arr[k]=a[i];
  35. k++;
  36. }
  37. }
  38.  
  39. }
  40.  
  41. return arr;
  42.  
  43. }
  44. int main()
  45. {
  46. int a[5]={1,2,3,4,5};
  47. int b[5]={1,4,6,8,7};
  48. int p=0;
  49. int *i;
  50. int j=0;
  51. i=common(a,b,5,&p);
  52. for (j=0;j<p;j++)
  53. printf("%d,",i[j]);
  54.  
  55. return 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement