In: Computer Science
Using the data in the following array write a program that finds and outputs the highest value, the lowest value, the count of elements in the array, the average value in the array, and the mode - the value that appears most often.
dataArray = {173.4, 873.7, 783.9. 000.0, -383.5, 229.5, -345.5, 645.5, 873.7, 591.2};
/***************ArrayOperation.java************************/
import java.text.DecimalFormat;
public class ArrayOperation {
/**
*
* @param dataArray
* @return the highest value in the array
*/
public static double findhighest(double[] dataArray)
{
//assign min value to the
highest variable
double highest =
Double.MIN_VALUE;
for (int i = 0; i < dataArray.length; i++) {
//compare
with each element and store the highest
if (dataArray[i]
> highest) {
highest = dataArray[i];
}
}
return highest;
}
/**
*
* @param dataArray
* @return the minimum element in the array
*/
public static double findMinimum(double[] dataArray)
{
//assign the max value to
lowest
double lowest =
Double.MAX_VALUE;
for (int i = 0; i < dataArray.length; i++) {
//compare and
store the minimum element in the array
if (dataArray[i]
< lowest) {
lowest = dataArray[i];
}
}
return lowest;
}
//count the number of the element
public static int countElement(double[] dataArray)
{
return dataArray.length;
}
/**
*
* @param dataArray
* @return the average of the array
*/
public static double findAverage(double[] dataArray)
{
double total = 0;
int count = 0;
for (int i = 0; i <
dataArray.length; i++) {
total = total
+ dataArray[i];
count++;
}
return total / count;
}
/**
*
* @param array
* @return the mode of the array
*/
public static double findMode(double[] array) {
double maxValue = 0, maxCount = 0;
for (int i = 0; i <
array.length; ++i) {
int count =
0;
for (int j = 0;
j < array.length; ++j) {
if (array[j] == array[i])
++count;
}
if (count >
maxCount) {
maxCount = count;
maxValue = array[i];
}
}
return maxValue;
}
public static void main(String[] args) {
double[] dataArray = { 173.4,
873.7, 783.9, 000.0, -383.5, 229.5, -345.5, 645.5, 873.7, 591.2
};
//To limit the decimal places
DecimalFormat format = new
DecimalFormat(".##");
System.out.println("The highest
elememt is: " + findhighest(dataArray));
System.out.println("The Lowest
elememt is: " + findMinimum(dataArray));
System.out.println("The Average of
the array is: " + format.format(findAverage(dataArray)));
System.out.println("The number of
element in the array is:" + countElement(dataArray));
System.out.println("The most apears
number is: " + findMode(dataArray));
}
}
/***********************output**************************/
The highest elememt is: 873.7
The Lowest elememt is: -383.5
The Average of the array is: 344.19
The number of element in the array is:10
The most apears number is: 873.7
Thanks a lot, Please let me know if you have any problem...........