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...
Write a program in MIPS to find the largest element of an array, the array size...
Write a program in MIPS to find the largest element of an array, the array size should be less than or equal to 10. Has to be extremely basic, cannot use stuff like move. Very basic. Here is what I already have and I am stuck. .data myarray: .word 0,0,0,0,0,0,0,0,0,0 invalid: .asciiz "Number is invalid, store a number in the array that is from 0-10.\n" large: .asciiz "The largest element is " colon: .asciiz " :\t" enter: .asciiz "Store a...
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 an algorithm that finds both the smallest and largest numbers in a list of n...
Write an algorithm that finds both the smallest and largest numbers in a list of n numbers. Try to find a method that does at most 1.5n comparisons of array items.(but please code in java).
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.
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)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT