Reputation: 23
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);
}
Upvotes: 1
Views: 85
Reputation: 236
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());
Upvotes: 3
Reputation: 89374
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;
Output:
Total
1087
Average
72.46666666666667
Highest number
212
Lowest number
3
Upvotes: 0
Reputation: 827
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);
}
Upvotes: 0
Reputation: 522007
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
Upvotes: 0