Write a complete Java program to: (a) Create an array of type double of size 10,
ID: 3829850 • Letter: W
Question
Write a complete Java program to:
(a) Create an array of type double of size 10,000
(b) Fill the array with 10,000 randomly generated numbers in the range 0 to 100,000
(c) Determine the largest value of the randomly generated numbers
(d) Prompt the user for a double number
(e) Report to the user if their number is different than the largest value in the array
Report what the difference or report to the user if the 2 numbers are the same.
Create an array of type double of size 10,000
(Multiply Math.random( ) by 100000, not 10000)
Fill the array with 10,000 randomly generated numbers in the range 0 to 100,000.
Determine the largest value of in the array of
the randomly generated numbers. Prompt the user for a number.
Report to the user (via System.out.println) if the user's number
is different than the largest value in the array, and what is the difference or
report the numbers are the same. Use the Scanner class in your program */
import java.util.Scanner;
public class Question40
{
public static void main(String[] args)
{
.etc. etc. .etc. Please write the rest of the program
Explanation / Answer
Question40.java
import java.util.Scanner;
public class Question40 {
public static void main(String[] args) {
// Create a double array of size 10000
double nos[] = new double[10000];
// Scanner object is used to get the inputs entered by the user
Scanner sc = new Scanner(System.in);
/*
* This for will generate the random numbers and populate them into an
* array
*/
for (int i = 0; i < nos.length; i++) {
nos[i] = 100000 * Math.random();
}
double max = nos[0];
// This for loop will find the maximum element in the array
for (int i = 0; i < nos.length; i++) {
if (max < nos[i])
max = nos[i];
}
// Prompting the user to enter a number
System.out.print("Enter a number :");
double num = sc.nextDouble();
// If the user entered number is not equal to max number display
// difference
if (num != max) {
System.out
.println("Entered number is different from Largest value in the array");
if (max > num)
System.out.println("The Differenc is :" + (max - num));
else
System.out.println("The Differenc is :" + (num - max));
} else
System.out.println("Entered number is same as from Largest value in the array");
}
}
_____________________
Output:
Enter a number :4567.78
Entered number is different from Largest value in the array
The Differenc is :95423.61098956226
__________________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.