Tails recursive method to calculate standard deviation
public class StampaInversa {
private static class NumberWithCounterAndAverage{
private int number=0;
private int counter=1;
private int average=0;
public int getNumber(){
return number;
}
public int getCounter(){
return counter;
}
public void setNumber(int number){
this.number=number;
}
public void setCounter(int counter){
this.counter+=counter;
}
public int getAverage(){
return avrage;
}
public void setAverage(int average){
this.average=average;
}
public String toString(){
return "Number: "+number+"Counter "+counter+"Average "+average;
}//toString
}//NumeroConContatore
public static void reversePrint(){
Scanner sc= new Scanner(System.in);
System.out.println("Insert number");
int x=sc.nextInt();
if(x==0) return;
reverseprint();
System.out.println(x);
}
public static int sumPrint(){
Scanner sc=new Scanner(System.in);
System.out.println("Insert number");
int x=sc.nextInt();
if(x!=0) x+=sumPrint();
return x;
}
public static NumberWithCounterAndAverage printAverage(){
Scanner sc=new Scanner(System.in);
NumberWithCounterAndAverage ncc= new NumberWithCounterAndAverage();
System.out.println("Insert number");
int x=sc.nextInt();
if(x!=0){
NumberWithCounterAndAverage nccem= printAverage();
ncc.setNumber(x+nccem.getNumber());
ncc.setCounter(+nccem.getCounter());
}
if (x!=0){
ncc.setAverage(ncc.getNumber()/(ncc.getCounter()-1));
}
return ncc;
}
public static void main(String[] args) {
NumberWithCounterAndAverage nccem= printAverage();
System.out.println(nccem.getCounter()+" "+nccem.getNumber()+" average "+nccem.getAverage());
}
}//StampaInversa
My prof. gave me an assignment: write tail recursive functions t开发者_如何学编程o calculate:
the sum of the number inserted from input till 0 is inserted; the average of the number inserted till 0 is inserted; the standard deviation of the number inserted till 0 is inserted;The conditions to accomplish the assignment are:
No data structure allowed (arrays, arraryslist, LinkedList...), only ad hoc objects are allowed (such as the one that I have created: NumberWithCounterAndAverage) No istance variables allowed, the variables must be own only by the functions. The function must be recursive.The functions in the code above work perfectly, but now I need to made a recursive function to calculate the standard deviation with the condition descripted above. Do you have any clue?
If it is still not clear how the function should be, tell me.
I think the idea you should be going for is to have a function which takes as arguments the current count, sum, average, etc that is needed for the calculation. This would support your requirement for no instance variables unlike what you have above.
The first call to this method would pass all zeros. Each subsequent call would read in one value and if not 0 call itself with the updated values. If zero, do the final calculation and output the results.
The answer is at this post: How to efficiently calculate a running standard deviation?
With this calculation: have the method take the sum_x, sum_x2 and count. (sum_x is the sum of the elements, sum_x2 is the sum of the squares). In the iteration where you receive 0 the results are:
sum = sum_x
average = sum_x / count
stdev = sqrt((sum_x2 / count) - (average * average))
To compute the standard deviation, you must, for each amount entered, square the difference between that amount and the average. You can't know the average until all elements are entered. So you'll have to compute the standard deviation while returning from the functions.
To compute the standard deviation, you will need the count, the average, and an accumulator for the sum of the squared differences. This would be your "NumberWithCounterAndAverage" class.
During the initial calls that build the stack, I would input one number per recursion, and pass count and sum variables as parameters.
I have solved the assignment, thanks to all of you. Here is the solution, I have added the double standardDeviation, to the object in the code above
public static NumberWithCounterAndAverage calculateStandardDeviation(){
Scanner sc= new Scanner(System.in);
NumberWithCounterAndAverage nccem= new NumberWithCounterAndAverage();
System.out.println("Insert number");
int x= sc.nextInt();
int counter=1;
int sum=x;
nccem=calculateStandardDeviation(sum, counter);
int partialDeviationSum=((x-nccem.getAverage())*(x-nccem.getAverage()));
nccem.setDeviazioneStandard(partialDeviationSum+nccem.getStandardDeviation());
double standardDeviation= Math.sqrt(nccem.getStandardDeviation());
nccem.setStandardDeviation(standardDeviation);
return nccem;
}
public static NumberWithCounterAndAverage calculateStandardDeviation(int sum, int counter){
Scanner sc= new Scanner(System.in);
NumberWithCounterAndAverage nccem= new NumberWithCounterAndAverage();
System.out.println("Insert number");
int x= sc.nextInt();
int partialSum=0, partialCounter=0;
if(x!=0){
partialSum= x+sum;
partialCounter=1+counter;
nccem=calculateStandardDeviation(partialSum, partialCounter);
}
if(x==0){//I am at the top of the stack, I calculate the average and i go down
nccem.setAverage(sum/counter);
}
if(x!=0){
int sumPartialDeviation=((x-nccem.getAverage())*(x-nccem.getAverage()));
nccem.setStandardDeviation(sumPartialDeviation+nccem.getStandardDeviation());
}
return nccem;
}
精彩评论