Welcome to Stack Overflow, Tony! Here at Stack Overflow, we really encourage users to provide some proof of effort or research, keep this in mind in future posts :)
Let's think about this problem logically.
We want to start by getting the average of the array from array[0]
to array[n-2]
(You use n-2, because index n-1 is actually holding the value '6').
For the second part. start at array[1]
and go to array[n-1]
Once we know this, we can take the average and return it.
There is no need for nested loops here, remember this concept while programming and many tears will be saved: Keep it simple
Here is a similar question that was posted: How to minpulate arrays and find the average
Here is a solution that I came up with. When you are in the design stage of your program, you want to think about how you can make your code reusable. There may be a time when you'll have a complex program and many parts will need to perform the same operation with different data. This is known as code re-usability and mastering it will make your life easier.
public static void main(String[] args) {
int [] arr = new int [] {1, 2, 3, 4, 5, 6}; //Stores the numbers we need to average
//We get the Lower-Average by starting at index 0, going to index n-2
System.out.println ("Lower-Average: " + average(0, arr.length - 2, arr));
//We get the Upper-Average by starting at index 1, going to index n-1
System.out.println ("Upper-Average: " + average(1, arr.length - 1, arr));
}
/*
* This method accepts a start index, end index, and an array to operate on
* The average is calculated iteratively and returned based on number of elements provided
*/
public static double average (int startIndex, int endIndex, int [] array) {
double avg = 0; //Stores the average
int counter; //Used to hold number of elements iterated through
for (counter = startIndex; counter <= endIndex; counter++) {
avg += array[counter]; //Summation for the average
}
return avg = avg / counter; //Calculate the average and return it to caller
}
Output:
Lower-Average: 3.0
Upper-Average: 3.3333333333333335