Question

In: Computer Science

Using the data in the following array write a program that finds and outputs the highest...

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};

Solutions

Expert Solution

/***************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...........


Related Solutions

Using JAVA Write a program that uses a 2-D array to store the highest and lowest...
Using JAVA Write a program that uses a 2-D array to store the highest and lowest temperatures for each month of the year. The program should output the average high, average low, and highest and lowest temperatures of the year. Your program must consist of the following methods with their appropriate parameters: a.) getData: This method reads and stores the data in the 2-D array. b.) averageHigh: This method calculates and returns the average high temperature of the year. c.)...
Given the following array, write a program in C++ to sort the array using a selection...
Given the following array, write a program in C++ to sort the array using a selection sort and display the number of scores that are less than 500 and those greater than 500. Scores[0] = 198 Scores[3] = 85 Scores[6] = 73 Scores[9] = 989 Scores[1] = 486 Scores[4] = 216 Scores[7] = 319 Scores[2] = 651 Scores[5] = 912 Scores[8] = 846
IN JAVA Write a program that uses a two-dimensional array to store the highest and lowest...
IN JAVA Write a program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year. Prompt the user for 12 months of highest and lowest.   Write two methods : one to calculate and return the average high and one to calculate and return the average low of the year. Your program should output all the values in the array and then output the average high and the average low. im trying to...
Write a program that uses a two-dimensional array to store the highest and lowest temperatures for...
Write a program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year. Prompt the user for 12 months of highest and lowest.   Write two methods : one to calculate and return the average high and one to calculate and return the average low of the year. These methods MUST be your original code.   Your program should output all the values in the array and then output the average high and the...
Write a program that uses a two dimensional array to store the highest and lowest temperatures...
Write a program that uses a two dimensional array to store the highest and lowest temperatures for each month of the calendar year. The temperatures will be entered at the keyboard. This program must output the average high, average low, and highest and lowest temperatures of the year. The results will be printed on the console. The program must include the following methods: A method named inputTempForMonth whose purpose is to input a high and a low temperature for a...
. Write a program to print * in the following order using 2d array in java...
. Write a program to print * in the following order using 2d array in java                                              *             *             *             *             *                                              *             *             *             *                                              *             *             *                                              *             *                                                          *
In Java, write a program that given an array A[1...n] and a value S finds 0...
In Java, write a program that given an array A[1...n] and a value S finds 0 < i < j < n such that A[i] + A[j] = S
Write a Y86 program in C language that sorts an array of data using Bubble Sort....
Write a Y86 program in C language that sorts an array of data using Bubble Sort. Allow the user to input up to 10 numbers from the keyboard. Sort the array in place (i.e., no need to allocate additional memory for the sorted array). Your program should be a complete one
1. (50 pts) Write a C program that generates a 2D array-of-double and finds the indexes...
1. (50 pts) Write a C program that generates a 2D array-of-double and finds the indexes of the largest value stored in the 2D array. Specific requirements: (1) Your main function defines the 2D array a. You will need to prompt the user to specify the size of the 2D array b. You will need to prompt the user to put in numbers to initialize the array (2) Write a function to display the array that is visualized as rows...
Write a program using multiple functions. Make use of an array to store data Make use...
Write a program using multiple functions. Make use of an array to store data Make use of searching techniques Read data from a file Write data to a file Instructions: In this lab, you will be examining a set of stock collected over a twenty four day period. Be sure to make use of an array to store these stocks. You will be required to read in the data points from a file. Write a function to read in the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT