[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 183
  • Last Modified:

adding up numbers in java


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;      
  }

Open in new window

Capture.PNG
0
awolarczuk
Asked:
awolarczuk
  • 5
  • 2
1 Solution
 
CEHJCommented:
Try
variance += (1.0/Count * (Math.pow(num[i] - mean)));

Open in new window

0
 
CEHJCommented:
Scratch that - pow has two arguments - but you should get the idea - you need num[ i ] in there
0
 
CEHJCommented:
So
variance += (1.0/Count * (Math.pow(num[i] - mean, 2)));

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
CEHJCommented:
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;
    }

Open in new window

0
 
awolarczukAuthor Commented:
thanks so much u r grwat
0
 
awolarczukAuthor Commented:
outstanding
0
 
CEHJCommented:
A small, but important adjustment: your 'Count' parameter implies the following:


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;
    }

Open in new window

0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now