Need help with this code

Hello, It's my first time in here hopefully I could get some help with this code. I am supposed to input 2 arrays A and B, and then get the elements are in A but not in B and place them in an array (diff) also If there are elements in B not in A the function should return true so here is my code.
I must only use arrays,for loops etc...aka no vectors or anything :)

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
 #include <iostream>
using namespace std;
bool Difference(int n,int x,int *a,int *b,int *diff, int &k){
int z=0;
int l;
for(int i=0;i<n;i++){
	l=4;
	for(int j=0;j<x;j++){
		if(a[i]==b[j]){
			l=5;}}
			if(l==4){
				k++;}
}
diff=new int[k];
for(int i=0;i<n;i++){
	l=4;
	for(int j=0;j<x;j++){
		if(a[i]==b[j]){
			l=5;}}
	if(l==4){
		diff[z]=a[i];
		z++;
}
}
for(int i=0;i<x;i++){
	l=4;
	for(int j=0;j<n;j++){
		if(b[i]==a[j]){
			l=5;}
	}
	if(l==4){
     return true;
		 break;
	}

}
}

int main(){
	int k=0;
	int n,x;
	bool different_in_B;
	int *diff=NULL;
	cout<<"Enter Array A no. of elements:";
	cin>>n;
	while(n<= 0){
		cout<<"Error,Enter no. of elements.";
			cin>>n;
	}
	cout<<"Enter Array B no. of elements:";
	cin>>x;
	while(x<= 0){
		cout<<"Error,Enter no. of elements.";
			cin>>x;
	}
	int *a=new int[n];
	int *b=new int [x];
	
	for(int i=0;i<n;i++){
		cout<<"Enter array 1 Elements :	";
		cin>>a[i];
	}
	
	for(int i=0;i<x;i++){
		cout<<"Enter array 2 Elements :	";
		cin>>b[i];
	}
	different_in_B=Difference(n,x,a,b,diff,k);
	cout<<endl<<"Array diff:"<<endl;
	for(int i=0;i<k;i++){
		cout<<diff[i]<<"	";}
	cout<<endl<<"different_in_B= "<<different_in_B<<endl;
int stop;
	cout<<endl<<"Press any key to continue:";
	cin>>stop;

}


Thanks in advance
Could you try to explain yourself more clearly - what exactly are you trying to do?

Also, in routine Difference() what is the significance (if any) of the variable l ("ell" in arial font!) being 4 or 5 and what value is returned (if any) if l = 5?
sorry for not making myself clear enough, the whole point idea as it's written in my assignment is

Exercise 2
1- Write function “Difference” that takes 2 sorted arrays of integers A and B and outputs the
following:
a. An array of integers “diff” that contains only the elements that are in A AND NOT in
B.
b. Number of elements of “diff” array.
c. Boolean variable “different_in_B” that is true if there are some elements in B that is
not in A (which means that B is NOT a subset of A), false otherwise
Note: A and B maybe have different sizes.
Note: the input arrays must not change.
2- Write a main function that:
a. Asks the user to enter the number of elements of array A
b. If the entered number of elements <= 0, repeat step a,
otherwise go to step c
c. Asks the user to enter the array elements (assume they’re entered sorted
ascendingly; don’t check on their order)
d. Repeats step a, b and d for array B then go to step e
e. Calls “Difference” function and passes A and B as its inputs
f. Prints the output “diff” and the value of the output “different_in_B”


Edit: l is a parameter that shows me whether the element x is doesn't appear in the second array or not , so if it remains 4 this means that a[i] doesn't equal any element in the second array, so I would increase the (diff) array size by 1 and in the second loop, I would add this element in the diff array.

I used 2 for loops as I needed to dynamically locate the array after deciding on its size here
diff=new int[k];
Last edited on
1. To keep things simple (at least in the beginning):
An array can be larger than the number of used positions, so you could
diff=new int[length of A];
That should always provide you with sufficient positions even if non of the values in A is in B.

2. To keep your code easy to read, consider placing all closing curly brackets at the same indentation as the corresponding opening curly bracket. And consider placing the opening curly brackets as far to the left as possible without disturbing the indenting.
Also: for the computer it doesn't matter if you use more sensible names for your variables, but for you it will become more and more helpful if you do.

3. To search for the differences, take a look at this idea (not tested):
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
// assuming diff has at least the same length as a
bool Difference(int length_of_a, int length_of_b, int *a, int *b, int *diff)
{	// first we find the values that are in A and not in B and save those to the diff array.
	int diff_index = 0;
	for (int i=0; i<length_of_a; i++)
	{
		bool match_found = false;
		for (int j=0; j<length_of_b; j++)
		{
			match_found = match_found || (a[i] == b[j]);
		}
		if (match_found == false)
		{
			diff[diff_index] = a[i];
			diff_index++;
		}
	}
	// If you don't like the diff array to be longer than required, you could use a temporary 
	// array in the loop above and make the diff array exactly the required size now. You 
	// would only have to copy the values once more.
	// ==============================================
	// If B contains a value that is not in A, this function should return true.
	for (int i=0; i<length_of_b; i++)
	{
		bool match_found = false;
		for (int j=0; j<length_of_a; j++)
		{
			match_found = match_found || (a[i] == b[j]);
		}
		if (match_found == false)
		{
			return true;
		}
	}
	return false;
}
Topic archived. No new replies allowed.