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 type of information is appropriate for headers and footers? Check all that apply.
lbvjy [14]

Answer:

slide/page number

smart art

maybe date and time

Explanation:

6 0
3 years ago
Read 2 more answers
Windows organises information on computer using a
zhuklara [117]
The correct answer is b folder
6 0
3 years ago
Read 2 more answers
SOMEONE PLEASE HELP ME I REPOSTED THIS 3 time and no ONE HAD HELPED ME
RoseWind [281]

Answer:

The answer is B

7 0
3 years ago
DOES ANYONE KNOW HOW TO CHAGE THE IP ON A COMPUTER?
MaRussiya [10]
Try downloading a VPN

8 0
3 years ago
What is the function of ROM?
Gnoma [55]

Answer:

Hello, There!

<h2>Your Question↓</h2>

What is the function of ROM?

<h2>Answer↓</h2>

Allows parts of the computer to communicate is the Correct Answer.

Explanation:

<h2>Now you May Ask What does it do or How it works↓</h2>
  • ⇒ROM provides the necessary instructions for communication between various hardware components.
  • ⇒ it is essential for the storage and operation of the BIOS, but it can also be used for basic data management, to hold software for basic processes of utilities.

Tip: Wanna know how to earn points back for asking your Question?

If you do it's easy just when two people answer your question whichever answer you think is the best click the crown on the side to get 25% of your points back! :D

Therefore, I hope this helps!

6 0
2 years ago
Other questions:
  • Explain which are of the brain you think might light up to show that you are telling a lie and why
    10·1 answer
  • Which of these jobs would be most appropriate for someone who majors in information technology? managing a database for a large
    6·2 answers
  • ? Assessment
    12·1 answer
  • Why would a store owner want to calculate a mean
    12·1 answer
  • What is the role of computer in modern problem solving​
    13·1 answer
  • Edhesive 2.3 code practice question 1​
    11·1 answer
  • How do we “read” and “write” in MAR and MDR memory unit, please help I am very confused :)
    10·1 answer
  • Name any three areas of of application of excel.
    12·1 answer
  • Role of User Defined Iprms for creation of paten
    8·1 answer
  • What is keyword density?
    15·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!