Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Contemporary Issues In Database Design And Information Systems Development

Authors: Keng Siau

1st Edition

1599042894, 978-1599042893

More Books

Students also viewed these Databases questions