Solved

# adding up numbers in java

Posted on 2011-05-11
176 Views

Hi all i am trying to work out a sum in Java and i am getting a error, my sum has to loop through an array and add it as it goes
here is the error i am getting and i will also show you the sum i am trying to work out and you can see if i am on the right trackwith my code

.\Calculate.java:90: operator - cannot be applied to double[],double
variance += (1/Count * (Math.pow(num - mean)));
^
1 error

``````public static double calVariance(double num[], double mean, int Count)
{
Double variance;
for (int i = 0; i <num.length; i++)
{
variance += (1/Count * (Math.pow(num - mean)));
}
System.out.println (variance);
return 1;
}
``````
Capture.PNG
0
Question by:awolarczuk

LVL 86

Expert Comment

Try
``````variance += (1.0/Count * (Math.pow(num[i] - mean)));
``````
0

LVL 86

Expert Comment

Scratch that - pow has two arguments - but you should get the idea - you need num[ i ] in there
0

LVL 86

Expert Comment

So
``````variance += (1.0/Count * (Math.pow(num[i] - mean, 2)));
``````
0

LVL 86

Accepted Solution

So the whole method
``````public static double calVariance(double[] num, double mean, int Count) {
double variance = 0.0;

for (int i = 0; i < num.length; i++) {
variance += (1.0 / Count * (Math.pow(num[i] - mean, 2)));
}

return variance;
}
``````
0

Author Comment

thanks so much u r grwat
0

Author Closing Comment

outstanding
0

LVL 86

Expert Comment

``````public static double calVariance(double[] num, double mean, int Count) {
double variance = 0.0;

for (int i = 0; i < Count; i++) {
variance += (1.0 / Count * (Math.pow(num[i] - mean, 2)));
}

return variance;
}
``````
0

## Featured Post

### Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…