Question

You will implement the following method public static int[] linearSearchExtended(int[][] numbers, int key) { // Implement...

You will implement the following method

public static int[] linearSearchExtended(int[][] numbers, int key) {
// Implement this method
return new int[] {};

}   
There is a utility method provided for you with the following signature. You may use this method to convert a list of integers into an array.

public static int[] convertIntegers(List<Integer> integers)

Provided code

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

public class MatrixSearch {

// This method converts a list of integers to an array of integers
// Do not change this method
   public static int[] convertIntegers(List<Integer> integers) {
       int[] ret = new int[integers.size()];
       Iterator<Integer> iterator = integers.iterator();
       for(int i = 0; i < ret.length; i++) {
           ret[i] = iterator.next().intValue();
       }
       return ret;
   }  

// Search for a key in a matrix and returns all the indices in an array
// The indices are converted into a linear form.
   public static int[] linearSearchExtended(int[][] numbers, int key) {
   // Implement this method
   return new int[] {};
  
   }  

  
   public static void main(String[] args) {
       Scanner input = new Scanner(System.in);
      
       int nRow, nColumn = 0;
       do {
           System.out.println("Both the row and column must be > 0");
           nRow = input.nextInt();
           nColumn = input.nextInt();
       } while (nRow <= 0 || nColumn <= 0);
       int[][] numbers = new int[nRow][nColumn];
       for (int i = 0; i < nRow; i++) {
           for(int j = 0; j < nColumn; j++) {
               numbers[i][j] = input.nextInt();
           }
       }

/*       System.out.println("The matrix contains: ");
       for (int i = 0; i < nRow; ++i) {
           for(int j = 0; j < nColumn; j++) {
               System.out.printf("%5d", numbers[i][j]);
           }
           System.out.println();
       }
       System.out.println();
*/
       System.out.printf("Enter a search key:%n ");
       int key = input.nextInt();

       int[] allIndices = linearSearchExtended(numbers, key);

       if(allIndices.length == 0) {
           System.out.printf("%d not found!%n", key);
       } else {
           System.out.printf("Found %d at index: ", key);
           for(int i = 0; i < allIndices.length; i++) {
               System.out.printf("%d ", allIndices[i]);
           }
       }
   }
}

Sample Input
4
5


63 -95 9 -80 77
-27 85 -57 -83 73
99 -57 -4 19 -75
35 -45 37 24 71


19

Sample Output
Found 19 at index: 13

Explanation

Considering the rows of the matrix as contiguous arrays (row 0 has index 0 to 4, row 1 has index 5 to 9, and so on), the index of 19 is 13.

0 0
Add a comment Improve this question Transcribed image text
Answer #1


Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

public class MatrixSearch {

// This method converts a list of integers to an array of integers
// Do not change this method
public static int[] convertIntegers(List<Integer> integers) {
int[] ret = new int[integers.size()];
Iterator<Integer> iterator = integers.iterator();
for(int i = 0; i < ret.length; i++) {
ret[i] = iterator.next().intValue();
}
return ret;
}

// Search for a key in a matrix and returns all the indices in an array
// The indices are converted into a linear form.
public static int[] linearSearchExtended(int[][] numbers, int key) {
// Implement this method
int rows = numbers.length, cols = numbers[0].length;
ArrayList<Integer> indices = new ArrayList<Integer>();
for(int r = 0; r < rows; r++){
for(int c = 0; c < cols; c++){
if(numbers[r][c] == key){
indices.add(r*cols + c);
}
}
}
return convertIntegers(indices);
}

  
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
  
int nRow, nColumn = 0;
do {
System.out.println("Both the row and column must be > 0");
nRow = input.nextInt();
nColumn = input.nextInt();
} while (nRow <= 0 || nColumn <= 0);
int[][] numbers = new int[nRow][nColumn];
for (int i = 0; i < nRow; i++) {
for(int j = 0; j < nColumn; j++) {
numbers[i][j] = input.nextInt();
}
}

/* System.out.println("The matrix contains: ");
for (int i = 0; i < nRow; ++i) {
for(int j = 0; j < nColumn; j++) {
System.out.printf("%5d", numbers[i][j]);
}
System.out.println();
}
System.out.println();
*/
System.out.printf("Enter a search key:%n ");
int key = input.nextInt();

int[] allIndices = linearSearchExtended(numbers, key);

if(allIndices.length == 0) {
System.out.printf("%d not found!%n", key);
} else {
System.out.printf("Found %d at index: ", key);
for(int i = 0; i < allIndices.length; i++) {
System.out.printf("%d ", allIndices[i]);
}
}
}
}


output
----
Both the row and column must be > 0
4
5
63 -95 9 -80 77
-27 85 -57 -83 73
99 -57 -4 19 -75
35 -45 37 24 71

Enter a search key:
19
Found 19 at index: 13

Add a comment
Know the answer?
Add Answer to:
You will implement the following method public static int[] linearSearchExtended(int[][] numbers, int key) { // Implement...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int...

    must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int [] arr); public static void quickSort(int [] arr); public static void mergeSort(int [] arr); The quick sort and merge sort must be implemented by using recursive thinking. So the students may provide the following private static methods: //merge method //merge two sorted portions of given array arr, namely, from start to middle //and from middle + 1 to end into one sorted portion, namely,...

  • Our 1st new array operation/method is remove. Implement as follows: public static boolean remove( int[] arr,...

    Our 1st new array operation/method is remove. Implement as follows: public static boolean remove( int[] arr, int count, int key ) { 1: find the index of the first occurance of key. By first occurance we mean lowest index that contains this value. hint: copy the indexOf() method from Lab#3 into the bottom of this project file and call it from inside this remove method. The you will have the index of the value to remove from the array 2:...

  • JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a...

    JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a parameter. This method calculates and returns the largest even number in the list. If there are no even numbers in the array, the method should return 0. You can assume that the array is not empty. For example: Test Result int[] values = {1, 4, 5, 9}; System.out.println(getMaxEven(values)); 4 System.out.println(getMaxEven(new int[]{1, 3, 5, 9})); 0 public static int --------------------------------------------------------------------------------- 2. Write a static method...

  • Your task is to go through and implement the methods getBucketIndex, add, get, and remove. Follow...

    Your task is to go through and implement the methods getBucketIndex, add, get, and remove. Follow the comments inside respective methods. import java.util.ArrayList; import java.util.Scanner; public class HashtableChaining<K, V> {    // Hashtable bucket    private ArrayList<HashNode<K, V>> bucket;    // Current capacity of the array list    private int numBuckets;    // current size of the array list    private int size;       public HashtableChaining(int buckets){        bucket = new ArrayList<>();        numBuckets = buckets;   ...

  • COMPLETE THE BUCKETSORT METHOD public static void bucketSort(int[] array) {        int bucketCount = array.length/2;...

    COMPLETE THE BUCKETSORT METHOD public static void bucketSort(int[] array) {        int bucketCount = array.length/2;        int minIntValue = 0;        int maxIntValue = array.length - 1;        // Create bucket array        List<Integer>[] buckets = new List[bucketCount];        // Associate a list with each index in the bucket array           for(int i = 0; i < bucketCount; i++){            buckets[i] = new LinkedList<>();        }        //...

  • import java.util.Random; import java.util.ArrayList; /** * */ public class hw5_task8 { public static void main(String[] args)...

    import java.util.Random; import java.util.ArrayList; /** * */ public class hw5_task8 { public static void main(String[] args) { int[] grades = randomIntArr(10); printIntArray("grades", grades); ArrayList<Integer> indexesF_AL = selectIndexes_1(grades); System.out.println(" indexesF_AL: " + indexesF_AL); int[] indexesF_Arr = selectIndexes_2(grades); printIntArray("indexesF_Arr",indexesF_Arr); } public static int[] randomIntArr(int N){ int[] res = new int[N]; Random r = new Random(0); for(int i = 0; i < res.length; i++){ res[i] = r.nextInt(101); // r.nextInt(101) returns an in in range [0, 100] } return res; } public static void...

  • public class SelectionSorter { //Returns the index of the largest element in the arrayOfIntegers, beginning from...

    public class SelectionSorter { //Returns the index of the largest element in the arrayOfIntegers, beginning from the fromIndex. public static Integer[] selectSort(Integer[] incoming) {        Integer[] ret = new Integer[incoming.length]; for (int i = 0; i < incoming.length; i++) {            ret[i] = incoming[i];        }        int temp = 0;        for (int i = 0; i < ret.length - 1; i++) {            if (ret[i] > ret[i + 1]) {...

  • Write a program named Remainder.java then implement the following method: public static int divisibleBy(int[] arr, int...

    Write a program named Remainder.java then implement the following method: public static int divisibleBy(int[] arr, int M, int K) this method determines the number of elements in the int array (arr) that are divisible by M but not K. Then write code inside main method to test your method: your main method accepts three numbers from the command argument list, N, M, K, use the first number to create int array with size of N, assign random number between 0...

  • Fix this program package chapter8_Test; import java.util.Scanner; public class Chapter8 { public static void main(String[] args)...

    Fix this program package chapter8_Test; import java.util.Scanner; public class Chapter8 { public static void main(String[] args) { int[] matrix = {{1,2},{3,4},{5,6},{7,8}}; int columnChoice; int columnTotal = 0; double columnAverage = 0; Scanner input = new Scanner(System.in); System.out.print("Which column would you like to average (1 or 2)? "); columnChoice = input.nextInt(); for(int row = 0;row < matrix.length;++row) { columnTotal += matrix[row][columnChoice]; } columnAverage = columnTotal / (float) matrix.length; System.out.printf("\nThe average of column %d is %.2f\n", columnAverage, columnAverage); } } This program...

  • Hi, So I have a finished class for the most part aside of the toFile method...

    Hi, So I have a finished class for the most part aside of the toFile method that takes a file absolute path +file name and writes to that file. I'd like to write everything that is in my run method also the toFile method. (They are the last two methods in the class). When I write to the file this is what I get. Instead of the desired That I get to my counsel. I am having trouble writing my...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT