D Suppose you have two arrays of ints both of length n Write
D. Suppose you have two arrays of ints, both of length n. Write a recursive C++ function that will return true if the arrays are the same (i.e., contain identical values in the identical order), and false otherwise. Specifically, (i) write down the base case(s); (ii) write down the recursive reduction step(s), and (iii) write a C++ function implementing your solution. Thank you so much!
Solution
#include <iostream>
using namespace std;
bool isIdentical(int a[], int b[], int n){
if(n == 0){
return true;
}
else{
if(a[n-1] != b[n-1]){
return false;
}
else{
isIdentical(a,b,n-1);
}
}
}
int main()
{
int a[] = {1,2,3,4,5};
int b[] = {1,2,3,4,5};
int c[] = {1,2,3,6,5};
cout<<\"Array a and b are identical: \"<<isIdentical(a,b,5)<<endl;
cout<<\"Array b and c are identical: \"<<isIdentical(b,c,5)<<endl;
return 0;
}
Output:
sh-4.3$ g++ -std=c++11 -o main *.cpp sh-4.3$ main Array a and b are identical: 1
Array b and c are identical: 0
