To compare two arrays, you must start element-by-element comparison from the last element of the array. If the length of the two arrays is not the same, only the number of elements in the shorter length array are compared. Implement the comparison by programming and return the number of unequal elements found in the comparison.
For example:
Array {, 5} and array {, 21, 5} are compared according to the requirements. The number of unequal elements is 0.
Arrays {, 5} and arrays {, 21,} are compared according to the requirements. The number of unequal elements is 3.
Required implementation functions:
Int array_compare (INT len1, int array1 [], int len2, int array2 [])
[Input] int len1: Enter the number of elements in the compared array 1;
Int array1 []: input is compared to array 1;
Int len2: Enter the number of elements in the compared array 2;
Int array2 []: The input is compared to array 2;
[Output] None
[Return] Number of unequal elements, type: int
Example:
1) input: int array1 [] = {, 5}, int len1 = 3, int array2 [] = {, 5}, int len2 = 5
Function return value: 0
2) input: int array1 [] = {, 5}, int len1 = 3, int array2 [] = {,}, int len2 = 6
Function return: 3
C ++ programming implementation:
It should be noted that the char type obtained by using the standard input CIN cannot identify the number greater than 9, but it does not prevent interface function verification.
1 #include <iostream> 2 using namespace std; 3 4 int array_compare(int len1, int array1[], int len2, int array2[]) 5 { 6 int count=0; 7 int len=len1; 8 if (len>len2) { 9 len=len2;10 }11 int i;12 for (i=1; i<=len; i++) {13 if (array1[len1-i]!=array2[len2-i]) {14 count++;15 }16 }17 return count;18 }19 20 int main()21 {22 char s1[50],s2[50];23 int i;24 while (cin>>s1>>s2) {25 int array1[50],array2[50];26 int len1=0,len2=0;27 for (i=0; s1[i]; i++) {28 array1[i]=s1[i]-‘0‘;29 len1++;30 }31 for (i=0; s2[i]; i++) {32 array2[i]=s2[i]-‘0‘;33 len2++;34 }35 cout<<array_compare(len1, array1, len2, array2)<<endl;36 }37 return 0;38 }
Running result:
Huawei machine test-array comparison