Question

In: Computer Science

Develop a recursive algorithm to find the smallest and largest element in an array and trace...

Develop a recursive algorithm to find the smallest and largest element in an array and trace the recursive function with appropriate message.
using c++
add comment to the code

Solutions

Expert Solution

#include <iostream>
using namespace std;
#define MAX_SIZE 100
 
// Function declarations
int Largestnumber(int array[], int index, int len);
int Smallestnumber(int array[], int index, int len);
 
 
int main()
{
    //declartion of variables
    int array[MAX_SIZE], size, max, min;
    int i;
 
    // Inputting size and elements of array
    cout<<"Enter size of the array: ";
    cin>>size;
    cout<<"Enter " <<size <<" elements in array: ";
    for(i=0; i<size; i++)
    {
       cin>>array[i];
    }
 
    //calling of functions
    max = Largestnumber(array, 0, size);
    min = Smallestnumber(array, 0, size);
 
    cout<<"Smallest element in array: "<<min<<endl;
    cout<<"Largest element in array: "<<max<<endl;
 
    return 0;
}
 
 
 //Recursive function to find largest element in the given array.
int Largestnumber(int array[], int index, int len)
{
    int max;
    if(index >= len-2)
    {
        if(array[index] > array[index + 1])
            return array[index];
        else
            return array[index + 1];
    }
 
    max = Largestnumber(array, index + 1, len);
 
    if(array[index] > max)
        return array[index];
    else
        return max;
}
 
//Recursive function to find smallest element in the array
int Smallestnumber(int array[], int index, int len)
{
    int min;
 
    if(index >= len-2)
    {
        if(array[index] < array[index + 1])
            return array[index];
        else
            return array[index + 1];
    }
 
    min = Smallestnumber(array, index + 1, len);
 
    if(array[index] < min)
        return array[index];
    else
        return min;
}

OUTPUT:

INPUT


Related Solutions

In Java Find the second largest and second smallest element in a given array. You can...
In Java Find the second largest and second smallest element in a given array. You can hardcode/declare the array in your program.
Given an array of numbers, find the index of the smallest array element (the pivot), for...
Given an array of numbers, find the index of the smallest array element (the pivot), for which the sums of all elements to the left and to the right are equal. The array may not be reordered. Example arr=[1,2,3,4,6] the sum of the first three elements, 1+2+3=6. The value of the last element is 6. Using zero based indexing, arr[3]=4 is the pivot between the two subarrays. The index of the pivot is 3. Function Description Complete the function balancedSum...
Consider the following algorithm to find the kth largest elementof a given array A of...
Consider the following algorithm to find the kth largest element of a given array A of n numbers. We pick every fifth element of A and place them in the array B. We find the median of B recursively, and use this median of B as a pivot to partition the array A. Depending on k and the number of elements that are smaller than the chosen pivot, we recurse into an appropriate subproblem of A.Answer the following questions.Write the...
Question 6 Which of the following for loops will find the largest element in the array...
Question 6 Which of the following for loops will find the largest element in the array numbers, assuming numbers has already been assigned a collection of numeric values? Question 6 options: largest = None for i in range(len(numbers)): if largest is None and numbers[i] > largest: largest = numbers[i] largest = None for i in range(numbers): if largest is None and numbers[i] > largest: largest = numbers[i] largest = None for i in range(len(numbers)): if largest is None or numbers[i]...
Describe an efficient recursive algorithm for solving the element uniqueness problem
Describe an efficient recursive algorithm for solving the element uniqueness problem, which runs in time that is at most O(n2) in the worst case without using sorting.    
Write a recursive algorithm replace (start) to replace the value of each element of A with...
Write a recursive algorithm replace (start) to replace the value of each element of A with that of the next element in A. A is a singly linked list.
Write a modification of the recursive binary search algorithm that always returns the smallest index whose...
Write a modification of the recursive binary search algorithm that always returns the smallest index whose element matches the search element. Your algorithm should still guarantee logarithmic runtime. Give a brief discussion of the best- and worst-case runtimes for this new algorithm as they compare to the original. NOTE: You do not have to re-write the entire algorithm. You just need to indicate any changes you would make and show the pseudocode for any portions that are changed. Example: Given...
implement in LEGV8 find the smallest value in an array.
implement in LEGV8 find the smallest value in an array.
Using Java Write a method that returns the index of the smallest element in an array...
Using Java Write a method that returns the index of the smallest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header:   public static int indexOfSmallestElement (double[] array)
Write a Java method that returns the index of the largest element in an array of...
Write a Java method that returns the index of the largest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header: 
 public static int indexOfLargestElement(double[] array)
 Write a test program that prompts the user to enter ten numbers, invokes this
method to return the index of the largest element, and displays the index.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT