79.3k views
4 votes
7.14 LAB: Word frequencies Write a program that reads a list of words. Then, the program outputs those words and their frequencies. The input begins with an 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.

User Ecjb
by
2.9k points

2 Answers

2 votes

Final answer:

Word frequencies can be determined using two arrays; one for storing unique words and another for their corresponding frequencies. The program increases the frequency count each time a word reoccurs in the list.

Step-by-step explanation:

Understanding Word Frequencies

To solve a word frequency problem, we should consider creating two arrays: one to store the unique words, and another to store the corresponding frequencies of those words. As the program reads each word, it will adjust the frequency of the word in the frequency array.

Let's say we are given a set of words: the program will firstly read the number of words to expect. It then reads each word, checks if the word is already present in the words array. If it's a new word, it adds the word to the array and sets its frequency to 1. If the word is already present, it increments the frequency of that word.

To demonstrate the concept with actual numbers, let's take frequency data from a group of people and how many hours they work per day, as an example. If we have responses like 2, 3, 3, 5, the corresponding frequencies are 1, 2, 1, since 2 and 5 appear once, and 3 appears twice. This data organization helps us establish clear patterns and answer related statistical questions.

User Crashmstr
by
3.1k points
0 votes

Final answer:

To solve this problem, you will need to use two arrays: one for storing the words and another one for storing the frequencies of each word.

Step-by-step explanation:

To solve this problem, you will need to use two arrays: one for storing the words and another one for storing the frequencies of each word. Here is an example implementation in Python:

num_words = int(input())
words = []
frequencies = []

for _ in range(num_words):
word = input()
if word in words:
index = words.index(word)
frequencies[index] += 1
else:
words.append(word)
frequencies.append(1)

for i in range(len(words)):
print(words[i], frequencies[i])

User Gkb
by
3.5k points