Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6803721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:22:45+00:00 2026-05-26T19:22:45+00:00

I have an assignment to use recursion to obtain the largest element in any

  • 0

I have an assignment to use recursion to obtain the largest element in any given array. I have the following code which will work unless the largest element is the last in the array.

Not sure how to correct this?

import java.util.Scanner;
public class RecursionLargestInArray
{
public static void main (String[] args)
{
    int max = -999;
    Scanner scan = new Scanner (System.in);
    System.out.print("Enter the size of the array: ");
    int arraySize = scan.nextInt();
    int[] myArray = new int[arraySize];
    System.out.print("Enter the " + arraySize + " values of the array: ");
    for (int i = 0; i < myArray.length; i++)
        myArray[i] = scan.nextInt();
    for (int i = 0; i < myArray.length; i++)
        System.out.println(myArray[i]);
    System.out.println("In the array entered, the larget value is "
                        + getLargest(myArray, max) + ".");
}

public static int getLargest(int[] myArray, int max)
{    
    int i = 0, j = 0, tempmax = 0;
    if (myArray.length == 1)
    {
        return max;
    }
    else if (max < myArray[i])
    {
        max = myArray[i];
        int[] tempArray = new int[myArray.length-1];
        for (i = 1; i < myArray.length; i++)
        {
            tempArray[j] = myArray[i];
            j++;
        }
        tempmax = getLargest(tempArray, max);
        return tempmax;
    }
    else if
    {
        int[] tempArray = new int[myArray.length-1];
        for (i = 1; i < myArray.length; i++)
        {
            tempArray[j] = myArray[i];
            j++;
        }
        tempmax = getLargest(tempArray, max);
        return tempmax;
    }
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:22:45+00:00Added an answer on May 26, 2026 at 7:22 pm

    Your first condition is the problem:

    if (myArray.length == 1)
    {
        return max;
    }
    

    replace it with:

    if (myArray.length == 1)
    {
        return myArray[0] > max ? myArray[0] : max;
    }
    

    In case of an array with only one element, you return the previous maximum. If the max is that last element, it will be skipped.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small assignment where I have to use a 2d array to
So I am being taught assembly and we have an assignment which is to
Are there exceptions for types which can't have thier assignment operator overloaded? Specifically, I'm
I have a C assignment. It is a lot longer than the code shown
I've got an assignment I can't figure out, any pointers will be much appreciated,
I have an assignment in which I need to create a function that tells
I have an assignment for my architecture class which to implement a 31 backgammon
I'd like to use the following idiom, that I think is non-standard. I have
I have an assignment to use JavaBeans to create an online Banking application. I
I have a homework assignment. I'm not looking for anyone to do the work

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.