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 most significant issue that needs to be addressed when ensuring the proper functioning of a computer?
ycow [4]

Answer:

The most significant issue addressed while ensuring the proper functionality of computer is the working of Operating System.

Explanation:

There are many issues faced while making the computer properly functional. Such as, Memory size, processor speed, software installation etc. while operating system not working properly can cause a significant problem. If operating system is not working properly, the user is unable to use the computer.

So, to make sure the functioning of computer, the significant issue that should be needed to address is Operating System Installation.

7 0
3 years ago
How to jail break iphone 7 with <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7617023602">[email&#160
GarryVolchara [31]
I do not think so i honestly do not know
6 0
3 years ago
Read 2 more answers
Draw a flowchart for an algorithm which calculates how much money a student will need per week to buy a meal and two drinks each
jasenka [17]

Flowcharts are used as a prototype of an actual program.

First, I will write out the algorithm; which will be used as an explanation of the flowchart (see attachment for flowchart)

The algorithm is as follows;

1. Start

2. Get input for cost of a meal and cost of a drink;

3. Calculate the total cost for a meal and two drinks

4. Multiply the total cost of a meal and two drinks by 7, to get the weekly cost

5. Display the weekly cost

6. Stop

At the end of the algorithm/flowchart, the weekly cost is calculated and printed.

See attachment for flowchart

Read more about algorithms and flowcharts at:

brainly.com/question/18088572

3 0
3 years ago
HELP PLZZZZZZ
Brums [2.3K]

Answer:

Do you want me the money for the policy that has been canceled

3 0
3 years ago
How much can a INIVDA 1050 TI take? (FPS wise)
mafiozo [28]

Answer:

At full graphics (ultra settings, full HD resolution), tests can range from 60-65 fps, 65.5 fps being the max.

Explanation:

You can benchmark graphics cards using in-game stress tests/benchmark tests or using a separate benchmarking software.

NVIDIA 1050 Ti's are now slightly outdated. Nevertheless, it is now significantly cheaper.

3 0
3 years ago
Other questions:
  • How can you tell which of the policies in the security options folder have changed settings?
    10·1 answer
  • What are two reasons people work?
    7·2 answers
  • The _____ feature will mark all changes made to a document for others to review at a later time.
    13·1 answer
  • How many characters should a strong password have? four six eight ten
    10·2 answers
  • Discuss the causes ,symptoms, preventive measures and treatment of AIDS​
    6·2 answers
  • Jobs with only 7 letters
    7·2 answers
  • What is the first step in finding a solution to a problem? Choose a solution. Think of options to solve the problem. Try the sol
    6·1 answer
  • Areas on which the development of the computer as a communication technology is based
    5·1 answer
  • What percentage of teens say they have witnessed cyberbullying?
    6·2 answers
  • While some audio recording programs are expensive, there are more user friendly,
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!