47.4k views
3 votes
Design a Java program that has a two-dimensional integer array with 7 rows and 7 columns. The program should store a random integer from 0 to 999 in each element of the array. The program should then search the array for the maximum and minimum element of the array.

User Jade Han
by
8.7k points

1 Answer

4 votes

Here's an example Java program that creates a 7x7 two-dimensional integer array, stores random integers between 0 and 999 in each element, and then finds the maximum and minimum values in the array:

import java.util.Random;

public class ArrayMinMax {

public static void main(String[] args) {

int[][] arr = new int[7][7];

Random rand = new Random();

// Fill the array with random integers between 0 and 999

for (int i = 0; i < arr.length; i++) {

for (int j = 0; j < arr[i].length; j++) {

arr[i][j] = rand.nextInt(1000);

}

}

// Find the minimum and maximum values in the array

int min = arr[0][0];

int max = arr[0][0];

for (int i = 0; i < arr.length; i++) {

for (int j = 0; j < arr[i].length; j++) {

if (arr[i][j] < min) {

min = arr[i][j];

}

if (arr[i][j] > max) {

max = arr[i][j];

}

}

}

// Print the results

System.out.println("Array:");

for (int i = 0; i < arr.length; i++) {

for (int j = 0; j < arr[i].length; j++) {

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

}

System.out.println();

}

System.out.println("Minimum value: " + min);

System.out.println("Maximum value: " + max);

}

}

This program first creates a 7x7 array and fills it with random integers between 0 and 999 using a Random object. Then it searches through the array and updates the min and max variables if it finds a smaller or larger value, respectively. Finally, the program prints out the entire array and the minimum and maximum values.

User Croote
by
7.7k points
Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.