Concept explainers
Explanation of Solution
Complete program:
//Import required packages
import java.util.*;
//Definition of class Sum
public class Sum
{
    //Definition of class main
    public static void main(String[] args)
    {
        //Declare array "a"
        int a[] = new int[7];
        int value, key;
        //Create an object for scanner class
        Scanner sc = new Scanner(System.in);
        //Get the array values
        System.out.println("Enter the array values:");
        //For loop to get the values from the user
        for (int i = 0; i<a.length; i++)
        {
            //Get the values
            value = sc.nextInt();
            //Assign the values to the array
            a[i] = value;
        }
        //Get the key value from the user
System.out.println("Enter key values to be search index:");
        key = sc.nextInt();
        //Call the method "trinarySearch()"
int result = ternarySearch(a, key, 0, a.length - 1);
System.out.println("Value found at position: " + result);
    }
    //Definition of method "trinarySearch()"
public static int ternarySearch(int data[], int target,
        int front, int end)
    {
        //Declare the variable "res"
        int res;
        //Check whether the front is greater than end
        if (front > end)
            //Assign "-1" to "res"
            res = -1;
        else {
/*Divide the first half of the array by "3"*/
            int firstHalf = (2 * front + end) / 3;
/*Divide the second half of the array by "3"*/
            int secondHalf = (front + 2 * end) / 3;
/*Check whether the given number in the first half of the array*/
            if (target == data[firstHalf])
   ;&#x...
Want to see the full answer?
Check out a sample textbook solutionChapter 11 Solutions
Java: An Introduction to Problem Solving and Programming (8th Edition)
- Write a recursive function that find the minimum element in an array of integers.int findMin(int a[], int size);arrow_forwardUsing recursion, write a Java program that creates an array of 10 numbers entered by the user. The recursive function receives the array and the position of the starting point of the array. The function returns the index of the smallest element of the array. The main method outputs the smallest of these numbers using the returned index.arrow_forward1. Write a program to compute the sum of elements in an array recursively. The elements of the array are : 22, 4, 12, 15, 21,6, 11, 3, 10, 34arrow_forward
- Finish the splitOdd10 using recursion to determine if the elements of an array can be split into two groups such that the sum of one group is a non-zero multiple of 10 and the sum of the other group is oddarrow_forwardDesign and implement a recursive program to determine and print the Nth line of Pascal's triangle, as shown below. Each interior value is the sum of the two values above it. Hint: Use an array to store the values on each line.arrow_forwardcan you please convert this python code into java? Python code is as shown below: # recursive function def row_puzzle_rec(row, pos, visited): # if the element at the current position is 0 we have reached our goal if row[pos] == 0: possible = True else: # make a copy of the visited array visited = visited[:] # if the element at the current position has been already visited then it's a loop. # as then its not possible to reach the last element, set possible to False if visited[pos]: possible = False else: # set visitied for the element as True visited[pos] = True possible = False # if its possible to move left then recurse in the left direction if pos - row[pos] > 0 and row_puzzle_rec(row, pos - row[pos], visited): # return immediately if the goal is reached return True # if its possible to move right then recurse in…arrow_forward
- In C language, implement two recursive versions of the linear search that gets an array of songs and a title and searches for a song with the given title in the array. 1. In this version the function returns the first index of the array containing the number. If the number is not in the array, the function returns -1. int linear_search_rec_first(int* ar, int length, int number); 2. In this version the function returns the last index of the array containing the number. If the number is not in the array, the function returns -1. int linear_search_rec_last(int* ar, int length, int number); Example of test for case 1: void test_q3() { int ar1[]={1,2,3,4,5,4,3,2,1}; if (linear_search_rec_first(ar1,9,2)==1) printf("Q3-1 ok\n"); else printf("Q3-1 ERROR\n"); }  Example of test for case 2:  void test_q3() { int ar1[]={1,2,3,4,5,4,3,2,1}; if (linear_search_rec_last(ar1,9,2) == 7) printf("Q3-3 ok\n"); else printf("Q3-3 ERROR\n"); }arrow_forwardProgram 2 Construct a BinarySearch class that contains recursive binary search static method. Test your recursive binary search on a sorted array holding 16 integers. Display the contents of the array before and after the sort method is called.arrow_forwardWrite a RECURSIVE function, without using any loops, that prints the contents of a matrix with 3 columns. The function should take the matrix and the number of rows as arguments. void print_matrix(int arr[][3], int num_rows);arrow_forward
- Without using Java environment Write a recursive function with two parameters: an array of ints and the number of elements in the array. The function will return the sum of all positive values in the array. Do not add any negative array elements to the sum. You can add another parameter to the function if you wish.arrow_forwardAn array of integers is given. Write a recusrive function recLastname ( Use your last name ) that takes the array as argument to find the sum of the elements in the array recursively. Just the function is enough. Java code beginner pleasearrow_forwardThere is an error in the following recursive method, which implements a binary search. Find the error and indicate how to fix the code so that the method will work correctly.arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning