I have questions for minimum and maximum values of numbers in arrays. I use DRJA
ID: 3763614 • Letter: I
Question
I have questions for minimum and maximum values of numbers in arrays. I use DRJAVA as my compiler and the answer cannot contain classes or anything more advanced than arrays.
1.) Write a method that accepts as an argument an array of numbers and returns the minimum value of the numbers in that array. Create an array to test your code with and call the method from main to print the min to the screen.
2.) Write a method that accepts as an argument an array of numbers and returns the maximum value of the numbers in that array. Create an array to test your code with and call the method from main to print the max to the screen.
Explanation / Answer
Hi,
Below is the program that you are looking for:
Program:
package java_project;
import java.util.Random;
import java.util.Scanner;
public class MaxMinArray {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("Enter the size of the array that you want to create");
int size = in.nextInt();
System.out.println("Enter the maximum value of the array");
int max_value = in.nextInt();
int [] array = initializeArr(max_value, size);
print(array);
findMin(array);
findMax(array);
}
//Method that is used to print the array being created
public static void print(int[] array){
for(int value:array){
System.out.print(value + ",");
}
System.out.println();
}
//Creates random values for the array to be intialized between 0 and the max value given by the user
public static int[] initializeArr(int max_value,int size){
Random random = new Random();
int [] array = new int[size];
for(int i=0;i<array.length;i++){
array[i]=random.nextInt(max_value);
}
return array;
}
//This method is used to find the min value of the array
public static void findMin(int[] array){
int min_value=array[0];
for(int i=0;i<array.length;i++){
if(array[i]<min_value){
min_value=array[i];
}
}
System.out.println("The minimum in the array is::"+min_value);
}
//This method is used to find the maximum value of an array
public static void findMax(int[] array){
int max_value=array[0];
for(int i=0;i<array.length;i++){
if(array[i]>max_value){
max_value=array[i];
}
}
System.out.println("The maximum in the array is::"+max_value);
}
}
output:
Enter the size of the array that you want to create
10
Enter the maximum value of the array
1000
83,321,371,489,896,851,777,286,516,202,
The minimum in the array is::83
The maximum in the array is::896
Explaination:
This is the flow of the program:
Hope that helps...HAPPY ANSWERING!!!!!!!!
Edited solution:
MaxArray.java:
import java.util.Random;
import java.util.Scanner;
public class MaxArray {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("Enter the size of the array that you want to create");
int size = in.nextInt();
System.out.println("Enter the maximum value of the array");
int max_value = in.nextInt();
int [] array = initializeArr(max_value, size);
print(array);
findMax(array);
}
//Method that is used to print the array being created
public static void print(int[] array){
for(int value:array){
System.out.print(value + ",");
}
System.out.println();
}
//Creates random values for the array to be intialized between 0 and the max value given by the user
public static int[] initializeArr(int max_value,int size){
Random random = new Random();
int [] array = new int[size];
for(int i=0;i<array.length;i++){
array[i]=random.nextInt(max_value);
}
return array;
}
//This method is used to find the min value of the array
public static void findMax(int[] array){
int max_value=array[0];
for(int i=0;i<array.length;i++){
if(array[i]>max_value){
max_value=array[i];
}
}
System.out.println("The maximum in the array is::"+max_value);
}
}
output:
Welcome to DrJava. Working directory is C:Userschaitanya.b
> run MaxArray
Enter the size of the array that you want to create
[DrJava Input Box]
Enter the maximum value of the array
[DrJava Input Box]
97,52,16,85,70,72,91,72,79,108,
The maximum in the array is::108
>
MinArray.java:
import java.util.Random;
import java.util.Scanner;
public class MinArray {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("Enter the size of the array that you want to create");
int size = in.nextInt();
System.out.println("Enter the maximum value of the array");
int max_value = in.nextInt();
int [] array = initializeArr(max_value, size);
print(array);
findMin(array);
}
//Method that is used to print the array being created
public static void print(int[] array){
for(int value:array){
System.out.print(value + ",");
}
System.out.println();
}
//Creates random values for the array to be intialized between 0 and the max value given by the user
public static int[] initializeArr(int max_value,int size){
Random random = new Random();
int [] array = new int[size];
for(int i=0;i<array.length;i++){
array[i]=random.nextInt(max_value);
}
return array;
}
//This method is used to find the min value of the array
public static void findMin(int[] array){
int min_value=array[0];
for(int i=0;i<array.length;i++){
if(array[i]<min_value){
min_value=array[i];
}
}
System.out.println("The minimum in the array is::"+min_value);
}
}
output:
Welcome to DrJava. Working directory is C:Userschaitanya.b
> run MinArray
Enter the size of the array that you want to create
[DrJava Input Box]
Enter the maximum value of the array
[DrJava Input Box]
3,4,4,0,2,7,4,7,5,3,
The minimum in the array is::0
>
Hope this solves your problem...HAPPY ANSWERING!!!!!