Need to add Median and Standard Deviation to the following program, but I have no idea where to start, I know what I want it to do but am not sure how to make it happen especially since the median has to come after the sorted numbers but if I put it in as void Median (), I loose the sorted array. Please help! Any assistance is much appreciated:
import java.util.Scanner;
import javax.swing.*;
public class JScannerO
{
public JScannerO(){}
public JScannerO(int A)
{
HowMuch = A;
}
void GenerateNumbers()
{
int k,Hide;
k = 0;
while ( k < HowMuch )
{
Hide = (int) (Math.random() * 100);
if ( Hide > 9 && Hide < 100 )
{
Hanson[k] = Hide;
k++;
}
}
}
void DisplayNumbers()
{
int k;
for ( k = 0; k < HowMuch; k++)
{
System.out.println("\t\tThe numbers are : " + Hanson[k]);
}
}
void BiggestNumber()
{
int k,Biggest;
Biggest = Hanson[0];
for ( k = 0; k < HowMuch; k++)
{
if ( Biggest < Hanson[k] ) Biggest = Hanson[k];
}
System.out.println("\n\n\t\tThe biggest number is : " + Biggest);
}
void SmallestNumber()
{
int k,Smallest;
Smallest = Hanson[0];
for ( k = 0; k < HowMuch; k++)
{
if ( Smallest > Hanson[k] ) Smallest = Hanson[k];
}
System.out.println("\n\n\t\tThe Smallest number is : " + Smallest);
}
void Average()
{
int k,Sum;
double Average;
Sum = 0;
for ( k = 0; k < HowMuch; k++)
{
Sum = Sum + Hanson[k];
}
Average = 1.0 * Sum / HowMuch;
System.out.format("\n\n\t\tThe Average number is : %7.2f",Average);
}
void Sort()
{
int k,Hide;
boolean DidISwap;
DidISwap = true;
while (DidISwap)
{
DidISwap = false;
for ( k = 0; k < HowMuch - 1; k++)
{
if ( Hanson[k] > Hanson[k+1] )
{
Hide = Hanson[k ];
Hanson[k] = Hanson[k+1];
Hanson[k+1] = Hide ;
DidISwap = true;
}
}
}
System.out.println("\n\n\n\t\tThe Sorted Numbers : \n");
}
private int HowMuch;
private int[] Hanson = new int[100];
}
This is run by using a calling program
I don't understand your question.
You have a sorting algorithm there that does the work for you!
Can't you just write a
public int median() {
Sort();
return Hanson[Math.floor(Hanson/2)];
}
?
This will cost you O(N) instead of O(1), but it is not error-prone and does what you need to do - especially since your "Hanson" array has only size 100 so we can just not care at this point.
As a final remark: function names are ALWAYS in lower camelCase, ie it's sort() and not Sort(), and it's calculateSomething() and not Calculate_Something() or any other variant.
There are kind of no exceptions to this rule, and that will make your code (much) more readable. Consider installing CheckStyle in Eclipse (http://eclipse-cs.sourceforge.net/) in order to have an interactive guide to style when you are writing :-)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.