-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintersection.c
More file actions
96 lines (79 loc) · 2.28 KB
/
Copy pathintersection.c
File metadata and controls
96 lines (79 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// #include<stdio.h>
// void intersection(int[],int , int[],int,int[]);
// int main(){
// int a[100],b[100],m,n,c,inter[100];
// printf("Input number of elements in first array\n");
// scanf("%d", &m);
// printf("Input %d integers\n", m);
// for (c = 0; c < m; c++) {
// scanf("%d", &a[c]);
// }
// printf("Input number of elements in second array\n");
// scanf("%d", &n);
// printf("Input %d integers\n", n);
// for (c = 0; c < n; c++) {
// scanf("%d", &b[c]);
// }
// intersection(a,m,b,n,inter); return 0;
// }
// void intersection(int a[],int m,int b[],int n,int inter[]){
// int i=0,j=0,k=0;
// while(i<=m && j<=n){
// if(a[i]<b[j]){
// i++;
// }
// else if(a[i]==b[j]){
// inter[k]= a[i];
// i++;
// j++;
// k++;
// }
// else{
// j++;
// }
// }
// printf("intersection of array\n");
// for(int l=0; l<k;l++){
// printf("%d",inter[k]);
// }
// }
//16 Intersection
#include <stdio.h>
void intersection(int a[], int m, int b[], int n, int inter[]);
int main() {
int a[100], b[100], m, n, c, inter[100];
printf("Input number of elements in first array: ");
scanf("%d", &m);
printf("Input %d integers for first array:\n", m);
for (c = 0; c < m; c++) {
scanf("%d", &a[c]);
}
printf("Input number of elements in second array: ");
scanf("%d", &n);
printf("Input %d integers for second array:\n", n);
for (c = 0; c < n; c++) {
scanf("%d", &b[c]);
}
intersection(a, m, b, n, inter);
return 0;
}
void intersection(int a[], int m, int b[], int n, int inter[]) {
int i = 0, j = 0, k = 0;
while (i < m && j < n) {
if (a[i] < b[j]) {
i++;
} else if (a[i] == b[j]) {
inter[k] = a[i];
k++;
i++;
j++;
} else {
j++;
}
}
printf("Intersection of the arrays:\n");
for (int l = 0; l < k; l++) {
printf("%d ", inter[l]);
}
printf("\n");
}