C++, Recursion Hi there, below are recursive problems that I need to implement t
ID: 3863989 • Letter: C
Question
C++, Recursion
Hi there, below are recursive problems that I need to implement the codes and prove them using inductive proof.
(7) Implement a function that determines whether a given int is an element of an array of int:
(8) Using your function in (7), implement a function which determines whether one array is a “subset” of another (that is, whether every element in array a is also an element of array b):
And below are my codes that, I think, need to be fixed.
I think codes for problem #7 is correct, but #8 is not working as it's supposed to.
bool is_element_of(int i, int* array, int length){
if(length < 0)
return false;
if(array[length] == i)
return true;
else
return is_element_of(i, array, length - 1);
}
bool is_subset(int* a, int length_a, int* b, int length_b){
if(!is_element_of(b[length_b], a, length_a))
return false;
else
is_subset(a, length_a, b, length_b -1);
return true;
}
Can someone fix my functions and write out the inductive proof for each of them?
Thank you in advance!
Explanation / Answer
8) implement a function which determines whether one array is a “subset” of another (that is, whether every element in array a is also an element of array b):
bool is_Subset(int arr1[], int arr2[], int m, int n)
{
int k = 0;
int j = 0;
for (k=0; k<n; k++)
{
for (j = 0; j<m; j++)
{
if(arr2[k] == arr1[j])
break;
}
/* If the above inner loop was not broken at all then
arr2[k] is not present in arr1[] */
if (j == m)
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.