Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

write a program that reads in a sequence of numbers (not necessary integers) fro

ID: 3621225 • Letter: W

Question

write a program that reads in a sequence of numbers (not necessary integers) from standard input until 0 is read, and store them in an array (including 0). This is done using iteration (choose one of for, while, or do while loop). You may assume that there will not be more than 100 numbers.

Then compute the minimum number, the sum of positive numbers, the sum of numbers at odd indexes (1,3,5,...) of the array, and count the number of negative numbers, using recursion.

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

public static double findMin(double[] numbers, int startIndex, int endIndex)

public static double computePositiveSum(double[] numbers, int startIndex, int endIndex)

public static double computeSumAtOdd(double[] numbers, int startIndex, int endIndex)

public static int countNegative(double[] numbers, int startIndex, int endIndex)

If these methods are implemented using a loop, points will be deducted (from the test cases) even if your program passes test cases. DO NOT use any Static Variables either.

Explanation / Answer

please rate - thanks

 

changed per comment

import java.io.*;
public class recursion
{public static void main(String[] args)throws IOException
  {int i=0;
  double[]s=new double[100];
  String inputString; 
  BufferedReader input=new BufferedReader
  (new InputStreamReader(System.in));
  System.out.print("Enter number "+(i+1)+"( 0 to exit): ");
  inputString=input.readLine();
  s[i]=Double.parseDouble(inputString);

  while(s[i]!=0)
      {i++;
         System.out.print("Enter number "+(i+1)+" 0 to exit): ");
       inputString=input.readLine();
  s[i]=Double.parseDouble(inputString);       }
System.out.println("Minimum number: "+findMin(s,0,i-1));
System.out.println("Sum of positive numbers: "+computePositiveSum(s,0,i-1));
System.out.println("Number of negative numbers: "+countNegative(s,0,i-1));
System.out.println("Sum of the numbers at odd indices: "+computeSumAtOdd(s,1,i-1));
}
public static double findMin(double[] numbers, int startIndex, int endIndex)
{int mid;
double top,bot;
if(startIndex==endIndex)
              return numbers[startIndex];
        mid=(startIndex+endIndex)/2;
        top=findMin(numbers,startIndex,mid);
        bot=findMin(numbers,mid+1,endIndex);
        if(top<=bot)
            return top;
        else
            return bot;
}
public static double computePositiveSum(double[] numbers, int startIndex, int endIndex)
{if(startIndex==endIndex)
    if (numbers[startIndex]%2==0)
       return numbers[startIndex];
    else
       return 0;
else
     if(numbers[startIndex]%2==0)
           return numbers[startIndex] + computePositiveSum(numbers, startIndex+1, endIndex);
     else
           return computePositiveSum(numbers, startIndex+1, endIndex);

}

public static double computeSumAtOdd(double[] numbers, int startIndex, int endIndex)
{if(startIndex==endIndex)
    if (startIndex%2==1)
       return numbers[startIndex];
    else
       return 0;
else
     if(startIndex%2==1)
           return numbers[startIndex] + computeSumAtOdd(numbers, startIndex+1, endIndex);
     else
           return computeSumAtOdd(numbers, startIndex+1, endIndex);


}
public static int countNegative(double[] numbers, int startIndex, int endIndex)
{
if(startIndex==endIndex)
    if (numbers[startIndex]<0)
       return 1;
    else
       return 0;
else
     if(numbers[startIndex]<0)
           return 1 + countNegative(numbers, startIndex+1, endIndex);
     else
           return countNegative(numbers, startIndex+1, endIndex);
}
}