Exercise: Write a Java program to answer about the statistical information such as arithmetic mean, median, mode, and standard deviation of an integer data set. The data points are input by the user from keyboard. This program will display the output similar to the one shown below:
Java arrays: statistics information
- How To Find Mean Mode Median
- Java Program To Calculate Mean Median Model
- Java Program To Calculate Mean Median Mode And Range
- Java Program To Calculate Mean Median Models
- Write a java program to calculate median of the array. Write a java program to calculate median of the array. Write a program to accept an int array as input, and calculate the median of the same. Median Calculation Procedure: Sort the sequence of numbers. The total number count is odd, Median.
- Jan 05, 2018 Calculating Mean, Standard Deviation, Frequencies and More in R R Tutorial 2.8 MarinStatsLectures - Duration: 6:50. MarinStatsLectures-R Programming & Statistics 239,353 views 6:50.
If you are not sure about statistical information such as arithmetic mean, median, mode, and standard deviation, you will need to read this page:
Solution:
import java.util.Scanner;
public class StatisticsInfo
{
public static void main(String[] args)
{
showStatistics();
}
static void showStatistics(){
//
int n;
float mean,median,std;
Scanner sc=new Scanner(System.in);
System.out.print('Enter number of data points:');
n=sc.nextInt();
if (n < 3)
{
System.out.println('The number of data points should be greater than 2.');
}
else
{
//declare an array of n size to store integral data points
int[] dataset = new int[n];
//allow user inputs
int i = 0;
for (i = 0; i < n; i++)
{
System.out.print('['+i+']:');
dataset[i] = sc.nextInt();
}
//sort the data set
bubblesort(dataset, n);
//calculate the mean
int sum = 0;
int j = 0;
while (j < n)
{
sum = sum + dataset[j];
j++;
}
mean = (float)sum / n;
//calculate median
//If n is odd, median=dataset[n/2]
//If n is even, median=(dataset[n/2]+dataset[1+n/2])/2
//The index of array starts from 0, so you need to subtract 1 from the indices used in calculating the median
if (n % 2 != 0) median = dataset[n / 2];
else median = (dataset[(n / 2) - 1] + dataset[n / 2]) / (float)2;
//calculate the mode
int[][] mode = new int[n][2];
//initialize 2D array storing numbers of occurences, and values
for (i = 0; i < 2; i++)
for (j = 0; j < n; j++) mode[j][i] = 0;
mode[0][0] = 1;
for (i = 0; i < n; i++)
for (j = 0; j < n - 1; j++)
if (dataset[i] dataset[j + 1]) { ++mode[i][0]; mode[i][1] = dataset[i]; }
int max;
int k = 0;
max = mode[0][0];
for (j = 0; j < n; j++)
if (max < mode[j][0]) { max = mode[j][0]; k = j; }
//calculate standard deviation,std
float temp = 0.0f;
for (j = 0; j < n; j++)
{
temp = temp + (float)Math.pow(dataset[j] - mean, 2);
}
std = (float)Math.sqrt(temp / (n - 1));
//Show results
System.out.println('Statistical Information:');
System.out.println(');
System.out.println('Arithmetic mean:'+mean);
System.out.println('Median:'+median);
if (mode[k][1] != 0)
System.out.println('Mode:'+ mode[k][1]);
else System.out.println('Mode: no mode');
System.out.println('Standard deviation:'+std);
}
//
}
public class StatisticsInfo
{
public static void main(String[] args)
{
showStatistics();
}
static void showStatistics(){
//
int n;
float mean,median,std;
Scanner sc=new Scanner(System.in);
System.out.print('Enter number of data points:');
n=sc.nextInt();
if (n < 3)
{
System.out.println('The number of data points should be greater than 2.');
}
else
{
//declare an array of n size to store integral data points
int[] dataset = new int[n];
//allow user inputs
int i = 0;
for (i = 0; i < n; i++)
{
System.out.print('['+i+']:');
dataset[i] = sc.nextInt();
}
//sort the data set
bubblesort(dataset, n);
//calculate the mean
int sum = 0;
int j = 0;
while (j < n)
{
sum = sum + dataset[j];
j++;
}
mean = (float)sum / n;
//calculate median
//If n is odd, median=dataset[n/2]
//If n is even, median=(dataset[n/2]+dataset[1+n/2])/2
//The index of array starts from 0, so you need to subtract 1 from the indices used in calculating the median
if (n % 2 != 0) median = dataset[n / 2];
else median = (dataset[(n / 2) - 1] + dataset[n / 2]) / (float)2;
//calculate the mode
int[][] mode = new int[n][2];
//initialize 2D array storing numbers of occurences, and values
for (i = 0; i < 2; i++)
for (j = 0; j < n; j++) mode[j][i] = 0;
mode[0][0] = 1;
for (i = 0; i < n; i++)
for (j = 0; j < n - 1; j++)
if (dataset[i] dataset[j + 1]) { ++mode[i][0]; mode[i][1] = dataset[i]; }
int max;
int k = 0;
max = mode[0][0];
for (j = 0; j < n; j++)
if (max < mode[j][0]) { max = mode[j][0]; k = j; }
//calculate standard deviation,std
float temp = 0.0f;
for (j = 0; j < n; j++)
{
temp = temp + (float)Math.pow(dataset[j] - mean, 2);
}
std = (float)Math.sqrt(temp / (n - 1));
//Show results
System.out.println('Statistical Information:');
System.out.println(');
System.out.println('Arithmetic mean:'+mean);
System.out.println('Median:'+median);
if (mode[k][1] != 0)
System.out.println('Mode:'+ mode[k][1]);
else System.out.println('Mode: no mode');
System.out.println('Standard deviation:'+std);
}
//
}
How To Find Mean Mode Median
}
Java Program To Calculate Mean Median Model
![Java Program To Calculate Mean Median Mode Java Program To Calculate Mean Median Mode](https://www.wikihow.com/images/thumb/b/b9/Write-a-Program-in-Java-to-Calculate-the-Mean-Step-3-Version-2.jpg/aid2613724-v4-728px-Write-a-Program-in-Java-to-Calculate-the-Mean-Step-3-Version-2.jpg)
Comments
Java Program To Calculate Mean Median Mode And Range
![Mean Mean](https://miro.medium.com/max/960/0*0vNXA43Vps-jzkjS.png)
Java Program To Calculate Mean Median Models
Specifically, we want to be able to determine the mean, median, and mode of the array of integers. Mean is the arithmetic average of a series of numbers. It is found by summing all of the numbers and then dividing by the number of elements (numbers) in the series. Median is the middle number (of a sorted set). Therefore, Mode = 4 Algorithm to find Mean, Median and Mode in C. Declare an array of size n and initialize with the data in it. Algorithm for mean: declare a variable sum and initialize it with 0. Start loop form i = 0 to n. For each arri, add arri in the sum. Print means of data as sum/n; Algorithm for median: sort the array.