I don't know how to define a variable for bubbleSort java
I am creating a program that calculates the mean (return as a double), median (return as a double), mode (return as an int), and the standard deviation of an array that I have selected myself. The most helpful things I can find are codes where an array is the user input.
I used this and some others similar to this tutorial , as well as my book and class notes. Some things I just keep tinkering with until they work.
But as I said, in my code, I would like to just put the array in myself and not collect input from the user. I'm stuck on the median. I typed it all in, but the compiler throws 1 error which says:
1 error found: File: C: \ Users \ Cori \ Desktop \ Statistics.java [line: 41] Error: method bubbleSort (int []) - undefined for type Statistics
I did bubbleSort exactly as the link says and I tried all kinds of crazy stuff. I think maybe it has something to do with a variable that is not being defined, but I really don't know because this is very foreign to me. Here is my entire code so far. I feel like if I can just figure this out, the rest of my project will be very simple.
public class Statistics {
public static void main(String[] args) {
int[] a = { 22, 44, 66, 55, 33 };
double mean;
double median;
median = calcMed(a);
mean = calcMean(a);
System.out.println("Median:" + median);
System.out.println("Mean:" + mean);
}
public static double calcMean(int[] a) {
// int[]array = {22,44,66,55,33};
int i;// =0;
int sum = 0;
double mean = 0;
for (i = 0; i < a.length; i++) {
System.out.println(a[i]);
sum = sum + a[i];
}
{
mean = ((double) sum / ((double) a.length));
System.out.println();
}
{
return mean;
}
}
// Calulate median
public static double calcMed(int[] a) {
int i;
int sum = 0;
int[] sortedArr = bubbleSort(a);
double median = 0;
{
int index = (sortedArr.length - 1) / 2;
median = sortedArr[index];
}
for (int v : sortedArr) {
System.out.println(v);
}
return median;
}
}
Please don't drink my formatting (just some advice would be nice). I just need to know how to fix bubbleSort so that I can calculate the median. Also I know that some things are unnecessary, so if you can also give me some pointers on what can be removed and what could be easier.
I understood that.
source to share
You are missing a method bubbleSort
(copied from the relevant link):
/**
* This program returns a sorted version of the input array.
*
* @param arr
* @return
*/
public static int[] bubbleSort(int[] arr)
{
// We must sort the array. We will use an algorithm called Bubble Sort.
boolean performedSwap = true;
int tempValue = 0;
// If we performed a swap at some point in an iteration, this means that array
// wasn't sorted and we need to perform another iteration
while(performedSwap)
{
performedSwap = false;
// Iterate through the array, swapping pairs that are out of order.
// If we performed a swap, we set the "performedSwap" flag to true
for (int i=0; i < arr.length; i++)
{
if (arr[i] > arr[i+1])
{
tempValue = arr[i];
arr[i] = arr[i+1];
arr[i+1] = tempValue;
performedSwap = true;
}
}
}
return arr;
}
Without this method, you cannot sort the array (there are better solutions than bubblesort, but fine for this case).
Mistake:
1 error found: File: C: \ Users \ Cori \ Desktop \ Statistics.java [line: 41] Error: method bubbleSort (int []) undefined for type Statistics
Reports that there is no method bubbleSort()
with a parameterint[]
source to share