I am writing a program that is meant to read in two arrays and find the difference between the two (elements found in set A but not in set B).
The sets are stored using arrays of 1s and 0s (1s for elements that exist and 0s for elements that don't). I have the following code written and can't seem to understand why I am getting these warnings
warning: comparison between pointer and integer [enabled by default]
if(p==1 && q==0)
^
warning: assignment makes pointer from integer without a cast [enabled by default]
set_difference = 1;
I have the following code written. It will not return a value, either.
#define N 10
void find_set_difference(int *set_a, int *set_b, int n, int *set_difference);
int main(void)
{
int i, k;
int n;
printf("Enter the number of elements in set A: \n");
scanf("%d", &n);
int a[n];
printf("Enter the elements in set A: \n");
for(i=0; i<n; i++){
scanf("%d", &a[k]);
a[k] = 1;
}
printf("Enter the number of elements in set B: \n");
scanf("%d", &n);
int b[n];
printf("Enter the elements in set B: \n");
for(i=0; i<n; i++){
scanf("%d", &b[k]);
b[k] = 1;
}
int set_dif[N];
find_set_difference(a, b, N, set_dif);
printf("The difference of set A and set B is: \n");
for(i=0;i<10;i++){
if(set_dif[i]==1)
printf("%d ",i);
}
return 0;
}
void find_set_difference(int *set_a, int *set_b, int n, int *set_difference){
int *p, *q;
for(p=set_a; p<set_a+n; p++){
for(q=set_b; q<set_b+n; q++){
if(p==1 && q==0)
set_difference = 1;
else
set_difference = 0;
}
}
}
Any assistance with formatting and using pointers would be helpful, as I am still new to coding and am having difficulty understanding the concepts.
b[k]=1will set the value 1 whatever you input before.set_differenceis a pointer to an array. What are you expectingset_difference = 1;to do? I think you want to assign to an element of the array, not the array itself.kbefore&a[k]. I think you meant&k.nis more than10? Why don't you use the same size forset_difasa?bto the function. You need this to be in different variables.