0

Read an array of 5 integer values in the main method. Create a separate function that will determine which number is the biggest. Return the biggest value to the main and print it within the main. I started the code but my problem is that I do not know how to pass the values that I read in the main method (the array values) into the function! I started off like this:

import java.util.*;
public class arraybiggest {
    public static int main (String[]args) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter 5 values: ");
        int [] x = new int[5];
        for (int i=0; i<5; i++) {
            x[i] = in.nextInt();
            return x;
        }
    }
    public static int  passarray(int [ ] value) {

    }
}

Pls help me! Thanks!

2
  • 2
    Why is your main method returning int? Shouldn't it be void? Commented Sep 5, 2013 at 13:17
  • I guess this isnt the right place.. Commented Sep 5, 2013 at 13:26

4 Answers 4

1

You can just call your function and pass the array to it, like this:

passarray(x);

But first you need to remove the return x; from the loop.

Sign up to request clarification or add additional context in comments.

1 Comment

Im still a beginner! So maybe what im doing is wrong. Can you correct the code and post it! Great help... Im trying so much but it is an error!
0
for (int i=0; i<5; i++) {
        x[i] = in.nextInt();
        }
 passarray(x);

Comments

0
import java.util.*;
public class arraybiggest {

   public static int main (String[]args) {
          Scanner in = new Scanner(System.in);
          System.out.println("Enter 5 values: ");
          int [] x = new int[5];
          for (int i=0; i<5; i++) {
             x[i] = in.nextInt();           
         }

         int result = passarray(x);
         System.out.println("the biggest number is:" +result );
   }


  public static int  passarray(int [ ] value) {
        int result=value[0]
        for(int i=1;i<value.length;i++){
            if (value[i] > result) {
                result= value[i];
            }
        }
       return result;
   }
}

Comments

0
    import java.util.*;
    public class arraybiggest {
        public static int main (String[]args) {
            Scanner in = new Scanner(System.in);
            System.out.println("Enter 5 values: ");
            int [] x = new int[5];
            for (int i =0; i<5; i++) {
                x[i] = in.nextInt();
            }
            System.out.println(findMax(x));
        }
        public static int  findMax(int [ ] value) {
            int max = value[0];
            for (int i = 1; i < value.length; i++) {
                if (value[i] > max) {
                    max = value[i];
                }
            }
            return max;
        }

    }

2 Comments

i dont understand in: for (int i = 1; i < value.length; i++) statement what value.length is...
It's the size of the integer array 'value'. 'value' is basically integer array x. Look at the line 'System.out.println(findMax(x));' in main - findMax(x) calls findMax with 'x' as value to the parameter called 'value'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.