Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1.7 StdintArray.anyOf Add a function called anyof to stdIntArray, which returns

ID: 3746425 • Letter: 1

Question

1.7 StdintArray.anyOf Add a function called anyof to stdIntArray, which returns true if any array or sub-array element matches a given value, and false otherwise. There should be two versions, one that works with the entire array, and one that works with sub-array defined by a half-open range. The signatures should be public static boolean anyof (int al], int value) public static boolean anyof (int al], int lo, int hi, int value) The version that works with the entire array should call the sub-array version, just like we did in class with the other stdIntArray functions For example, assume that array a (1, 1, 4, 9, 9, 5, 7, 5). Then anyof (a, 1) returns true anyof (a, 2) returns false anyof (a, 5) returns true anyof (a, 8) returns false anyof (a, 2, 6, 1) returns false anyof (a, 2, 6, 4) returns true anyof (a, 2, 6, 5) returns true anyof (a, 2, 6, 7) returns false anyof (a, 2, 2, 4) returns false Note that false is returned for any empty sub-array Your function may assume that e s lo s hi s a.length

Explanation / Answer

public static boolean anyOf(int a[], int value) { return anyOf(a, 0, a.length, value); } public static boolean anyOf(int a[], int lo, int hi, int value) { if (lo >= hi) { return false; } else if (a[lo] == value) { return true; } else { return anyOf(a, lo+1, hi, value); } }