23.3k views
0 votes
Write a function named findmax()that finds and displays the maximum values in a two dimensional array of integers. The array should be declared as a 10 row by 15 column array of integers in main()and populated with random numbers between 0 and 100.

1 Answer

1 vote

Answer:

import java.util.*;

public class Main

{

public static void main(String[] args) {

Random r = new Random();

int[][] numbers = new int[10][15];

for (int i=0; i<10; i++){

for (int j=0; j<15; j++){

numbers[i][j] = new Random().nextInt(101);

}

}

for (int i=0; i<10; i++){

for (int j=0; j<15; j++){

System.out.print(numbers[i][j] + " ");

}

System.out.println();

}

findmax(numbers);

}

public static void findmax(int[][] numbers){

int max = numbers[0][0];

for (int i=0; i<10; i++){

for (int j=0; j<15; j++){

if(numbers[i][j] > max)

max = numbers[i][j];

}

}

System.out.println("The max is " + max);

}

}

Step-by-step explanation:

*The code is in Java.

Create a function called findmax() that takes one parameter, numbers array

Inside the function:

Initialize the max as first number in the array

Create a nested for loop that iterates through the array. Inside the second for loop, check if a number is greater than the max. If it is set it as the new max

When the loop is done, print the max

Inside the main:

Initialize a 2D array called numbers

Create a nested for loop that sets the random numbers to the numbers array. Note that to generate random integers, nextInt() function in the Random class is used

Create another nested for loop that displays the content of the numbers array

Call the findmax() function passing the numbers array as a parameter

User Renato Pradebon
by
5.6k points