(JAVA) write a program that store random number from 1 to 52 in a array Create a
ID: 3752639 • Letter: #
Question
(JAVA) write a program that store random number from 1 to 52 in a array
Create another array, store first four number and find the biggest one.
Run 10000 times to see the occurences of each.
After I get the biggest number, I need help for run it 10000 times and find the occurences, please help
out put will be like this (should be increasing order)
Number: 4 Times:2
Number:5 Times:3
until 52 Number 52 Times 6XXX
import java.util.Arrays;
import java.util.Random;
public class RandomArray {
public static void main(String[] args) {
// creating Random class object to generate random numbers
Random r=new Random();
// creating two arrays
int arr[]=new int[52];
int arr2[]=new int[4];
// initialize first array with 1 to 52 random generated numbers
for(int i=0;i<52;i++)
arr[i]=r.nextInt(52-1)+1;
System.out.println("First four elements which are stored in another array are: ");
for(int i=0;i<4;i++) // storing first four numbers to second array
{
arr2[i]=arr[i];
System.out.print(arr2[i]+" ");
}
int max=arr2[0];
for(int i=1;i<4;i++)
{
// finding biggest number in second array
if(arr2[i]>max)
max=arr2[i];
}
System.out.println(" Biggest number is: "+max);
Explanation / Answer
import java.util.Arrays;
import java.util.Random;
public class RandomArray {
public static void printOccurences(int arr[], int index){
Random r=new Random();
int max=arr[index];
int ar2[]=new int[52];
for(int i=0;i<10000;i++){
int num=r.nextInt(52-1)+1;
ar2[num-1]++; //increasing on number index
}
for(int i=1;i<=52;i++){ //loop to print the occurences in increasing order
if(i==arr[0]){
System.out.println(arr[0]+" "+ar2[arr[0]-1]);
}
else if(i==arr[1]){
System.out.println(arr[1]+" "+ar2[arr[1]-1]);
}
else if(i==arr[2]){
System.out.println(arr[2]+" "+ar2[arr[2]-1]);
}
else if(i==arr[3]){
System.out.println(arr[3]+" "+ar2[arr[3]-1]);
}
}
}
public static void main(String[] args) {
// creating Random class object to generate random numbers
Random r=new Random();
// creating two arrays
int arr[]=new int[52];
int arr2[]=new int[4];
// initialize first array with 1 to 52 random generated numbers
for(int i=0;i<52;i++)
arr[i]=r.nextInt(52-1)+1;
System.out.println("First four elements which are stored in another array are: ");
for(int i=0;i<4;i++) // storing first four numbers to second array
{
arr2[i]=arr[i];
System.out.print(arr2[i]+" ");
}
int max=arr2[0];
int index=-1;
for(int i=1;i<4;i++)
{
// finding biggest number in second array
if(arr2[i]>max){
max=arr2[i];
index=i;
}
}
System.out.println(" Biggest number is: "+max);
//print occurences of 4 numbers
printOccurences(arr2, index);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.