Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Help me to understand how does this recursive function (max) work int i = max(Arrays.copyOf(a,n-1));

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Help me to understand how does this recursive function (max) work int i = max(Arrays.copyOf(a,n-1));

    import java.util.*;
     
    public class MaxElement {
     
       public static void main(String[] args) {
    // creating array lenght of 5 integers, and receving input from keyboard
          int[] a = new int[5];
          Scanner tastatura = new Scanner(System.in);
     
          System.out.println("Type elements of array ... ");
          for (int i = 0; i < a.length; i++) {
             System.out.print("Type " + (i+1) + ". element: ");
             a[i] = tastatura.nextInt();
          }
    //printing max element of array
             System.out.print("Max element of array is: ");
             System.out.println(a[max(a)]);
          }
     
       public static int max(int[] a) {
     
           int n = a.length;
           if (n == 1)
             return 0;
           else {
             int i = max(Arrays.copyOf(a,n-1));//this is a problem... i dont know what is returned to int i?
             if (a[i] < a[n-1])
                 return n-1;
             else
                 return i;
           }
       }
    }
    Last edited by impaler; March 6th, 2014 at 04:55 AM.


  2. #2
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    284
    My Mood
    Amused
    Thanks
    6
    Thanked 63 Times in 61 Posts

    Default Re: Help me to understand how does this recursive function (max) work int i = max(Arrays.copyOf(a,n-1));

    Welcome to the Forum! Please read this Announcements - What's Wrong With My Code? to learn how to post code correctly and other useful tips for newcomers.

    Please edit your code with code tags

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,453
    My Mood
    Mellow
    Thanks
    146
    Thanked 607 Times in 594 Posts

    Default Re: Help me to understand how does this recursive function (max) work int i = max(Arrays.copyOf(a,n-1));

    Thanks for fixing your code. A good start to understanding would be to comment the code with your best guess at what it's doing. Once you've done that, post the commented code and let us know where you've gotten stuck.

Similar Threads

  1. min and max value of int
    By Oldemor in forum Java Theory & Questions
    Replies: 13
    Last Post: October 22nd, 2013, 05:43 AM
  2. Min/Max of fields
    By jbraque in forum Object Oriented Programming
    Replies: 1
    Last Post: May 27th, 2013, 03:58 PM
  3. Replies: 2
    Last Post: October 5th, 2012, 01:14 PM
  4. [SOLVED] String max out
    By lorider93p in forum Java Theory & Questions
    Replies: 7
    Last Post: March 2nd, 2012, 09:43 AM
  5. Multi-D array. daily max/min/avg, weekly max/min/avg, sum total (99% done)
    By TheWhopper858 in forum Collections and Generics
    Replies: 1
    Last Post: November 6th, 2011, 07:50 PM