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

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

In java please

**Assignment 5A: Multiple Frequencies**

In the last assignment, we calculated the frequency of a coin flip, which required us to have two separate variables to record the number of heads and tails. Now, with our understanding of 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 specify the length of a number sequence to be generated using that range. You will then generate and save the sequence in an array. After that, count the occurrences of each number in the sequence and print the frequency of each number.

**Hints:** Multiple arrays can be used for this assignment. One array could hold the number sequence, and another could track the frequencies of each number.

### Sample Output #1:

- **Input:**
  - What’s the highest number you want to generate?: 5
  - How long of a number sequence do you want to generate?: 10

- **Output:**
  - Okay, we’ll generate 10 number(s) ranging from 1 to 5!
  - Sequence: 1, 3, 1, 3, 2, 4, 5, 1, 1
  - **Frequency:**
    - 1 occurs 40.0% of the time
    - 2 occurs 10.0% of the time
    - 3 occurs 30.0% of the time
    - 4 occurs 10.0% of the time
    - 5 occurs 10.0% of the time

### Sample Output #2:

- **Input:**
  - What’s the highest number you want to generate?: 3
  - How long of a number sequence do you want to generate?: 7

- **Output:**
  - Okay, we’ll generate 7 number(s) ranging from 1 to 3!
  - Sequence: 1, 1, 3, 1, 3, 1, 3
  - **Frequency:**
    - 1 occurs 57.14% of the time
    - 2 occurs 0.0% of the time
    - 3 occurs 42.85% of the time
Transcribed Image Text:**Assignment 5A: Multiple Frequencies** In the last assignment, we calculated the frequency of a coin flip, which required us to have two separate variables to record the number of heads and tails. Now, with our understanding of 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 specify the length of a number sequence to be generated using that range. You will then generate and save the sequence in an array. After that, count the occurrences of each number in the sequence and print the frequency of each number. **Hints:** Multiple arrays can be used for this assignment. One array could hold the number sequence, and another could track the frequencies of each number. ### Sample Output #1: - **Input:** - What’s the highest number you want to generate?: 5 - How long of a number sequence do you want to generate?: 10 - **Output:** - Okay, we’ll generate 10 number(s) ranging from 1 to 5! - Sequence: 1, 3, 1, 3, 2, 4, 5, 1, 1 - **Frequency:** - 1 occurs 40.0% of the time - 2 occurs 10.0% of the time - 3 occurs 30.0% of the time - 4 occurs 10.0% of the time - 5 occurs 10.0% of the time ### Sample Output #2: - **Input:** - What’s the highest number you want to generate?: 3 - How long of a number sequence do you want to generate?: 7 - **Output:** - Okay, we’ll generate 7 number(s) ranging from 1 to 3! - Sequence: 1, 1, 3, 1, 3, 1, 3 - **Frequency:** - 1 occurs 57.14% of the time - 2 occurs 0.0% of the time - 3 occurs 42.85% of the time
Expert Solution
Program

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);
  }
 }
}

steps

Step by step

Solved in 4 steps with 3 images

Blurred answer
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education