31 32 33 34 35 36 37 38 39 40 41 42) 43 } // Output the words and frequencies for (i = 0; i

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter5: Repetition Statements
Section: Chapter Questions
Problem 7PP
icon
Related questions
Question

The output is actually missing one hi -2 at the end before mark - 1

 

**Section 5.20 - CIS 161: Introduction to C Programming - LAB: Word Frequencies**

### Objective:
The aim of this lab is to write a C program that reads a series of words and outputs the frequency of each word.

### Instructions:
- Develop your program and test it as much as you need. 
- Submit the final version before the deadline.

### Code Details:
A skeleton code is provided with the following functionality:
- Read input string.
- Count and store frequencies of each word.
- Print the words and their respective frequencies.

```c
#include <stdio.h>
#include <string.h>
#define MAX_WORDS 50
#define MAX_WORD_LENGTH 50

int main() {
    char words[MAX_WORDS][MAX_WORD_LENGTH];
    int frequencies[MAX_WORDS];
    int numWords = 0;
    char input[200];

    fgets(input, 200, stdin);

    // Split the input string into words
    char *token = strtok(input, " \n");
    while (token != NULL) {
        int found = 0;
        for (int i = 0; i < numWords; i++) {
            if (strcmp(words[i], token) == 0) {
                frequencies[i]++;
                found = 1;
                break;
            }
        }
        if (!found) {
            strcpy(words[numWords], token);
            frequencies[numWords] = 1;
            numWords++;
        }
        token = strtok(NULL, " \n");
    }

    // Output the words and frequencies
    for (int i = 0; i < numWords; i++) {
        if (frequencies[i] != 0) {
            printf("%s - %d\n", words[i], frequencies[i]);
        }
    }

    return 0;
}
```

### User Interface:
- **Develop mode:** Allows you to write and test your code.
- **Submit mode:** Submit the final version of your code for grading.
  
### Input and Output:
1. **Enter Program Input (optional):**
   - Example: `5 hey hi Mark hi mark`

2. **Run Program:**
   - Click to execute the code and observe the program's output.

3. **Program Output Displayed Here:**
   - The area where the result of running the program will be displayed.
  
### Example:
#### Input:
```
5 hey hi Mark hi mark
```
Transcribed Image Text:**Section 5.20 - CIS 161: Introduction to C Programming - LAB: Word Frequencies** ### Objective: The aim of this lab is to write a C program that reads a series of words and outputs the frequency of each word. ### Instructions: - Develop your program and test it as much as you need. - Submit the final version before the deadline. ### Code Details: A skeleton code is provided with the following functionality: - Read input string. - Count and store frequencies of each word. - Print the words and their respective frequencies. ```c #include <stdio.h> #include <string.h> #define MAX_WORDS 50 #define MAX_WORD_LENGTH 50 int main() { char words[MAX_WORDS][MAX_WORD_LENGTH]; int frequencies[MAX_WORDS]; int numWords = 0; char input[200]; fgets(input, 200, stdin); // Split the input string into words char *token = strtok(input, " \n"); while (token != NULL) { int found = 0; for (int i = 0; i < numWords; i++) { if (strcmp(words[i], token) == 0) { frequencies[i]++; found = 1; break; } } if (!found) { strcpy(words[numWords], token); frequencies[numWords] = 1; numWords++; } token = strtok(NULL, " \n"); } // Output the words and frequencies for (int i = 0; i < numWords; i++) { if (frequencies[i] != 0) { printf("%s - %d\n", words[i], frequencies[i]); } } return 0; } ``` ### User Interface: - **Develop mode:** Allows you to write and test your code. - **Submit mode:** Submit the final version of your code for grading. ### Input and Output: 1. **Enter Program Input (optional):** - Example: `5 hey hi Mark hi mark` 2. **Run Program:** - Click to execute the code and observe the program's output. 3. **Program Output Displayed Here:** - The area where the result of running the program will be displayed. ### Example: #### Input: ``` 5 hey hi Mark hi mark ```
Expert Solution
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Binary numbers
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT