Question

In: Computer Science

Write a C++ function which will search for TWO elements. The two elements must be right...

Write a C++ function which will search for TWO elements. The two elements must be right next to each other, contiguously. The prototype of the function should be: bool search_adjacent_elements(int const *x, size_t n, int target1, target2); It will return true if target1 and target2 exist in the array x somewhere, with target1 immediately to the left of target2. For example: int x[] = { 3, 5, 10, 14, 19, 25, 45, 60 }; cout << search_adjacent_elements(x, sizeof x / sizeof x[0], 10, 14) << endl; // true!! cout << search_adjacent_elements(x, sizeof x / sizeof x[0], 25. 19) << endl; // false (wrong order) cout << search_adjacent_elements(x, sizeof x / sizeof x[0], 25, 60) << endl; // false (not right next to one another) You may assume the array is ordered. Your function must only search through your array once. If you search through your array multiple times (e.g., search once for target1, then search again for target2), you will receive a 2 mark deduction. You may use any code we have used in class as a reference. If referencing code from online, you must include a comment with a link to where you got it from. For a maximum 3/4 marks, do a linear search. For a maximum 4/4 marks, do a binary search.

Solutions

Expert Solution

CODE -

#include<iostream>

using namespace std;

// Function declaration

bool search_adjacent_elements(int const *x, size_t n, int target1, int target2);

// Main function

int main()

{

    int x[] = { 3, 5, 10, 14, 19, 25, 45, 60 };

    // This is required to print bool values as true/false because by default C++ prints 1 for true and 0 for false

    cout << boolalpha;

    cout << search_adjacent_elements(x, sizeof x / sizeof x[0], 10, 14) << endl;

    cout << search_adjacent_elements(x, sizeof x / sizeof x[0], 25, 19) << endl;

    cout << search_adjacent_elements(x, sizeof x / sizeof x[0], 25, 60) << endl;

   return 0;

}

// Function definition

bool search_adjacent_elements(int const *x, size_t n, int target1, int target2)

{

    // Variable declaration

    int l = 0;

    int r = n - 1;

    int m;

    // Loop to implement binary search

    while(l <= r)

    {

        m = l + (r - l)/2;

        // Search for target1 and check if next element is equal to target2 or not

        // Check if target is present at the mid

        if(x[m] == target1)

        {

            if (x[m+1] == target2)

                return true;

            else

                return false;

        }

        // If target1 is greater than mid, search only in right half

        else if (x[m] < target1)

            l = m + 1;

        // If target1 is smaller than mid, search only in left half

        else

            r = m - 1;

    }

    // Return false if both elements were not found adjacent to each other

    return false;

}

SCREENSHOTS -

CODE -

OUTPUT -

If you have any doubt regarding the solution, then do comment.
Do upvote.


Related Solutions

Write a c program Write a function to swap two elements of an integer array. Call...
Write a c program Write a function to swap two elements of an integer array. Call the function to swap the first element, i[0] with last element i[n], second element i[1] with the last but one element i[n-1] and so on. Should handle arrays with even and odd number of elements. Call the swap function with the following arrays and print results in each case before and after swapping. i. int arr1[] = {0, 1, 2, 3, 30, 20, 10,...
Write a C program in Unix which uses a function called search to find the location...
Write a C program in Unix which uses a function called search to find the location of a value in THREE arrays of floats. The function should take three parameters :          the value to be found          the array to be searched          the size of the array N.B.!!!! The main program should read in three arrays of varying size          example : array a has twelve elements                    array b has six elements                    array c has nine...
For C++ Function 1: Write a recursive function to perform a sequential search on a set...
For C++ Function 1: Write a recursive function to perform a sequential search on a set of integers The function will require an array parameter and the number to look for. These are the minimal parameter requirements The function should take an array of any size Function 2: Write a recursive function that will convert an integer (base 10) to binary The function should only have an integer parameter Have the function write the binary number to the console You...
Write a C function to add the elements of two same-sized integer arrays and return ptr...
Write a C function to add the elements of two same-sized integer arrays and return ptr to a third array. int *addTwoArrays(int *a1, int *b1, int size); The function should follow the following rules: If the sum for any element is negative, make it zero. If a1 and b1 point to the same array, it returns a NULL If any input array is NULL, it returns a NULL. Please call this function with the following arrays and print the sums...
C++ Write a function called linearSearch that takes an array as a parameter and search for...
C++ Write a function called linearSearch that takes an array as a parameter and search for a specific value inside this parameter. The function returns the frequency of a specific value in the array. In the main function: 1. Define an array called salaries of length 5. 2. Initialize the array by asking the user to input the values of its elements. 3. Define a variable called key and ask the user to enter a value for this variable. 4....
Write a program of Binary Search in C++ by using function and arrays with the explanation.
Write a program of Binary Search in C++ by using function and arrays with the explanation.
Write a function in C that takes one argument, an array of 50 elements. Your function...
Write a function in C that takes one argument, an array of 50 elements. Your function should print out the index and value of the smallest element in the array.
In C++ Write a function which takes two parameters: an array of ints and an int...
In C++ Write a function which takes two parameters: an array of ints and an int size of the array and prints every element greater than 5 to the screen. As an example, if the array has the following 10 elements: 2 5 8 9 7 1 0 2 6 3, your function should print out 8 9 7 6. You may assume that the parameters passed to the function are valid. Your function must have the following signature: void...
Write a function called swapElements to swap two elements in a character array. This function takes...
Write a function called swapElements to swap two elements in a character array. This function takes two parameters, a character array and input file. The function should read two numbers from a file and it swap the elements stored in the indices read. please code in c++
Write a C function to swap the first and last elements of an integer array. Call...
Write a C function to swap the first and last elements of an integer array. Call the function from main() with an int array of size 4. Print the results before and after swap (print all elements of the array in one line). The signature of the arrItemSwap() function is: void arrItemSwap(int *, int, int); /* array ptr, indices i, j to be swapped */ Submit the .c file. No marks will be given if your pgm does not compile...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT