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
solmaris [256]
2 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]2 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
5.11 Of these two types of programs:a. I/O-boundb. CPU-boundwhich is more likely to have voluntary context switches, and which i
Leno4ka [110]

Answer:

The answer is "both voluntary and non-voluntary context switch".

Explanation:

The description to this question can be described as follows:

Whenever processing requires resource for participant contextual switch, it is used if it is more in the situation of I/O tied. In which semi-voluntary background change can be used when time slice ends or even when processes of greater priority enter.

  • In option a, It requires voluntary context switches in I /O bound.
  • In option b, it requires a non-voluntary context switch for CPU bound.
7 0
2 years ago
In a transaction-processing system (tps), if the tps database can be queried and updated while the transaction is taking place,
Alona [7]
This is called Real time processing. A Transaction Processing System (TPS) is a kind of data framework that gathers, stores, adjusts and recovers the information exchanges of an undertaking. Exchange handling frameworks additionally endeavor to give unsurprising reaction times to demands, despite the fact that this is not as basic with respect to continuous frameworks.
6 0
2 years ago
You have installed a device at the physical layer. All of the systems you connect to this device will talk to each other at the
9966 [12]

Answer: Hub

Explanation:

Hub is a electronic and networking device that is placed in a network to maintain a common link with the multiple attached devices. Hub is situated at the physical layer of the Open systems interconnection(OSI) model.

  • Hub is used for creation of link between numerous local area network sections as it persist various ports which replicates data packet and distributes to other ports.
  • There are different types of hub for different purpose of connection such as USB hub, Ethernet hub etc.  

8 0
2 years ago
The technique where a system clock generates interrupts, and at each clock interrupt the OS regains control and assigns the proc
Misha Larkins [42]

Answer: Time slicing

Explanation:

 Time slicing is one of the technique that basically implement the multitasking in the operating system. It basically generate the interrupt and at the each clock of the interrupt in the operating system basically regain  control and then assign that particular system or processor to the another user.

Time slice are typically work in milliseconds. By using the multi users system, the time slice is the total amount of the processing time for the each user get assigned.

7 0
2 years ago
While in an interactive nslookup session, you'd use the ______ keyword to change the DNS server you're using
serious [3.7K]
Hostname is assigned to a device that would be connected to the computer network and thus would help in identifying the form of electronic communication. While in an interactive nslookup session, one would use the DMNNAMSVR keyword to change the DNS server one is using.
4 0
2 years ago
Read 2 more answers
Other questions:
  • HELP ASAP U GET BRAINLIEST
    15·2 answers
  • ___________ device that uses a light source to read characters, marks, and codes and then converts them into digital data that a
    11·1 answer
  • Which term describes a process by which malicious code can enter from a non-secure network, and make a hairpin, or sharp turn, a
    6·1 answer
  • Static packet filtering firewalls are limited to ________. inspecting packets for which there are good application proxy filteri
    8·1 answer
  • This program finds the sum and average of three numbers. What are the proper codes for Lines a and b?
    6·2 answers
  • Give a recursive algorithm to compute the sum of the cubes of the first n positive integers. The input to the algorithm is a pos
    5·1 answer
  • Consider the following two code segments, which are both intended to determine the longest of the three strings "pea", "pear", a
    7·1 answer
  • Please can someone help me answer this question.
    11·1 answer
  • In Python, parentheses are used in calculations where the order of operations affects the outcome. (5 points)
    9·1 answer
  • Ali has created a small program in Python, but he wants to store his data in a multi-dimensional array. He would like to use adv
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!