OhHeyImBri
OhHeyImBri

Reputation: 23

Array returning the sum of calculations

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

Upvotes: 0

Views: 113

Answers (3)

dchar
dchar

Reputation: 1

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]

Upvotes: 0

sumanr
sumanr

Reputation: 178

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

Upvotes: 0

Rahul
Rahul

Reputation: 45090

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]);

Upvotes: 1

Related Questions