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 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 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...
In C Write a main function with a function call to a function called GetLetter which...
In C Write a main function with a function call to a function called GetLetter which has two double arguments/parameters. The function returns a character. Declare and initialize the necessary data variables and assign values needed to make it executable and to prevent a loss of information
Program must be in C++! Write a program which: Write a program which uses the following...
Program must be in C++! Write a program which: Write a program which uses the following arrays: empID: An array of 7 integers to hold employee identification numbers. The array should be initialized with the following values: 1, 2, 3, 4, 5, 6, 7. Hours: an array of seven integers to hold the number of hours worked by each employee. payRate: an array of seven doubles to hold each employee’s hourly pay rate. Wages: an array of seven doubles to...
C++ Write a program that has two functions. The 1st function is the main function. The...
C++ Write a program that has two functions. The 1st function is the main function. The main function should prompt the user for three inputs: number 1, number 2, and an operator. The main function should call a 2nd function called calculate. The 2nd function should offer the choices of calculating addition, subtraction, multiplication, and division. Use a switch statement to evaluate the operator, then choose the appropriate calculation and return the result to the main function.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT