1

I know i can't pass arrays the way I'm doing it. Would I need to pass by reference if so how? The question is at the bottom for reference.

import java.util.Scanner;
public class MethodsArrays {

    public static int[] fillArray() {
        Scanner scan = new Scanner(System.in);
        int size = scan.nextInt();
        int array[] = new int[size];
        int pos=0;

        for(int i=0; i<array.length; i++) {
            pos=i+1;
            System.out.println("Enter element " + pos);
            array[i]=scan.nextInt();
        }
        return array;
    }

    public static int sumArray(int [] array) {
        int sum=0;

        for(int i=0; i<array.length-1; i++) {
            sum=array[i]+array[i+1];
        }
        return sum;
    }

    public static int avgArray(int [] array) {
        int avg=0;
        int sum = sumArray(array);
        avg = sumArray(array)/array.length-1;
        return avg;
    }

    public static void printArray(int [] array) {
        for(int i=0; i<array.length; i++) {
            System.out.print(array[i] + " ");
        }
    }

    public static void main(String[] args) {
        fillArray();
        System.out.println("Sum=" + sumArray(array));
        System.out.println("Average=" + avgArray(array));
        printArray(array);
    }
}

Write a Java program, called MethodsArrays that has 4 static methods called fillArray(), sumArray(), avgArray(), and printArray(). The fillArray() method should be called from the main method. The fillArray() method should use a Scanner to take in a number representing the length of the array and then read in numbers to fill the array. The sumArray() method should take an int array as its input parameter and returns an integer value that is the sum of all the elements in the array. The avgArray() method should take an int array as its input parameter and returns an integer value that is the average of all the elements in the array. The printArray() method should take an int array as its input parameter and has no return value. It should then print out the elements of the array on the same line separated by a space (“ “). All methods should work for integer arrays.

5
  • 1
    You would have less problems if you would actually care about what fillArray() returns ... Commented Feb 16, 2017 at 15:44
  • Why can't you pass arrays the way you are doing? You just need to store the result of the call to fillArray() in a variable named array so the variable name actually exists and you can use it in the following calls. Commented Feb 16, 2017 at 15:44
  • 1
    i think that you need to assign the fillArray() result to a variable, like int[] array= fillArray(); Commented Feb 16, 2017 at 15:44
  • sum=array[i]+array[i+1]; What is this supposed to do? Commented Feb 16, 2017 at 15:49
  • Yes thanks forgot to assign the fillArray() to an array. Commented Feb 16, 2017 at 15:53

1 Answer 1

2

Your code looks OK. You just need to assign the result of fillArray() to a variable, in order to use this result for further methods.

It would look like:

 int[] array = fillArray();
 System.out.println("Sum=" + sumArray(array));
 System.out.println("Average=" + avgArray(array));
 printArray(array);
Sign up to request clarification or add additional context in comments.

1 Comment

In avgArray method OP is dividing by array.length-1. Not sure if its intended.

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.