1

Question: Write a method that takes as input a variable number of integers. The method should return the average of the integers as a double. Write a full program to test the method.

That code below creates an array of 10 integers and finds their average. However, I need it to create a variable number of arguments (int...a) where any number of integers entered can be averaged. Can someone help me.Thanks.

My code:

package average.pkgint;
import java.util.Scanner;
public class AverageInt {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int [] number = new int [10];
        Scanner b = new Scanner(System.in);
        System.out.println("Enter your 10 numbers:");
        for (int i = 0; i < number.length; i++) {
             number[i] =  b.nextInt() ;

        }

        System.out.println("The average is:"+Avnt(number));

    }

    public static int  Avnt(int [] a){//declare arrays of ints
        int result = 1;      
        int sum = 0;
        //iterate through array
        for (int num : a) {
              sum += num; 
        }
            double average = ( sum / a.length);//calculate average of elements in array
            result = (int)average;

             return result;

    }

    }
1
  • Don't use int as type of sum. You can make that overflow with just 2 ints (Integer.MAX_VALUE + 1 == Integer.MIN_VALUE). Use long or double instead. Commented Jun 8, 2014 at 13:30

3 Answers 3

1

This is a way of getting as many variables as you want to a method and going through them all:

public static void testVarArgs(int... numbers){
    for(double u: numbers) {
        System.out.println(u);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

If you don't want to read the number of integers as the first input you can declare a stack in stead of an array

Stack<Integer> numbers=new Stack<>()

and then you can add the numbers with the add method.

3 Comments

Thanks for the heads-up. I was working at something with an array of stacks and forgot to change the left part.
it does but the question said " Write a method that takes as input a VARIABLE number of integers". the program takes only 10. want to rewrite using the (int...a) syntax.
You just add them as a stack and when you call the int...a method you make a conversion from stack to array.
0

Just change your method declaration to a variable length parameter:

public static int  Avnt(int ... a){//declare variable int arguments

When you use a variable length parameter, you supply the values in the method invocation. For example,

    System.out.println(Avnt(1,2,3));
    System.out.println(Avnt(1,2,3,4,5,6,9,9,9,10,10));

Actually, this looks like it meets your requirements. Just add method calls in your main() similar to these.

4 Comments

How would I re-write this program then. Do I still need to loop through the array in main. Also, since it would now be a variable number of arguments do I still need to set the array size.
Leave your method as is. "a" will become an array inside your method. All you need to do is change the declaration as above.
Thanks..but the number of integers I can average is still limited to 10 since that's the size of my array in main. How do I make it unlimited?
See the System.out.println() statements above.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.