creates a 100-element int array called elements: int[] elements = new int[100];
ID: 3629946 • Letter: C
Question
creates a 100-element int array called elements: int[] elements = new int[100];declares an int variable called count and sets it equal to the number of inputs returned by readData(elements): int count = readData(elements);
declares an int array called values and sets it equal to the 2-element array returned by getLargestAndSmallest(elements, count) with the largest and smallest values from elements: int[] values = getLargestAndSmallest(elements, count);
prints out the two values from the values array with appropriate messages
Explanation / Answer
import java.util.*;
class minmax
{
public static int readData(int[] elements)
{
Scanner in = new Scanner(System.in);
System.out.println(" Enter no of integers ");
int k = in.nextInt();
for(int i=0; i<k; i++)
{
System.out.println(" Enter integer " + (i+1) + " : ");
elements[i] = in.nextInt();
}
return k;
}
public static int[] getLargestAndSmallest(int[] elements,int count)
{
int[] values = new int[2];
int max = elements[0];
int min = elements[0];
for(int i=1; i<count; i++)
{
if(elements[i]>max) max = elements[i];
if(elements[i]<min) min = elements[i];
}
values[0] = max;
values[1] = min;
return values;
}
public static void main(String[] args)
{
int[] elements = new int[100];
int count = readData(elements);
int[] values = getLargestAndSmallest(elements, count);
System.out.println( " max value in elements is " + values[0]);
System.out.println( " min value in elements is " + values[1]);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.