1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
solmaris [256]
3 years ago
5

Write a program that reads a list of words. Then, the program outputs those words and their frequencies. The input begins with a

n integer indicating the number of words that follow. Assume that the list will always contain fewer than 20 words.
Ex: If the input is:
5 hey hi Mark hi mark
the output is:
hey 1
hi 2
Mark 1
hi 2
mark 1
Hint: Use two arrays, one array for the strings and one array for the frequencies.

Computers and Technology
1 answer:
katrin2010 [14]3 years ago
4 0

Answer:

Here is the JAVA program:

import java.util.Scanner;   //to accept input from user

public class Main {  //class name

  public static void main(String[] args) {  //start of main method

      Scanner input = new Scanner(System.in);  //creates Scanner object

      int integer = input.nextInt();  //declares and reads the integer indicating the number of words

      String stringArray[] = new String[integer]; //creates array to hold strings

      for (int i = 0; i < integer; i++) {  //iterates through the array

          stringArray[i] = input.next();         }  //reads strings

      int frequency[] = new int[integer];  //creates array for frequencies

      int count;         //declares variable to count frequency of each word

      for (int i = 0; i < frequency.length; i++) {  //iterates through the frequency array

          count = 0;  //initializes count to 0

          for (int j = 0; j < frequency.length; j++) {  //iterates through array

              if (stringArray[i].equals(stringArray[j])) {  //if element at ith index of stringArray is equal to element at jth index of stringArray

                  count++;                 }            }  //adds 1 to the count

          frequency[i] = count;      }  //adds count to ith index of frequency array

      for (int i = 0; i < stringArray.length; i++) {  //iterates through the array

          System.out.println(stringArray[i] + " " + frequency[i]);         }    }    } //displays each word in stringArray and its corresponding frequency in frequency array

Explanation:

I will explain the program with an example:

let integer = 3

for (int i = 0; i < integer; i++) this loop is used to read input strings into stringArray.

At first iteration:

i = 0

0<3

stringArray[i] = input.next(); this reads a word and stores it to ith index of stringArray. Lets say user enters the string "hey" so hey is stored in stringArray[0]

i++ becomes i = 1

At second iteration:

i = 1

1<3

stringArray[i] = input.next(); this reads a word and stores it to ith index of stringArray. Lets say user enters the string "hi" so hi is stored in stringArray[1]

i++ becomes i = 2

At third iteration:

i = 2

2<3

stringArray[i] = input.next(); this reads a word and stores it to ith index of stringArray. Lets say user enters the string "hi" so hi is stored in stringArray[2]

i++ becomes i = 3

Now the loop breaks as i<integers evaluates to false.

Next the outer loop for (int i = 0; i < frequency.length; i++)

and inner loop for (int j = 0; j < frequency.length; j++)  iterate through the array and if condition if (stringArray[i].equals(stringArray[j])) checks if any of the words at specified indices is equal. This is used in order to check if any word in the list comes more than once. So if any word occurs more than once in the array, then its count is increased each time and the count value is stored in frequency[] array to store the count/frequency of each word in the stringArray. Since hey occurs once so its count is set to 1 and hi occurs twice so its frequency is set to 2. So the output of the entire program is:

hey 1

hi 2

hi 2

The screenshot of the program along with its output with the given example in question is attached.

You might be interested in
What is the future of web development
Anna35 [415]

Answer:

Creating websites that can execute automated tasks and new programing languages revolving around web development.

Explanation:

7 0
2 years ago
What is a URL (Please explain)?
GalinKa [24]
Definition

URL a.k.a uniform resource locater, is simply the address of a World Wide Webpage.

Sentence example:

"Type a URL into a browser's address bar."
5 0
3 years ago
What is data mining ​
Vesna [10]

Answer:

Data mining is a process of extracting and discovering patterns in large data sets involving methods at the intersection of machine learning, statistics, and database systems.

5 0
3 years ago
Read 2 more answers
What is an RGB value?
jasenka [17]
Color's RGB value indicates its red, green, and blue intensity. Each intensity value is on a scale of 0 to 255, or in hexadecimal from 00 to FF.

RGB values are used in HTML, XHTML, CSS, and other web standards. For more, including visual samples and RGB values, see:
5 0
3 years ago
. The _____________ is the responsibility of the CISO, and is designed to reduce incidence of accidental security breaches by or
irinina [24]

Answer: SETA program

Explanation:

 The SETA program is basically stand for the security, education, training and awareness. It is basically used to reduce the accidental security which is break by the organization member such as employees, vendors and contractors.

The SETA program provided various benefits to the organization as it improve the behavior of the employees.It basically enhance the training and education program by focus and concentrate on the information security.

It can also inform he member of the organization about the report of violating many policies.

7 0
2 years ago
Other questions:
  • Using the flowchart diagram, identifythe decision point of this solution?
    6·1 answer
  • You are configuring two switches of different vendors such that they connect to each other via a single link that will carry mul
    13·1 answer
  • What is the definition of D1-D4?
    14·1 answer
  • What does the binary odometer show about representing large numbers?
    8·1 answer
  • What steps might a company or organization need take in order to keep its digital data secure online?
    8·1 answer
  • In what stage of an algae or fungi life cycle are they able to reproduce spores?
    10·1 answer
  • ___are designed to infect and cause damage to operating system files, computer registry, and other locations on the motherboard
    15·1 answer
  • What do I do if my friend wants me to watch a video at my house?
    8·2 answers
  • Explain the characteristics of a first generation computer​
    8·1 answer
  • What are backup storage devices of computer? Why are they necessary in the computer system?​
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!