Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

second biggest number in array

import java.util.Random;

public class secondBiggestNumber {

    public static void main (String[] args){

        // creating array
        int[] array1 = new int[8];

        // randomly filling with 8 elements
        fill(array1, -100, 100);

        // printing them out
        for (int i = 0; i < array1.length ; i++) {
            System.out.println(array1[i] + " ");
        }

        //sorting them by ascending order
        int current = 0;
        for (int i = 0; i < array1.length ; i++) {
            for (int j = i+1; j < array1.length ; j++) {
                if (array1[i]>array1[j]) {
                    current = array1[i];
                    array1[i] = array1[j];
                    array1[j] = current;
                }
            }
        }

        //printing out the second biggest number
        System.out.println("The second biggest number: " + array1[6] );
    }

    private static void fill(int[] array, int lowerBound, int highBound) {
        Random random = new Random();
        int bound = highBound - lowerBound;
        for (int i = 0; i < array.length; i++) {
            array[i] = random.nextInt(bound) + lowerBound;
        }
    }
}
Source by www.geeksforgeeks.org #
 
PREVIOUS NEXT
Tagged: #biggest #number #array
ADD COMMENT
Topic
Name
6+7 =