fork download
  1. #include <stdio.h>
  2.  
  3. // scan sorted array a and b, put intersection in c.
  4. void set_intersection( int *a, int asize, int *b, int bsize, int *c, int *csize) {
  5. int i = 0, j = 0, k = 0;
  6.  
  7. while ( i < asize && j < bsize ) {
  8. if ( a[i] == b[j] ) {
  9. c[k] = a[i];
  10. ++k;
  11. ++i;
  12. ++j;
  13. }
  14. else if ( a[i] < b[i] )
  15. ++i;
  16. else
  17. ++j;
  18. }
  19. *csize = k;
  20. }
  21.  
  22. int main(void) {
  23. int a[] = {1,3,4,5,7,8,9,12};
  24. int b[] = {2,3,5,6,7,10,11,12};
  25. int c[10];
  26. int size = 0;
  27.  
  28. set_intersection(a, 8, b, 8, c, &size);
  29.  
  30. int i;
  31. for ( i = 0; i < size; ++i ) {
  32. printf("%d ",c[i]);
  33. }
  34. return 0;
  35. }
  36.  
  37.  
Success #stdin #stdout 0s 2112KB
stdin
Standard input is empty
stdout
3 5 7 12