1

so I'm being instructed to do the following, I can get it to run just fine, but it needs to be done with only a single for loop and I just can't figure out how to do it. Any help would be appreciated. This is what I came up with. I need to be able to get the Total, Average, Highest number and lowest number with one for loop.

public static void arrayTotalAndAverage(int[] array) {

        int[] numbers = { 10, 4, 13, 29, 57, 92, 114, 212, 3, 88, 36, 101, 77, 42, 209 };

        int total = 0;
        double average = 0;
        int highest = numbers[0];
        int lowest = numbers[0];

        for (int i = 0; i < numbers.length; i++) {
            total += numbers[i];
            average = total / numbers.length;
        }

        for (int i = 1; i < numbers.length; i++) {
            if (numbers[i] > highest)
                highest = numbers[i];
            if (numbers[i] < lowest)
                lowest = numbers[i];
        }

        System.out.println("Total");
        System.out.println(total);

        System.out.println("Average");
        System.out.println(average);

        System.out.println("Highest number");
        System.out.println(highest);

        System.out.println("Lowest number");
        System.out.println(lowest);

    }
2
  • Take the logic in the body of each loop and combine into a single loop. Commented Jul 15, 2020 at 2:58
  • You would also want to remove the average calculation from the loop and do it once, outside the loop. Commented Jul 15, 2020 at 3:46

4 Answers 4

3

You can do all this operations by StreamAPI.

 int[] numbers = {10, 4, 13, 29, 57, 92, 114, 212, 3, 88, 36, 101, 77, 42, 209};
    IntSummaryStatistics statistics = IntStream.of(numbers).summaryStatistics();
    
    System.out.println("total: " + statistics.getSum());
    System.out.println("average: " + statistics.getAverage());
    System.out.println("minimum: " + statistics.getMin());
    System.out.println("maximum: " + statistics.getMax());
Sign up to request clarification or add additional context in comments.

Comments

0

Actually, perhaps the most efficient way to proceed here would be to just iterate the array once, and then keep track of state for the minimum, maximum, average, and total:

int[] numbers = { 10, 4, 13, 29, 57, 92, 114, 212, 3, 88, 36, 101, 77, 42, 209 };
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
int total = 0;
double average = 0;

for (int i=0; i < numbers.length; ++i) {
    total += numbers[i];
    if (numbers[i] > max) {
        max = numbers[i];
    }
    if (numbers[i] < min) {
        min = numbers[i];
    }
}

average = 1.0d * total / numbers.length;

System.out.println("total: " + total);
System.out.println("average: " + average);
System.out.println("minimum: " + min);
System.out.println("maximum: " + max);

This prints:

total: 1087
average: 72.46666666666667
minimum: 3
maximum: 212

Comments

0

You can do all the operations in single loop like this. And the average can be moved out of the loop.

int[] numbers = { 10, 4, 13, 29, 57, 92, 114, 212, 3, 88, 36, 101, 77, 42, 209 };

int total = 0;
double average = 0;
int highest = Integer.MIN_VALUE;
int lowest = Integer.MAX_VALUE;

for (int i = 0; i < numbers.length; i++) {
    total += numbers[i];
    if (numbers[i] > highest)
        highest = numbers[i];
    if (numbers[i] < lowest)
        lowest = numbers[i];
}
average = total / numbers.length;

System.out.println("Total");
System.out.println(total);

System.out.println("Average");
System.out.println(average);

System.out.println("Highest number");
System.out.println(highest);

System.out.println("Lowest number");
System.out.println(lowest);

}

Comments

0

You can simply merge your second loop with the first one. Note that you should only calculate average at the end of the loop and one of the operands must be cast to double first to avoid the truncation of integer division.

int total = 0;
double average = 0;
int highest = numbers[0];
int lowest = numbers[0];

for (int i = 0; i < numbers.length; i++) {
 total += numbers[i];
 if (numbers[i] > highest)
  highest = numbers[i];
 if (numbers[i] < lowest)
  lowest = numbers[i];
}
average = (double) total / numbers.length;

Demo

Output:

Total
1087
Average
72.46666666666667
Highest number
212
Lowest number
3

Comments

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.