Arrays Java A set or matrix of variables Declaration of one

Arrays (Java)

A set or matrix of variables.

Declaration of one dimensional integer arrays:

Int array[] = new int[size]

Or

Int[] array = new int[size]

Used for performing basic numeric operations.

Math.Random() method:

Returns a double value between 0.0 and 1.0

-Don’t forget to cast this value to int!

(int) (Math.Random())

-Define your range: max-min+1

min + (int)(Math.random()*(max-min)+1)

Random ran=new Random()

-Define your range: max-min+1

Int random=ran.nextInt(range) + min

Find the jth smallest element in an array (the array is not sorted).

•Note that it is the jth smallest element in the sorted order, not the jth distinct element.

For example,

•Given [3,7,1,15,61,4] and j = 2, return 3.

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

ACTUAL TASK (in relation to info above and below):

•create a array with size as 10, each element of the array is int value and range is (1-1000), randomly.

•j should be a random number between 1-10

•Print the array you created, and j

•Print the result

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Hint:

•Lots of ways to solve it

• Sort the array in non-decreasing order, index j of the sorted array is the answer.

•Use loop, in each iteration before j, get the smallest number, replace it with a number larger than 1000, until jth iteration.

•You can design your own method…

• Don’t use sort method of Arrays class, design your own method!

Solution

PROGRAM CODE:

package array;

public class ArrayRandom {

  

   public static int random(int min, int max)

   {

       return min + (int)(Math.random()*(max-min)+1);

   }

  

   public static int[] sort(int[] a)

   {

       for(int i=0; i<a.length; i++)

       {

           for(int j=0; j<a.length; j++)

           {

               if(a[i]<a[j])

               {

                   int temp = a[i];

                   a[i] = a[j];

                   a[j] = temp;

               }

           }

          

       }

       return a;

   }

  

   public static void main(String[] args) {

       int array[] = new int[10];

       for(int i=0; i<10; i++)

           array[i] = random(1, 1000);

      

       int j = random(1, 10);

      

       //Created Array

       for(int i=0; i<10; i++)

       {

           System.out.print(array[i] + \" \");

       }

       //jth value

       System.out.println(\"\ jth value: \" + j);

       array = sort(array);

       System.out.println(\"jth smallest value: \" + array[j-1]);//array index starts from 0

   }

}

OUTPUT:

523 322 675 133 999 764 464 731 674 411

jth value: 9

jth smallest value: 764

Arrays (Java) A set or matrix of variables. Declaration of one dimensional integer arrays: Int array[] = new int[size] Or Int[] array = new int[size] Used for p
Arrays (Java) A set or matrix of variables. Declaration of one dimensional integer arrays: Int array[] = new int[size] Or Int[] array = new int[size] Used for p
Arrays (Java) A set or matrix of variables. Declaration of one dimensional integer arrays: Int array[] = new int[size] Or Int[] array = new int[size] Used for p

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site