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
choli [55]
3 years ago
5

You are given a C program "q2.c" as below. This program is used to calculate the average word length for a sentence (a string in

a single line): Enter a sentence: It was deja vu all over again. Average word length: 3.42 For simplicity, the program considers a punctuation mark to be part of the word to which it is attached. And it displays the average word length to two decimal places.
Engineering
1 answer:
MrMuchimi3 years ago
5 0

Answer:

  1. #include <stdio.h>
  2. #include <string.h>
  3. int main()
  4. {
  5.    char sentence[100];
  6.    int i;
  7.    int wordCount = 1;
  8.    int charCount = 0;
  9.    float averageLength;
  10.    
  11.    printf("Enter a sentence: ");
  12.    gets(sentence);
  13.    
  14.    for(i = 0; i < strlen(sentence); i++){
  15.        if(sentence[i] != ' '){
  16.            charCount++;
  17.        }else{
  18.            wordCount++;
  19.        }    
  20.    }
  21.    
  22.    averageLength = (float)charCount / wordCount;
  23.    printf("Average word length: %.2f", averageLength);
  24.    
  25.    return 0;
  26. }

Explanation:

Firstly we need to import the string.h library  as we need to use strlen method to estimate the length of the input string (Line 2).

To estimate the average word length for an input sentence, we can calculate the total of characters in the sentence and then divide it by the total number of words. To do so, we first get the input sentence from the user (Line 11-12). Next, use a for loop to traverse through the sentence character by character and check if the character is not a space ' ', increment charCount by one. Whenever there is a space, this mark an end of a word and therefore increment wordCount by one (Line 18).

After the loop, we can calculate the average word length by dividing the charCount by wordCount and print the output to two decimal places (Line 22- 23).  

You might be interested in
Calculate how large a mass would be necessary to obtain a mechanical noise limit of [Equation] = 1 nG, 1 µG, and 1 mG if the mec
olga55 [171]

Answer:

Mechanical resonance frequency is the frequency of a system to react sharply when the frequency of oscillation is equal to its resonant frequency (natural frequency).

The physical dimension of the silicon is 10kg

Explanation:

Using the formular, Force, F = 1/2π√k/m

At resonance, spring constant, k = mw² ( where w = 2πf), when spring constant, k = centripetal force ( F = mw²r).

Hence, F = 1/2π√mw²/m = f ( f = frequency)

∴ f = F = mg, taking g = 9.8 m/s²

100 Hz = 9.8 m/s² X m

m = 100/9.8 = 10.2kg

6 0
3 years ago
What does snow fall from?
Klio2033 [76]

Answer:

Clouds

Explanation:

It is created by trapped dust and water.

4 0
3 years ago
Are front-end engineers starting to decline in China?
laiz [17]

Yes. They are declining in China. Very fast

7 0
3 years ago
Which best explains Susan B Anthony purpose in her speech “Women’s rights to the suffrage”
Furkat [3]

Answer:

She wrote and delivered a speech in 1873, which came to be known as the “Women's Rights to the Suffrage” speech. In her address, she lets the audience know of her “crime” of voting. She reminds the listener that the Constitution of the United States says “we the people” and does not exclude women as people

6 0
3 years ago
Read 2 more answers
The increasing interconnections of peoples and countries around the world is known as ________.
Irina18 [472]
I think it’s rationalization.
Hope this helps
4 0
3 years ago
Read 2 more answers
Other questions:
  • If there are 16 signal combinations (states) and a baud rate (number of signals/second) of 8000/second, how many bps could I sen
    7·1 answer
  • What are the challenges posed by strategic information systems, and how should they be addressed?
    10·1 answer
  • Air enters a horizontal, constant-diameter heating duct operating at steady state at 290 K, 1 bar, with a volumetric flow rate o
    15·2 answers
  • If you were choosing between two strain gauges, one which has a single resistor in a bridge that varies and one that has two res
    11·1 answer
  • Convert mechanical energy into electric energy. What can he use?
    13·1 answer
  • A 3-phase induction motor is being driven at a frequency of 80 Hz, and the motor speed is 1000 rpm. How many poles does the moto
    8·1 answer
  • The temperature of a gas stream is to be measured by a thermocouple whose junction can be approximated as a 1.2-mm-diameter sphe
    7·1 answer
  • An inventor claims to have developed a heat engine that produces work at 10 kW, while absorbing heat at 10 kW. Evaluate such a c
    12·1 answer
  • *100 POINTS
    6·2 answers
  • Think about the KIA factory shown in the video, what are two things that managers could do to reduce waste or increase efficienc
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!