In: Computer Science
1. Coding Assignment
the program will start the menu calling the method
extractLargestAndSmallestYourName(), which is specified as
below.
The method extractLargestAndSmallestYourName() will receive 2
array of integers as arguments, which are denoted as array number 1
and array number 2.
The method will then search for the largest value and the
smallest value. The search will also track from which array(s)
these values can be found; and
The information will be return an array, which has the two (2)
groups of data as follows,
1) The first group involves the largest value and from which
array(s) and has the following structure (see the sample output for
details):
largestValue numberOfArrayWithLargestValue {listOfArray}
2) The second group involves the smallest value and from which
array(s) and has the following structure (see the sample output for
details):
smallestValue numberOfArrayWithSmallestValue {listOfArray}
The method MUST NOT MODIFY the existing arrays given as
arguments.
Note also that
No global variables and data are allowed. All methods (as if you
need them) must be written and no library/package methods are
allowed.
Only output streaming through System.out and Scanner(System.in)
(with their supportive methods) are allowed to be used.
by using java programming
Code - Main.java
public class Main
{
//method to be called with 2 array as arguement and return the
smallest
public static int [][] extractLargestAndSmallestYourName(int
number1[], int number2[]){
//variable declare initialize max1 = first number of number1 array
and same for max2
int
max1=number1[0],max2=number2[0],min1=999999,min2=999999,i;
//first array extract the maximum and minimum
for(i=0;i<number1.length;i++){
if(max1<number1[i])
max1 = number1[i];
if(min1>number1[i])
min1 = number1[i];
}
//second array extract the maximum and minimum
for(i=0;i<number2.length;i++){
if(max2<number2[i])
max2 = number2[i];
if(min2>number2[i])
min2 = number2[i];
}
//initialize 2d array 2 store data for max and min and
corresponding from which array
int a[][] = new int[2][2];
//if max1 is greater than max2 means max foound in array 1 is
greater than max found in array 2
if(max1>max2){
//store the result in array
a[0][0] = max1;
a[0][1] = 1;
}
else{
a[0][0] = max2;
a[0][1] = 2;
}
//if min1 is greater than min2 means min foound in array 1 is
greater than min found in array 2
if(min1>min2){
//store the result in array
a[1][0] = min2;
a[1][1] = 2;
}
else{
a[1][0] = min1;
a[1][1] = 1;
}
return a;
}
public static void main(String[] args) {
//array first with some random number
int num1[] = {32, 54,23,65,34,76,34,12,43,545};
//array second with some random number
int num2[] = {53,65,34,12,15,56,97,56,76,87,2};
//call the method extractLargestAndSmallestYourName
and store value in 2 dimensional array b
int b[][] =
extractLargestAndSmallestYourName(num1,num2);
//print the largest and smallest result
System.out.println("Largest number found is
"+b[0][0]+" from array "+b[0][1]);
System.out.println("Smallest number found is
"+b[1][0]+" from array "+b[1][1]);
}
}
Screenshots -