Program Description: Write a program to create an array based on a * size specif
ID: 3759764 • Letter: P
Question
Program Description: Write a program to create an array based on a * size specified by the user. After creating the array, the * program will create a random number that represents how many * numbers to insert into the array. A 'for' loop will be used to * insert that many numbers into the array. The numbers inserted * into the array will be random numbers between 10 - 20. *BEGIN Array Input Using Sentinel Value * Prompt User for size of the array * Instantiate the array * Create a random # (0 - array size) for how many #s to insert * FOR (each number to be inserted into the array) * Create a random number between 10 - 20 * Insert the number into the array * ENDFOR * Clear the Screen * FOR (Each value in the array) * Display value on its own line * END FOR * END Lab01 - Array Input Using Sentinel ValueExplanation / Answer
import java.util.Random;
import java.util.Scanner;
public class RandomNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Random r = new Random();
System.out.print("Enter size: ");
int len = sc.nextInt();
int[] arr = new int[len];
for (int i = 0; i < arr.length; i++) {
arr[i] = r.nextInt(11) + 10;
}
System.out.println("Array: ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+", ");
}
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.