Question

In: Computer Science

java by using Scite Objectives: 1. Create one-dimensional arrays and two-dimensional arrays to solve problems 2....

java by using Scite Objectives:

1. Create one-dimensional arrays and two-dimensional arrays to solve problems

2. Pass arrays to method and return an array from a method

Problem 2: Find the largest value of each row of a 2D array

            (filename: FindLargestValues.java)

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

PROGRAM FOR DYNAMIC ARRAY SIZE:

import java.util.Scanner;
public class Main
{
   public static void main(String[] args) {
   Scanner sc=new Scanner(System.in);
   System.out.println("Enter number of rows");
   int m=sc.nextInt();
   System.out.println("Enter number of columns");
   int n=sc.nextInt();
       int[][] a=new int[m][n];
       System.out.println("2D array is:");
       for(int i=0;i<m;i++)
       {
       for(int j=0;j<n;j++)
       {
       a[i][j]=(int)(Math.random()*9); //take random integer values
       System.out.print(a[i][j]+" "); //print the array
       }
       System.out.println();
       }
       int[] r=largestValues(a); //call the largestValues function
       System.out.println("Largest value of each row for give 2D array:");
       for(int i=0;i<r.length;i++)
       {
       System.out.println(r[i]);
       }
      
   }
   public static int[] largestValues(int[][] num)
   {
   int[] b=new int[num.length];
   int max;
   for(int i=0;i<num.length;i++)
       {
       max=0; //set initially largest value as 0 i.e. max=0
       for(int j=0;j<num[0].length;j++)
       {
       if(num[i][j]>max)
       max=num[i][j];
       }
       b[i]=max;
       }
         
       return b;   
   }
}

PROGRAM FOR STATIC ARRAY SIZE:

if we give static array size 4x5 as in question statement, see the below program:

import java.util.Scanner;
public class Main
{
   public static void main(String[] args) {
       int[][] a=new int[4][5];
       System.out.println("2D array is:");
       for(int i=0;i<4;i++)
       {
       for(int j=0;j<5;j++)
       {
       a[i][j]=(int)(Math.random()*9);
       System.out.print(a[i][j]+" ");
       }
       System.out.println();
       }
       int[] r=largestValues(a);
       System.out.println("Largest value of each row for give 2D array:");
       for(int i=0;i<4;i++)
       {
       System.out.println(r[i]);
       }
      
   }
   public static int[] largestValues(int[][] num)
   {
   int[] b=new int[4];
   int max;
   for(int i=0;i<4;i++)
       {
       max=0;
       for(int j=0;j<5;j++)
       {
       if(num[i][j]>max)
       max=num[i][j];
       }
       b[i]=max;
       }
         
       return b;   
   }
}
output:

2D array is:                                                                                                                    

5 2 6 4 1                                                                                                                       

6 6 1 1 0                                                                                                                       

1 2 4 1 3                                                                                                                       

7 5 3 1 1                                                                                                                       

Largest value of each row for give 2D array:                                                                                    

6                                                                                                                               

6                                                                                                                               

4                                                                                                                               

7       


Related Solutions

java by using Scite Objectives: 1. Create one-dimensional arrays and two-dimensional arrays to solve problems 2....
java by using Scite Objectives: 1. Create one-dimensional arrays and two-dimensional arrays to solve problems 2. Pass arrays to method and return an array from a method Problem 1: Find the average of an array of numbers (filename: FindAverage.java) Write two overloaded methods with the following headers to find the average of an array of integer values and an array of double values: public static double average(int[] num) public static double average(double[] num) In the main method, first create an...
Code should be Written in C Using initialization lists, create 5 one-dimensional arrays, one for each...
Code should be Written in C Using initialization lists, create 5 one-dimensional arrays, one for each of these: hold the names of the people running in the election hold the names of the subdivision hold the vote counts in the Brampton subdivision for each candidate hold the vote counts in the Pickering subdivision for each candidate hold the vote counts in the Markham subdivision for each candidate Use the data from the example below. Your C program should take the...
Code should be Written in C Using initialization lists, create 5 one-dimensional arrays, one for each...
Code should be Written in C Using initialization lists, create 5 one-dimensional arrays, one for each of these: hold the names of the people running in the election hold the names of the subdivision hold the vote counts in the Brampton subdivision for each candidate hold the vote counts in the Pickering subdivision for each candidate hold the vote counts in the Markham subdivision for each candidate Use the data from the example below. * Your C program should take...
how to create BANKACCOUNT program using Arrays in JAVA.
how to create BANKACCOUNT program using Arrays in JAVA.
Create a “Main” method that contains two 2-Dimensional arrays of characters. The first array, which we...
Create a “Main” method that contains two 2-Dimensional arrays of characters. The first array, which we will call our visible field, needs to be 5 by 5 and should initially hold the underscore character “_”.  This will be used in our game of minesweeper to represent the possible locations the player can check. The second array, which we will call our hidden field, should also be 5 by 5 but filled with the capital letter "S” which means safety. However, we...
Create a program using Java. Create two different 3D arrays with random numbers. (lets name them...
Create a program using Java. Create two different 3D arrays with random numbers. (lets name them array1 and array 2) Add the two 3Darrays together and then get the average. Save the average on a separate 3D array.(lets name it array3) Then add array1 and array3 then get the average Save the average on a separate 3Darray(lets name it array 4) Then add array2 and array3 then get the average Save the average on a separate 3Darray (lets name it...
Longest ‘A’ Path Objectives Manipulating two-dimensional lists Using recursion to solve a problem Problem Specification Write...
Longest ‘A’ Path Objectives Manipulating two-dimensional lists Using recursion to solve a problem Problem Specification Write a Python application to find the longest ‘A’ path on a map of capital (uppercase) letters. The map is represented as a matrix (2-dimensional list) of capital letters. Starting from any point, you can go left, right, up and down (but not diagonally). A path is defined as the unbroken sequence of letters that only covers the spots with the letter A. The length...
Write a Java program that will use a two-dimensional array to solve the following tasks: 1....
Write a Java program that will use a two-dimensional array to solve the following tasks: 1. Create a method to generate a 2-dimensional array (random numbers, range 0 - 500). The array has ROW rows and COL columns, where ROW and COL are class constants. 2. Create a method to print the array. 3. Create a method to find the largest element in the array 4. Create a method to find the smallest element in the array 5. Create a...
Java program problem 1 Come up with an idea for parallel arrays Create the arrays to...
Java program problem 1 Come up with an idea for parallel arrays Create the arrays to hold 5 items in each In the main(), load the arrays with data Then output the entire contents of the arrays Create a method called find1() and pass the 2 arrays to that method. problem 2 In the find1() method, ask the user to enter a value to search for Write logic to search the first array and then output the related data from...
answer in JAVA language please In this assignment you will use: one-dimensional arrays; strings and various...
answer in JAVA language please In this assignment you will use: one-dimensional arrays; strings and various string-handling methods; file input and output; console input and output; loops; and conditional program statements. Instruction You are asked to write a program that translates a Morse code message into English language. The program will ask the user to enter filenames for two input files, one for the code translation table data and the other for the coded message data, as well as an...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT