0

I have to finish this method that returns the sum of the CDD calculations over a whole month. My cdd method that I use to calculate the sum is below first. Can I get a hint as to how to go about this? I struggle with arrays and I'm unsure of how to start.

public static double cdd(int max, int min)
{
    double average = ((max + min) / 2.0);
    double cdd = 0.0;

    if (average > 65.0)
    {
        cdd = average - 65.0;
    }
    else
    {
        cdd = 0.0;
    }

    if (max == -999 || min == -999)
    {
        cdd = 0.0;
    }
    else if (max < min)
    {
        cdd = 0.0;   
    }

    return cdd;


public static double monthCdd(int[] max, int[] min)
{
    double sum = 0.0;

    max = new int[31];
    min = new int[31];
    cdd(,);


    return sum;        
}    
1
  • Where does your data come from? I mean: your arrays max and min both have data elements in them, so where does this data come from? Do you make it up? Or what? Commented Apr 8, 2014 at 5:10

3 Answers 3

1

After populating the max and minarrays,

  1. Start a for loop which will run from 0 to max.length - 1 with the counter variable i.
  2. Call the cdd() method in loop by passing the current index element from each of the arrays, something like this, cdd(max[i], min[i]).
  3. Keep adding the value returned from cdd() method to the sum variable, sum += cdd(max[i], min[i]);
Sign up to request clarification or add additional context in comments.

Comments

0

You might want to do something like this:

final int length= 31;
double sum=0.0
max =new int[length];
min= new int[length];
//add code to initialize the arrays
for(int i=0;i<length;i++) {
  sum +=cdd(min[i],max[i]);
}

Comments

0

Well, if you struggle with arrays, then the first thing I'd suggest is playing around with iterators in Java.

In pseudo, the method should perform elementary operations on each index of two arrays. Accessing the content of both arrays will require an iterator that "steps" through each index of the array, beginning with 0 and ending with the last index (hence why others have suggested declaring a static length).

The statement:

for (int i = 0; i < length; i++) {

simply states "for every (arbitrary variable) i on the interval [0, length), perform some action using i, then increment i by one for the next loop"

Understand this, and you have the means to add each index of two arrays (same size), i.e.

sum += arr1[i] + arr2[i]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.