在Java中查詢未排序陣列的平均值和中位數
在Java中,陣列是一個物件。它是一種非基本資料型別,用於儲存相同資料型別的多個值。
根據題目要求,我們必須在Java中找到未排序陣列的平均值和中位數。
陣列的平均值可以透過計算陣列中所有元素的平均值來獲得。
Mean= (sum of all elements present in array) / (total number of elements present)
陣列的中位數表示在奇數個元素的已排序陣列中位於中間的元素;如果已排序陣列包含偶數個元素,則中位數可以透過計算中間兩個數字的平均值來獲得。
讓我們探討這篇文章,看看如何使用Java程式語言來實現。
舉幾個例子
示例1
給定陣列 = [12, 23, 34, 45, 15]。
該陣列的平均值 = (12 + 23 + 34 + 45 + 15) / (5) = 129 / 5 = 25.8
給定陣列的已排序陣列 = [12, 15, 23, 34, 45]
由於這是一個奇數元素陣列,因此中位數是中間元素。
中位數 = 23
示例2
給定陣列 = [38, 94, 86, 63, 36]。
該陣列的平均值 = (38 + 94 + 86 + 63 + 36) / (5) = 317 / 5 = 63.4
給定陣列的已排序陣列 = [36, 38, 63, 86, 94]
由於這是一個奇數元素陣列,因此中位數是中間元素。
中位數 = 63
示例3
給定陣列 = [54, 67, 23, 95, 24, 60]。
該陣列的平均值 = (54 + 67 + 23 + 95 + 24 + 60) / (6) = 323 / 6 = 53.83
由於這是一個偶數元素陣列,因此中位數是中間兩個元素的平均值。
給定陣列的已排序陣列 = [23, 24, 54, 60, 67, 95]
中位數 = (54 + 60) / 2 = 57
演算法
步驟1 - 宣告並初始化一個整數型別的陣列。
步驟2 - 將陣列按升序排序。
步驟3 - 在第一個使用者自定義方法中,我們找到平均值。在第二個使用者自定義方法中,我們找到中位數。
步驟4 - 呼叫這兩個使用者自定義方法,並將陣列和長度值作為引數傳遞。
步驟5 - 找到平均值和中位數後,將這兩個值列印為輸出。
語法
要獲取陣列的長度(陣列中的元素個數),陣列有一個內建屬性length
以下是其語法:
array.length
其中,“array”指的是陣列引用。
您可以使用Arrays.sort()方法將陣列按升序排序。
Arrays.sort(array_name);
多種方法
我們提供了不同方法的解決方案
使用靜態輸入方法
使用使用者輸入方法
讓我們一一檢視程式及其輸出。
方法1:使用靜態輸入方法
在這種方法中,我們透過靜態輸入方法宣告一個數組,並將此陣列及其長度作為引數傳遞到我們使用者自定義的方法中,然後在方法內部使用演算法可以找到平均值和中位數。
示例
import java.util.*;
public class Main {
public static void main(String args[]) {
//declare an integer type array and store some random integer values
int inputArray[] = { 23, 24, 65, 87, 85, 12, 76,21};
//declare an integer variable to store the length of the given array
int len = inputArray.length;
//call the user defined method and print the output
System.out.println("Mean of given array "+ Arrays.toString(inputArray)+ " is = " + mean(inputArray, len));
System.out.println("Median of given array "+ Arrays.toString(inputArray) + " is = " + median(inputArray, len));
}
// user defined function to find mean value
public static double mean(int arr[], int len)
{
//declare a Integer variable to store the sum value of given array's elements
int sum = 0;
//initiate the loop to calculate the sum value of all the elements of given array
for (int i = 0; i < len; i++)
sum += arr[i];
//return the mean value
return (double)(arr[(len - 1) / 2] + arr[len / 2]) / 2.0;
}
//user defined function to find median value
public static double median(int arr[], int len) {
// sort the given array in ascending order
Arrays.sort(arr);
// check whether the given array consists of even or odd number of elements
if (len % 2 != 0)
{
return (double)arr[len / 2];
}
return (double)(arr[(len - 1) / 2] + arr[len / 2]) / 2.0;
}
}
輸出
Mean of given array [23, 24, 65, 87, 85, 12, 76, 21] is = 49.125 Median of given array [23, 24, 65, 87, 85, 12, 76, 21] is = 44.5
方法2:使用使用者輸入方法
在這種方法中,我們透過使用者輸入方法宣告一個數組,並將此陣列及其長度作為引數傳遞到我們使用者自定義的方法中,然後在方法內部使用演算法可以找到平均值和中位數。
示例
import java.util.*;
public class Main {
public static void main(String args[]) {
//create the Objectof Scanner class
Scanner sc=new Scanner(System.in);
//ask the user to enter the length of the array
System.out.print("Enter the number of elements: ");
//declare a variable to store the length
int len=sc.nextInt();
//declare an empty array of given length
int[] inputArray = new int[len];
System.out.println("Enter the elements: ");
//initiate the loop to store the elements into the array
for(int i=0; i < len; i++) {
//store the elements intop the array
inputArray[i]=sc.nextInt();
}
//call the user defined method and print the output
System.out.println("Mean of given array "+ Arrays.toString(inputArray) + " is = " + mean(inputArray, len));
System.out.println("Median of given array "+ Arrays.toString(inputArray) + " is = " + median(inputArray, len));
}
// user defined function to find mean value
public static double mean(int arr[], int len) {
//declare a Integer variable to store the sum value of given array's elements
int sum = 0;
//initiate the loop to calculate the sum value of all the elements of given array
for (int i = 0; i < len; i++)
sum += arr[i];
//return the mean value
return (double)sum / (double)len;
}
//user defined function to find median value
public static double median(int arr[], int len) {
// sort the given array in ascending order
Arrays.sort(arr);
// check whether the given array consists of even or odd number of elements
if (len % 2 != 0){
return (double)arr[len / 2];
}
System.out.println(arr[(len - 1)]);
System.out.println(arr[(len - 1)]);
return (double)(arr[(len - 1) / 2] + arr[len / 2]) / 2.0;
}
}
輸出
Enter the number of elements: 8 Enter the elements: 2 5 3 7 1 6 8 4 Mean of given array [2, 5, 3, 7, 1, 6, 8, 4] is = 4.5 Median of given array [2, 5, 3, 7, 1, 6, 8, 4] is = 4.5
在這篇文章中,我們探討了如何使用Java程式語言在未排序陣列中查詢陣列的平均值和中位數。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP