Question

2.4.3 Finding the Largest Value in a Sorted or Unsorted Array Suppose that you have an array anarray of integers in any ordermaxArray(anArray) maxArray(left half of anArray) AND maxArray(right half of anArray) Figure 2-12 Recursive solution to the la

  1. Implement the algorithm maxArray, discussed in Section 2.4.3, as a C++ function.

  2. Make sure the following requirements are met.

  3. Program must compile and run.
  4. maxArray function must be a recursive template function.
  5. Add a main function to test the maxArray function so that you have a complete program.
  6. Test the maxArray function on two arrays of different types.
  7. Name the program maxarray.cpp. Make sure the following requirements are met.
0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include<climits>
using namespace std;
void maxArray(int arr[],int low,int high,int& max)
{
    // if array contains only one element
        if (low==high)
        {
                if (max < arr[low])
                        max = arr[low];
                return;
        }
        // if array contains only two elements
        if (high-low==1)
        {
                if(arr[low]<arr[high])
                {
                        if(max<arr[high])
                                max=arr[high];
                }
                else
                {
            if(max<arr[low])
                                max=arr[low];
                }
                return;
        }
        int mid=(low+high)/2;

        // recur for left sub-array
        maxArray(arr,low,mid,max);
        // recur for right sub-array
        maxArray(arr,mid+1,high,max);
}

int main()
{
        int arr1[]={1,6,8,3};
        int arr2[]={21,98,23,4,1,43,6};
        int n=sizeof(arr1)/sizeof(arr1[0]);

        int max=INT_MIN;
        maxArray(arr1,0,n-1,max);
        cout << "The maximum element in the array 1 is " << max;
        n=sizeof(arr2)/sizeof(arr2[0]);
        maxArray(arr2,0,n-1,max);
        cout << "\nThe maximum element in the array 2 is " << max;

        return 0;
}

We divided the array recursively into two equal parts and update the value of max.

I tried to explain the code with the help of comment and recognizable variables

Feel free to ask any query related to code or DIVIDE AND CONQUER Algorithm which we are using here.

Add a comment
Know the answer?
Add Answer to:
Implement the algorithm maxArray, discussed in Section 2.4.3, as a C++ function. Make sure the following...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that meets the following criteria: Define a function that implements the selection sort...

    Write a program that meets the following criteria: Define a function that implements the selection sort algorithm to sort an integer array. Define a function that implements the binary search algorithm to search an array for a given integer. The function must return the location of the integer if it is found or a -1 if it is not found. The main function in your program must declare an integer array initialized with 10 unsorted values. The main function must...

  • You are going to implement Treesort algorithm in C++ to sort string data. Here are the...

    You are going to implement Treesort algorithm in C++ to sort string data. Here are the steps to complete the homework 1) Use the following class definition for binary search tree nodes. Its constructor is incomplete you should first complete the constructor. class TreeNode t public: string data; / this is the string stored in the node TreeNode left: TreeNode right; TreeNode (string element, TreeNode 1t, TreeNode rt //your code here 2) Write a function that will insert a string...

  • Just Q3 and Q4 Q1] Write a C function to implement the binary search algorithm over...

    Just Q3 and Q4 Q1] Write a C function to implement the binary search algorithm over an array of integer numbers and size n. The function should return the index of the search key if the search key exists and return - 1 if the search key doesn't exist. [10 Points] Q2] Write a C function to implement the selection sort algorithm, to sort an array of float values and size n. The function should sort the array in ascending...

  • PLEASE IMPLEMENT YOUR SOLUTION RECURSIVELY IN C++. IT WOULD BE GREAT IF YOU COULD ALSO PROVIDE...

    PLEASE IMPLEMENT YOUR SOLUTION RECURSIVELY IN C++. IT WOULD BE GREAT IF YOU COULD ALSO PROVIDE TEST CODE FOR IT. 5) Design a recursive function to find the immediate successor of a target integer in an array o:f sorted integers. Here the immediate successor of a target is defined as the smallest number that is no smaller than the target in this sorted array int binarySuccessor(const int anArrayl, const int first, const int last, int target); In the above function...

  • Data Structures, algorithm, c++. Please explain if possible. Thank you! Complete the following function that returns...

    Data Structures, algorithm, c++. Please explain if possible. Thank you! Complete the following function that returns the largest element of the binary tree rooted at root. template <typename T> T max (Node<T>root) if (root nullptr) return TO; T max1 = root->x; T max2 root->x; if (root->left != nullptr) maxlmax (root->left); if (root->right != nullptr) max2 max ( root->right); - if (root->x >= max1 && root->x >= max2) return __.-; if (max1 >= root->x && max1 >= max2) return-_-> return___--> Let...

  • You are making a .h file. Implement a recursive binary search function. bSearch passes in an...

    You are making a .h file. Implement a recursive binary search function. bSearch passes in an array of integers, the size of the array, and the value the function is searching for. The function should return the index where found or -1 if not found. You will want to implement a recursive helper function that passes in the array, the value to be located, and the beginning and ending of the range of values to search within. Use the provided...

  • Make a program using Java that asks the user to input an integer "size". That integer...

    Make a program using Java that asks the user to input an integer "size". That integer makes and prints out an evenly spaced, size by size 2D array (ex: 7 should make an index of 0-6 for col and rows). The array must be filled with random positive integers less than 100. Then, using recursion, find a "peak" and print out its number and location. (A peak is basically a number that is bigger than all of its "neighbors" (above,...

  • Consider the following: Algorithm 1 Smallest (A,q,r) Precondition: A[ q, ... , r] is an array...

    Consider the following: Algorithm 1 Smallest (A,q,r) Precondition: A[ q, ... , r] is an array of integers q ≤ r and q,r ∈ N. Postcondition: Returns the smallest element of A[q, ... , r]. 1: function Smallest (A , q , r) 2: if q = r then 3: return A[q] 4: else 5: mid <--- [q+r/2] 6: return min (Smallest(A, q, mid), Smallest (A, mid + 1, r)) 7: end if 8: end function (a) Write a recurrence...

  • Write a menu based program implementing the following functions: (0) Write a function called displayMenu that...

    Write a menu based program implementing the following functions: (0) Write a function called displayMenu that does not take any parameters, but returns an integer representing your user's menu choice. Your program's main function should only comprise of the following: a do/while loop with the displayMenu function call inside the loop body switch/case, or if/else if/ ... for handling the calls of the functions based on the menu choice selected in displayMenu. the do/while loop should always continue as long...

  • In Java, Implement a class MyArray as defined below, to store an array of integers (int)....

    In Java, Implement a class MyArray as defined below, to store an array of integers (int). Many of its methods will be implemented using the principle of recursion. Users can create an object by default, in which case, the array should contain enough space to store 10 integer values. Obviously, the user can specify the size of the array s/he requires. Users may choose the third way of creating an object of type MyArray by making a copy of another...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT