0
votes

I need to use this method:

public static int countNegative(double[] numbers, int count){ }

to count the number of negative numbers in a double array. I could easily do it if I could include a 3rd parameter, sum, but I can only use the array and an int. I'm completely stuck. I've tried a few things, but cannot get it right. I've gotten everything from the size of the array to ArrayIndexOutOfBounds, but never the right answer. Could anyone help me out with this?

-EDIT-

Well here is the exact assignment:

Write a program that reads in a sequence of numbers (not necessary integers) from standard input until 0 is read, and stores them in an array, similar to what you did in assignment 2. This part is done using iteration . You may assume that there will not be more than 100 numbers.

Then compute the maximum number stored in the array, the count of negative numbers, and compute the sum of positive numbers, using recursion. Thus you will create recursive methods findMax, countNegative, and computeSumPositive in Assignment9 class and they will be called by a main method.

Specifically, the following recursive methods must be implemented (These method should not contain any loop):

public static double findMax(double[] numbers, int count)  -> It finds the maximum number in the array, count is the number of elements

in the array

public static int countNegative(double[] numbers, int count) -> counts the negative integers

public static double computeSumPositive(double[] numbers, int count) -> sums number of positive integers

findMax() was easy:

public static double findMax(double[] numbers, int count){
        if(numbers.length - 1 == count)
            return numbers[count];
        else 
            return Math.max(numbers[count], findMax(numbers, count+1));
    }

This is my most recent attempt at countNegative. It just returns 99 (I have the array initialized with 100 elements):

public static int countNegative(double[] numbers, int count){
        int i=0;
        if(numbers[count]<0)
            i=1;
        if(numbers.length-1==count)
            return count;
        else
            return i+countNegative(numbers,count+1);
     }

I should be able to figure out the computeSumPositive if I can figure out this negative one.

Count can be whatever you need it to be. I used it more as an index in findMax.

5
Do you have to use recursion? Also, what's the parameter count for? It will help a lot if you quote the requirements you've been given in the question, and also show the gist of the code you've tried.David Z
Use count as the index.Sotirios Delimanolis
I added more code. Yes I have to use recursion. Count is whatever you want it to be. Requirements have been quoted. The most recent attempt was added. This is "homework." And yes, count is probably best used as the index.rphello101

5 Answers

2
votes

What is the use of count? It would make sense if it is index:

public static int countNegative(double[] numbers, int index)
{
    if(index == numbers.length) return 0;
    return (numbers[index] < 0 ? 1 : 0) + countNegative(numbers, index + 1);
}

and call it like this:

int count = countNegative(array, 0);
1
votes

Use the int parameter as an index into the numbers array. Determine if the current index's value is negative (count 0 or 1 here). Then return the sum of that 0/1 count and the recursive call that looks at the next index position. The base case is when you've run past the end of the array, which returns 0.

1
votes
public static int countNegative(double[] numbers, int count){  
   if(count == numbers.length){  
        return 0;  
    }  
    int sum = countNegative(numbers, count + 1);  
    if(numbers[count] < 0){  
          sum++;  
    }  
    return sum;  
}

You call this method: countNegative(numbers, 0);
count is to be used as the base condition of recursion. You return the result back up the stack

Example:

double a[]={-12.0,1.0,0.0,23.0,-23.0,-9.0};  
System.out.println(countNegative(a, 0));  

I get 3 in console

0
votes

Start by implementing it for an array with 0 elemtents. The for an array of 1 element. The for an array of more,usign the previous results...

0
votes

here's how it might work

public static int countNegative(double[] numbers){
    int result = numbers[0] < 0 ? 1 : 0;

    if(numbers.length > 1) {
        result += countNegative(Arrays.copyOfRange(numbers, 1, numbers.length));
    }

    return result;
}

You don't need the count parameter because of the way recursion works. when you call the function with an array it first determines if the first element is less than zero, making it negative. Next it checks if the array has more than one element, and if it does it calls itself with everything but the first element of the array and adds that to the result.

After that it returns the result which depending if it's in a recursive call or not, will either add it to the result of the call above it or give it back to the person calling it.