Advertisements
Advertisements
Question
Write a program for the following.
Create an array DW[ ] of type double and size 8 and fill it with variables. Calculate the average of the maximum and the minimum element.
For example:
| Input | 30.8 | 27.5 | 18.2 | 35.2 | 21.1 | 25.6 | 8.8 | 9.4 |
| Output | Max = 35.2 | Min = 8.8 | Average = 22.0 | |||||
Code Writing
Advertisements
Solution
import java.util.Scanner;
public class CalculateAverage
{
public static void main(String[] args)
{
double DW[] = new double[8];
Scanner sc = new Scanner(System.in);
System.out.println("Enter 8 double elements:");
for (int j = 0; j < DW.length; j++)
{
DW[j] = sc.nextDouble();
}
double max = DW[0];
double min = DW[0];
for (int j = 1; j < DW.length; j++)
{
if (DW[j] > max)
{
max = DW[j];
}
if (DW[j] < min)
{
min = DW[j];
}
}
double average = (max + min)/2.0;
System.out.println("Max =" + max);
System.out.println("Min =" + min + "\tAverage =" + average);
sc.close();
}
}
shaalaa.com
Is there an error in this question or solution?
Chapter 7: Arrays - One Dimension - Exercises [Page 176]
