Question

In: Computer Science

Write a method with the following header to return an array of integer values which are...

Write a method with the following header to return an array of integer values which are the largest values from each row of a 2D array of integer values

public static int[] largestValues(int[][] num)

For example, if the 2D array passed to the method is:

8 5 5 6 6
3 6 5 4 5
2 5 4 5 2
8 8 5 1 6

The method returns an array of integers which are the largest values from each row as follows:

8
6
5
8

In the main method, create a 2D array of integer values with the array size 4 by 5 and invoke the method to find and display the largest values from each row of the 2D array.

You can generate the values of the 2D array using the Math.random() method with the range from 0 to 9.

Solutions

Expert Solution

JAVA CODE :

import java.util.*;
import java.lang.*;

public class Main
{
public static int[] largestValues(int[][] num)
{
int max = -1;
int result[] = new int[num.length];
for(int i = 0; i < num.length; i++) // ioterate through each row
{
for(int j = 0; j < num[i].length; j++)
{
if(num[i][j] > max) // find maximum element
{
max = num[i][j];
}
}
result[i] = max; // store them in the 1D array
max = -1;
}
return result; // return the result
}
  
   public static void main(String[] args) {
       Scanner s = new Scanner(System.in);
       int row = 4;
       int column = 5;
       int a[][] = new int[row][column];
       for(int i = 0; i < row; i++){
       for(int j = 0; j < column; j++){
       a[i][j] = (int)(Math.random() * 10); // generate random integers
       }
       }
       int result[] = largestValues(a);
       System.out.println("2D array with generated random numbers are : ");
       for(int i = 0; i < row; i++){
       for(int j = 0; j < column; j++){
       System.out.print(a[i][j] + " "); // print the 2D array
       }
       System.out.println();
       }
       System.out.println("Largest element in each row is : ");
       for(int i = 0; i < row; i++)
       {
       System.out.println(result[i]); // print the largest elemet in each row
       }
   }
}
OUTPUT :


Related Solutions

Write a method that, given an array of grades stored as double values, computes and return...
Write a method that, given an array of grades stored as double values, computes and return their mean.    Write another method that will return an array of the mean grade obtained by an arbitrary number of student.    This method will take a 2-dimensional array of double values in which each row corresponds to the grade vector    for a different student. You can compute the mean for each row as you did before...    Once you are done,...
Suppose you are provided with an array of integer values. Write pseudocode for a program to...
Suppose you are provided with an array of integer values. Write pseudocode for a program to determine how many unique values are in the array. Your solution should require time that is linearithmic in the size of the array. (For example, if the array contains the values {1,3,4,1,2}, the answer is "4", because the array contains the values 1, 2, 3, and 4)
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method could be public int BinarySearch(int target, int low, int high)
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method could be public int BinarySearch(int target, int low, int high)
Write a method that takes an integer array as its parameter and sorts the contents of...
Write a method that takes an integer array as its parameter and sorts the contents of the array in ascending order using the Insertion Sort algorithm. Call this method after the original array and other stats have been displayed. Once the array has been sorted by your method, display its contents to the screen in the same manner as the original array was displayed. CODE SO FAR: import java.util.*; public class ArrayInteger { public static void getRandomNumber(int A[],int n){ Random...
Write a C program to Declare an integer array of size 10 with values initialized as...
Write a C program to Declare an integer array of size 10 with values initialized as follows. int intArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; Compute each item of a new array of same size derived from the above array by: adding first item (intArray[0]) of the array with 3rd, 2nd with 4th, 3rd with 5th and so on. For the last-but-one item intArray[8], add it with first item and for the last item (intArray[9])...
4. Write a function called mean that will return the average value of an integer array....
4. Write a function called mean that will return the average value of an integer array. The integer array and its length must be passed to the function as parameters. 5.Write a function called max that will return the index of the max value of an integer array. The integer array and its length must be passed to the function as parameters. E.g. array = {1,2,3,4,5,6,7,8,9,0} max = 9 index = 8 6.Write a function called variance that calculates and...
Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer called removeItem.
Java programming:Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer called removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (Note that after deleting the element, the array size is reduced by 1.) You may assume that the array is unsorted.Now re-do this exercise and name it ExInsertionSort....
// Java // This method takes an integer array as well as an integer (the starting...
// Java // This method takes an integer array as well as an integer (the starting // index) and returns the sum of the squares of the elements in the array. // This method uses recursion. public int sumSquaresRec(int[] A, int pos) { // TODO: implement this method        return -1; // replace this statement with your own return }    // This method takes a character stack and converts all lower case letters // to upper case ones....
Initialize and Print an Array Write a program that accepts two integer values, called "arraySize" and...
Initialize and Print an Array Write a program that accepts two integer values, called "arraySize" and "multiplier", as user input. Create an array of integers with arraySize elements. Set each array element to the value i*multiplier, where i is the element's index. Next create two functions, called PrintForward() and PrintBackward(), that each accept two parameters: (a) the array to print, (b) the size of the array. The PrintForward() function should print each integer in the array, beginning with index 0....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT