answer.
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
AlladinOne
1 month 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:
8_murik_8 [892]1 month ago
4 0

Answer:

Below is the JAVA program:

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

public class Main {  //name of the class

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

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

      int integer = input.nextInt();  //defines and collects an integer for the number of words

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

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

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

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

      int count;         //initializes variable to calculate frequency of each word

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

          count = 0;  //sets count to zero

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

              if (stringArray[i].equals(stringArray[j])) {  //checks if element at ith index matches jth index

                  count++;                 }            }  //increments count

          frequency[i] = count;      }  //stores count in the frequency array

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

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

Explanation:

To illustrate the program, consider:

let integer = 3

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

On the first pass:

i = 0

0<3

stringArray[i] = input.next(); takes a word and saves it to the ith index of stringArray. For example, if the user types "hey", it will be in stringArray[0].

Then, i increments to i = 1

On the second pass:

i = 1

1<3

stringArray[i] = input.next(); takes another word and assigns it to stringArray[1]. If the user enters "hi", then hi will be in stringArray[1]

Next, i becomes i = 2

On the third pass:

i = 2

2<3

stringArray[i] = input.next(); captures a word and places it in stringArray[2]. If the user types "hi", it goes to stringArray[2]

Then, i increments to i = 3

The loop terminates since i<integers is false.

The next outer loop for (int i = 0; i < frequency.length; i++) and inner loop for (int j = 0; j < frequency.length; will check each word and if (stringArray[i].equals(stringArray[j])) identifies any duplicates. Words that repeat will have their count incremented and the frequency array will record these values. For example, hey appears once while hi shows up twice, thus resulting in the final outputs:

hey 1

hi 2

hi 2

The visual representation of the program and its outcome based on the example provided is attached.

You might be interested in
You're installing two new hard drives into your network attached storage device. Your director asks that they be put into a RAID
8_murik_8 [892]

Response:

d. RAID 6

Clarification:

RAID is a technological method for data storage that integrates several physical hard drive components into a unified logical structure. Its primary purpose is to ensure both performance and data redundancy.

RAID 0 is focused on data striping, but it lacks redundancy.

RAID 1 enhances performance to nearly double but restricts disk space usage to around 50%.

RAID 5 offers both redundancy and improved performance, though it is constrained by smaller drive sizes.

RAID 6 provides redundancy as well but with a decrease in performance.

RAID 10 boosts both performance and data security.

Hence, RAID 6 is the optimal choice that emphasizes redundancy at the cost of speed.

8 0
1 month ago
How can you check an orthographic drawing to be sure there are no missing lines
maria [879]
Using a magnifying glass.
4 0
21 day ago
The budget process which emphasizes the achievement of goals and competition between alternatives is:
Rzqust [894]

The question offers several choices;


<span>A) </span>Incremental budgeting.
B) Performance budgeting.
C) Program budgeting.
D) Target based budgeting.


I'd argue that D is the correct option; Target based budgeting.


Target based budgeting centers on achieving objectives and competing alternatives. This budgeting approach uses pricing as a strategic tool to influence sales outcomes. It involves market research to determine the precise selling price of a product.






4 0
1 month ago
Allows you to manually add an entry to the arp cache that resolves the ip address inetaddr to the physical address etheraddr. wh
Amiraneli [921]
In this situation, when the Adaptor or router gets the destination IP address (even if an incorrect MAC address is entered), it would strip the IP address from the Ethernet frame and using ARP, it will obtain the accurate MAC address of the destination.
6 0
21 day ago
The function below takes a single string parameter: sentence. Complete the function to return a list of strings that are in the
Rzqust [894]

Response:

#section 1

def listofWordswitha(text):

   ''' This function outputs all words from a string that include the letter 'a' '''

   tex = text.split()

   new = []

#section 2

   for word in tex:

       for char in word:

           if char == 'a':

               new.append(word)

               

               break

   return new

Clarification:

#section 1

First, you need to establish your function and provide an argument representing the string that will be utilized.

It's beneficial to include a docstring that describes the function's purpose, which I've added.

Then, use the split method to break the string into a list. In conclusion, you create a list to store all the words that contain 'a'.

#section 2

The terminology chosen is specific to facilitate understanding.

FOR EVERY WORD inside the list and FOR EACH LETTER in the WORD.

IF a LETTER 'a' is found in the word, ADD that WORD to the NEW LIST.

The append function serves to incorporate new entries into the list.

A break statement is employed to avoid redundancy since some words might have multiple instances of 'a'. Thus, upon encountering a word containing 'a', it appends it and shifts to the next word.

Ultimately, the new list is returned.

I will utilize your inquiry to validate the code and provide the results for you.

4 0
1 month ago
Other questions:
  • Assume that the following variables have been properly declared and initialized.
    12·1 answer
  • Explain how abstraction is used in a GPS system
    12·2 answers
  • Susan is assisting a web designer to create a promotional web page on eco-friendly classroom designs. She uses color combination
    5·1 answer
  • Edhesive 2.3 code practice question 1​
    11·1 answer
  • An Internet user has a need to send private data to another user. Which of the following provides the most security when transmi
    14·2 answers
  • Form the recurrence relations (RRs) for the number of vertices and the number of edges of a hypercube of n dimensions, Hn. Solve
    13·1 answer
  • Which generation of programming languages provides programmers with a visual environment for coding programs?
    12·1 answer
  • Write a loop to populate the list user_guesses with a number of guesses. The variable num_guesses is the number of guesses the u
    13·1 answer
  • A 2-dimensional 3x3 array of ints, has been created and assigned to tictactoe. Write an expression whose value is true if the el
    10·1 answer
  • An application specifies a requirement of 200 GB to host a database and other files. It also specifies that the storage environm
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!