Assignment 5A: Multiple Frequencies. In the last assignment, we calculated the frequency of a coin flip. This required us to have two separate variables, which we used to record the number of heads and tails. Now that we know about arrays, we can track the frequency of all numbers in a randomly generated sequence. For this program, you will ask the user to provide a range of values (from 1 to that number, inclusive) and how long of a number sequence you want to generate using that number range. You will then generate and save the sequence in an array. After that, you will count the number of times each number occurs in the sequence, and print the frequency of each number. Hints: You can use multiple arrays for this assignment. One array should hold the number sequence, and another could keep track of the frequencies of each number. Sample Output #1: What's the highest number you want to generate?: 5 How long of a number sequence do you want to generate?: 10 Okay, we'll generate 10 number (s) ranging from 1 to 5! 1, 3, 1, 3, з, 2, 4, 5, 1, 1, Frequency: 1 occurs 40.00% of the time 2 occurs 10.00% of the time 3 occurs 30.00% of the time 4 occurs 10.00% of the time 5 occurs 10.00% of the time Sample Output #2: What's the highest number you want to generate?: 3 How long of a number sequence do you want to generate?: 7 Okay, we'll generate 7 number(s) ranging from 1 to 3! 1, 1, 3, 1, 3, 1, 3 Frequency: 1 occurs 57.14% of the time 2 occurs 0.00% of the time 3 occurs 42.85% of the time
In java please
import java.util.Scanner;
import java.util.Random;
public class Assignment5A
{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int highestNumber, numberOfSequence;
int i,j;
System.out.print("What's the highest number you want to generate?: ");
highestNumber = input.nextInt();
System.out.print("How long of a number sequence do you want to generate?: ");
numberOfSequence = input.nextInt();
int numberSequence[] = new int[numberOfSequence];
int frequencies[] = new int[highestNumber];
for(i = 0; i < numberOfSequence; i++){
int rand = (int )(Math.random() * highestNumber + 1);
numberSequence[i] = rand;
}
for(i = 0; i < numberOfSequence-1; i++){
System.out.print(numberSequence[i]+", ");
}
System.out.print(numberSequence[i]);
for(i = 0; i < highestNumber; i++){
frequencies[i] = 0;
}
for(i = 0; i < highestNumber; i++){
int count = 0;
for(j = 0 ; j < numberOfSequence; j++){
if(numberSequence[j] == i+1){
count++;
}
}
frequencies[i] = count;
}
System.out.println("\nFrequency:");
for(i = 0; i < highestNumber; i++){
System.out.printf("%d occurs %.2f%% of the time \n", (i+1), (double)frequencies[i]/numberOfSequence*100);
}
}
}
Step by step
Solved in 4 steps with 3 images