Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Your exercise will involve creating a main program to create the array, call the

ID: 3848720 • Letter: Y

Question

Your exercise will involve creating a main program to create the array, call the sort function and display the sorted results. The array will contain 10000 elements and will be filled with double values from a random number generator. Random doubles may be generated by the repeated calling of the “random” function from the Java Math library, as follows:

// fill an existing array with random doubles

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

{

     ArrayToBeSorted[i] = Math.random( );

}

This will fill an array named ’ with 10000 random doubles, values between 0.0 and 0.99999999 (1.0 won’t be used).

Testing:

There is no good reason to display 10,000 numbers since no human would be able to see if an out-of-order situation exists. So the final part of the main program will contain a loop that will compare each element to the one immediately after it in the sorted array and display a message if they are out of order.

Testing will consist of running the program. Since the elements are random and it checks itself for correct sorting, no further testing is necessary.

Explanation / Answer

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.Random;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
   public static void main (String[] args) throws java.lang.Exception
   {
   double[] testArray = new double[10000];
       int counterAsc = 0;
   int counterDesc = 0;
for (int i=0; i<10000; i++) {
testArray[i] = Math.random();
}
System.out.println(Arrays.toString(testArray));
for(int i=0; i<10000-1; i++)
{
   if(testArray[i]<testArray[i+1]){counterAsc++;}
   else if(testArray[i]>testArray[i+1]){counterDesc++;}
}
   if(counterDesc == 0){System.out.println("Sorted in Ascending");}
       else if(counterAsc == 0){System.out.println("Sorted in descscending");}
       else System.out.println("Not Sorted");
   }
}

Note: I have used the online IDE to test the code.so Please cut the function within the Main class and past in the main function you created.Sorry for the format.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote