Create a program RandomArray with an array containing 100 random integers from 1
ID: 3860878 • Letter: C
Question
Create a program RandomArray with an array containing 100 random integers from 1-100.
Use a for loop to generate and store random numbers in the array. Use the SecureRandom class to generate the random numbers.
Sort the array. Use the bubble sort method below to sort the array in ascending order (low to high).
Display the sorted array in right-aligned columns with ten numbers to a line.
Determine how many duplicates there are and display each duplicated number and the count. Create a duplicates array to hold the count of each random number. Hint: array size should be 101. The array index should represent the number.
Determine all numbers from 1-100 that are missing from the array of random numbers and display them in right-aligned columns five numbers to a line.
Use the command prompt window (terminal) for your program.
Do NOT create any other methods or use any built-in Java methods.
Be sure to label all output.
It should look like:
Explanation / Answer
import java.security.SecureRandom;
public class HelloWorld{
public static void main(String []args){
SecureRandom random = new SecureRandom();
int r,i;
int[] count = new int[101];
int[] rans = new int[100];
// Generating random numbers
for(i=0;i<100;i++)
{
r = random.nextInt(100)+1;
count[r]++;
rans[i]=r;
}
// Bubble sort
for (int k = 0; k < 100; k++)
for (int j = 0; j < 100-k-1; j++)
if (rans[j] > rans[j+1])
{
int temp = rans[j];
rans[j] = rans[j+1];
rans[j+1] = temp;
}
// printing sorted array
System.out.println(" Sorted Array");
for(i=1;i<=100;i++)
{
System.out.printf("%-4d",rans[i-1]);
if(i%10==0)
System.out.println();
}
// printing duplicates and count
System.out.println(" Duplicate Count");
for(i=1;i<=100;i++)
{
if(count[i]>0)
{
System.out.println(i+" "+count[i]);
}
}
// finding and printing missing numbers
System.out.println(" Missing Numbers");
int x = 0;
for(i=1;i<=100;i++)
{
if(count[i]==0)
{
x++;
System.out.printf("%-4d",i);
if(x%5==0)
System.out.println();
}
}
}
}
/* Sample Output
Sorted Array
2 3 4 4 7 7 7 9 9 11
13 14 14 16 16 16 19 19 20 22
23 24 25 25 26 28 28 28 29 29
30 31 32 37 38 39 40 41 43 44
44 44 45 47 47 51 51 51 52 54
54 56 57 58 58 60 60 61 62 62
64 65 65 66 66 67 67 68 69 72
72 73 74 77 77 77 79 80 82 82
85 87 87 88 88 90 90 91 91 92
92 92 92 95 95 95 96 96 98 99
Duplicate Count
2 1
3 1
4 2
7 3
9 2
11 1
13 1
14 2
16 3
19 2
20 1
22 1
23 1
24 1
25 2
26 1
28 3
29 2
30 1
31 1
32 1
37 1
38 1
39 1
40 1
41 1
43 1
44 3
45 1
47 2
51 3
52 1
54 2
56 1
57 1
58 2
60 2
61 1
62 2
64 1
65 2
66 2
67 2
68 1
69 1
72 2
73 1
74 1
77 3
79 1
80 1
82 2
85 1
87 2
88 2
90 2
91 2
92 4
95 3
96 2
98 1
99 1
Missing Numbers
1 5 6 8 10
12 15 17 18 21
27 33 34 35 36
42 46 48 49 50
53 55 59 63 70
71 75 76 78 81
83 84 86 89 93
94 97 100
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.