Write a method that performs a binary search. It will accept an array of doubles
ID: 3685260 • Letter: W
Question
Write a method that performs a binary search. It will accept an array of doubles as the fust parameter and a double precision number as the second parameter. The method will return an integer result. The method should search for the location of that number in the array. If the number is found, then return that subscript as the result. Otherwise, it should return the integer -1 as the result. Chapter 8 1. Write the instructions to do each of the following. a. Declare an array of doubles with 3 rows and 5 columns named salea. b. Declare and array of doubles named ktentityMatrtx with 3 rows and 3 columns which is initialized to the following 100 010 001 2. Write some instructions to do the following. Ask the user for the row number and read it Ask the user for the column number and read it. Ask the user for the sale amount and read it. Add the sale amount to the value in sales from problem 1 using the row and column given. 3. Write instructions that will find the total for each row. the total for each column, an the grand total for the array sales from problem I. Use the following to hold the totals double 11 rowTotals - new double (3); double (] colTotals new double (5); double prandTotal 0: 4 Write a method that will accept a 2-dimensional array of doubles and return the total of the numbers m the array 5 What is a ragged array?Explanation / Answer
Answer for Question 1:
This below java binary search will do the array of doubles search on a given double kind value.
import java.util.Arrays;
public class DoublesSearch
{
public static void main(String[] args) {
// Initialize unsorted double array
double arr[] = { 5.52, 4.42, 9.91, 2.26, 3.35 };
// Sorting array
Arrays.sort(arr);
// Printing Sorted Double Array
System.out.println("The sorted double array is:");
for (double num : arr) {
System.out.println("Array Element is " + num);
}
// Entering the value to be searched
double val = 5.52;
int index = Arrays.binarySearch(arr, val);
System.out.println("The index of element 5.52 is : " + index);
}
}
Answer for Question 2:
Part a) double[][] sales =new double[3][5];
Part b) double IdentityMatrix[3][3] = {{1,0,0},{0,1,0},{0,0,1}};
Answer for Question 3:
import java.util.Scanner;
public class UserInput
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int row ;
System,out.println("Enter Row Size:");
row = sc.nextInt();
System,out.println("Enter Coloumn Size:");
int col = sc.nextInt();
System,out.println("Enter Sales amount:");
double sales[][] = new double[row][col];
for(int i=0;i<rwo;i++)
for(int j=0;j<col; j++)
sales[i][j] = sc.hasDouble();
for(int i=0;i<rwo;i++)
for(int j=0;j<col; j++)
System.out.println(sales[i][j]+" ");
}
}
Answer for Question 4:
import java.util.Scanner;
public class UserInput
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int row ;
System,out.println("Enter Row Size:");
row = sc.nextInt();
System,out.println("Enter Coloumn Size:");
int col = sc.nextInt();
System,out.println("Enter Sales amount:");
double sales[][] = new double[row][col];
for(int i=0;i<rwo;i++)
for(int j=0;j<col; j++)
sales[i][j] = sc.hasDouble();
double[] rowTotal = new double[3];
double[] colTotal = new double[3];
for(int i=0;i<rwo;i++)
{
for(int j=0;j<col; j++)
{
colTotal[j]+= sales[i][j];
}
rowTotal[i] += sales[i][j];
}
double grandTotal = 0.0;
for(int i=0;i<rwo;i++)
{
for(int j=0;j<col; j++)
{
grandTotal = sales[i][j];
}
rowTotal[i] += sales[i][j];
}
double
}
}
Answer for Question 5:
double arrayRead()
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter Row Size:");
int row = sc.nextInt();
System.out.println("Enter Col Size:");
int col = sc.nextInt();
System.out.println("Enter Enter Array Elements:");
double[][] array = new double[row][col];
int row = sc.nextInt();
double total;
for(int i=0; i<row; i++)
for(int j =0; j<col;j++)
{
array[i][j] = sc.hasDouble();
total += array[i][j];
}
return total;
}
Answer for Question 6:
A Ragged array is a n-dimensional array that need not the be reactangular means:
int[][] array = {{3, 4, 5}, {77, 50}};
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.