Question
Write the following recursive method int count_negative(int[] list, int n) that returns the number of negative integers in an integer array. 1. Base case n==0:
Write the following recursive method
int count_negative(int[] list, int n)
that returns the number of negative integers in an integer array.
1. Base case n==0:
return list[0]>0 ? 1 : 0
2. Recursive case n>0:
return (list[n]>0 ? 1 : 0) + count_negative(list,n-1)
Use the following class to test the method
public class HW2
{
public static void main(String[] args)
{
// initial data set, add a few more members
int[] data = { -1, 2, -43, 14, -5};
// replace ??? with appropriate call
System.out.println("Count = " + ??? );
}
public static int count_negative(int[] list, int n)
{
int result;
if ( n==0)
{
// your code
}
else
{
// your code
}
return result;
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started