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

We say that a value \"is everywhere\" in an array of integers if, for every pair

ID: 3673650 • Letter: W

Question

We say that a value "is everywhere" in an array of integers if, for every pair of adjacent elements in the array, at least one number in that pair is that value. Write a function named is_everywhere that is passed an array containing n integers. The function prototype is: The function returns true if the given value "is everywhere" in the array, otherwise it returns false. Your function should assume that n will always be at least 2. Here are some clarify what the function does: if arr contains {1, 2, 1, 3} and value is 1, the function returns true, because 1 is in the pairs (1, 2), (2, 1), and (1, 3). If arr contains {1, 2, 3, 1} and value is 1, the function returns false, because 1 is in the pairs (1, 2) and (3, 1). but not in the pair (2, 3). if arr contains {1, 2, 3, 1} and value is 2, the function returns false because 2 is in the pairs (1, 2) and (2, 3), but not in the pair (3, 1). If arr contains {2, 1, 1, 3} and value is 1, the function returns true, because 1 is in the pairs (2, 1), (1, 1), and (1, 3).

Explanation / Answer

#include <bits/stdc++.h>
#include <stdbool.h>
using namespace std;

_Bool is_everywhere(int a[], int n, int value) {
   for (int i = 0; i < n - 1; ++i) {
       if (a[i] != value && a[i + 1] != value) {
           return false;
       }
   }
   return true;
}

int main() {
   _Bool EXPECTED_TRUE = true;
   _Bool EXPECTED_FALSE = false;

   int numberOfTestCasesPassed = 0;

   int test1[] = {1, 2, 1, 3}; // value 1;
   if (is_everywhere(test1, 4, 1) != EXPECTED_TRUE) {
       cout << "Test 1 Failed!" << endl;
   } else ++numberOfTestCasesPassed;

   int test2[] = {1, 2, 3, 1}; // value 1
   if (is_everywhere(test2, 4, 1) != EXPECTED_FALSE) {
       cout << "Test 2 Failed!" << endl;
   } else ++numberOfTestCasesPassed;

   cout << numberOfTestCasesPassed << "/2 passed!" << endl;

   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote