1

Need to find the smallest and largest in two array and the number of occurrence of those two digits in two array.

package Assignment;
import java.util.Scanner;

public class ArrayTester {
    public static double counts (int[] sampled, int val) {
        int count = 0;

        for (int i = 0; i < sampled.length; i++) {
            if (sampled[i] == val) {
                count++;
            }
        }

        return count;
    }

    public static void main(String[] args) {
        int[] numbers1;
        int[] numbers2;
        numbers1 = new int[5];
        numbers2 = new int[5];
        int smallest , largest , array_number1, array_number2;
        int smallest1 = numbers1[0], largest1 = numbers1[0];
        int smallest2 = numbers2[0], largest2 = numbers2[0];

        // create Scanner object
        Scanner input = new Scanner(System.in);

        System.out.print("Please enter 5 numbers: \n");

        for (int counter = 0; counter < numbers1.length; counter++) {
            numbers1[counter] = input.nextInt();
        }

        // finding smallest and largest value in 1st array      
        for (int i : numbers1) {            
            if (i < smallest1) {
                smallest1 = i;
            } 
            else if (i > largest1) {
                largest1 = i;
            } 
        } 

        System.out.print("Please enter 5 numbers: \n");
        for (int counter = 0; counter < numbers2.length; counter++) {
            numbers2[counter] = input.nextInt();
        }

        // finding smallest and largest value in 2nd array   
        for (int i : numbers2) {            
            if (i < smallest2) {
                smallest2 = i;
            } // end finding smallest
            else if (i > largest2) {
                largest2 = i;
            }  // end finding largest number 
        }

        // finding smallest and largest value of both the array.## Heading ##    
        if (smallest1 < smallest2) {
            smallest=smallest1;
            array_number1 = 1;
        } else {
            smallest=smallest2;
            array_number1 = 2;
        }

        if (largest1 > largest2) {
            largest = largest1;
            array_number2 = 1;
        } else {
            largest = largest2;
            array_number2 = 2;
        }

        System.out.printf("Smallest number: %d \t %d \nLargest number: %d \t %d \n", smallest,array_number1, largest, array_number2);
    }
}    

Explain how to get the count of smallest and largest value from the two array.

1
  • This code does not print even smallest and largest value correctly. Commented Oct 1, 2015 at 5:44

3 Answers 3

1

The following code will do what you want:

   public static void main(String[] args) {
        int[] numbers1;
        int[] numbers2;
        numbers1 = new int[5];
        numbers2 = new int[5];
        int smallest , largest , array_number1, array_number2;
        int smallestOccurence = 0, largestOccurence = 0;
        // create Scanner object
        Scanner input = new Scanner(System.in);

        System.out.print("Please enter 5 numbers: \n");

        for (int counter = 0; counter < numbers1.length; counter++) {
            numbers1[counter] = input.nextInt();
        }
        int smallest1 = numbers1[0], largest1 = numbers1[0];
        // finding smallest and largest value in 1st array
        for (int i : numbers1) {
            if (i < smallest1) {
                smallest1 = i;
            }
            else if (i > largest1) {
                largest1 = i;
            }
        }

        System.out.print("Please enter 5 numbers: \n");
        for (int counter = 0; counter < numbers2.length; counter++) {
            numbers2[counter] = input.nextInt();
        }
        int smallest2 = numbers2[0], largest2 = numbers2[0];

        // finding smallest and largest value in 2nd array
        for (int i : numbers2) {
            if (i < smallest2) {
                smallest2 = i;
            } // end finding smallest
            else if (i > largest2) {
                largest2 = i;
            }  // end finding largest number
        }
        // finding smallest and largest value of both the array.## Heading ##
        if(smallest1 < smallest2){
            smallest=smallest1;
            array_number1 = 1;
        } else {
            smallest=smallest2;
            array_number1 = 2;
        }

        if(largest1 > largest2){
            largest=largest1;
            array_number2 = 1;
        } else {
            largest=largest2;
            array_number2 = 2;
        }

        System.out.printf("Smallest number: %d \t %d \nLargest number: %d \t %d \n", smallest, array_number1, largest, array_number2);
        // You dont know the smallest and largest until we traverse the arrays atleast once. Once we know them we need to search for them in array again.
        for(int item : numbers1) {
            if(item == smallest) smallestOccurence++;
            if(item == largest) largestOccurence++;
        }
        for(int item : numbers2) {
            if(item == smallest) smallestOccurence++;
            if(item == largest) largestOccurence++;
        }
        System.out.println("Occurences of smallest: " + smallest + " and largest: " + largestOccurence);

    }
    public static double counts (int[] sampled, int val)
    {
        int count = 0;
        for (int i = 0; i < sampled.length; i++) {
            if (sampled[i] == val) {
                count++;
            }
        }
        return count;
    }

Explanation: We need to traverse both arrays at least once to find smallest and largest among arrays. Once we find smallest and largest then we need to traverse both arrays again to find their number of occurrences. Also we need to move the declaration of smallest1 etc. else they get value zero (default for array element) and will be smallest always (until negative numbers are stored).

Sample run:

Please enter 5 numbers: 5 5 4 3 1 Please enter 5 numbers: 5 5 2 3 1 Smallest number: 1 2 Largest number: 5 2 Occurences of smallest: 1 and largest: 4

Sign up to request clarification or add additional context in comments.

1 Comment

in sample run 1 is occurred two times right? one more thing i need to display in which array the digit is present my code it is displaying if it is in one array when it contains in both the array it is not. help on it.
0

You can use Arrays.Sort() for "numbers1" and "numbers2" arrays, it will sort your arrays smalest to largest, then you can do some operation as below.

public class Test {
    public static void main(String[] args) {


        int [] numbers1 = {1,2,3,4,7,34,5,-10};
        int [] numbers2 = {1,2,3,4,7,45,5,0};

        int smallest , largest;

        Arrays.sort(numbers1);
        Arrays.sort(numbers2);

        if (numbers1[0] <  numbers2[0])
            smallest = numbers1[0];
        else
            smallest = numbers2[0];

        if (numbers1[numbers1.length-1] >  numbers2[numbers2.length-1])
            largest = numbers1[numbers1.length-1];
        else
            largest = numbers2[numbers2.length-1];

        System.out.println(smallest);
        System.out.println(largest);
    }
}

Comments

0

Hope the following code will help you.

    int [] numbers1 = {}; //get your array1 here
    int [] numbers2 = {}; //get your array2 here
    int[] combinedArray = new int[numbers1.length + numbers2.length];

    System.arraycopy(numbers1, 0, combinedArray, 0, numbers1.length);
    System.arraycopy(numbers2, 0, combinedArray, numbers1.length, numbers2.length);
    Arrays.sort(combinedArray);

    System.out.println("Smallest: " + combinedArray[0]);
    System.out.println("Largest: " + combinedArray[combinedArray.length-1]);

    int smallestOccurrences = 0;
    int largestOccurrences = 0;

    for (int i : combinedArray) {
        if(combinedArray[0] == i)
            smallestOccurrences++;
        if(combinedArray[combinedArray.length - 1] == i)
            largestOccurrences++;
    }

    System.out.println("occurrences of smallest is: " + smallestOccurrences + " and largest is: " + largestOccurrences);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.