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
Socket Programming: (30 points) Use Python TCP socket to implement an application with client-server architecture. In this appli
ololo11 [35]

Answer:

Explanation:

Run the code given in text file following instructions.

Download txt
8 0
3 years ago
Line(s) indicates passing is allowed if there are no oncoming cars.
anygoal [31]
Broken yellow b/c you can’t pass on a double solid yellow
5 0
3 years ago
a stem and leaf display describes two-digit integers between 20 and 80. for one one of the classes displayed, the row appears as
allochka39001 [22]

Answer:

  52, 50, 54, 54, 56

Explanation:

The "stem" in this scenario is the tens digit of the number. Each "leaf" is the ones digit of a distinct number with the given tens digit.

  5 | 20446 represents the numbers 52, 50, 54, 54, 56

8 0
3 years ago
Air enters an adiabatic turbine at 900 K and 1000 kPa. The air exits at 400 K and 100 kPa with a velocity of 30 m/s. Kinetic and
Ivanshal [37]

hooooooooooooooooooooooooooooooooooooooooooooooooooooooe    

3 0
3 years ago
The formula for calculating risk considering risk perception is ?​
s2008m [1.1K]

Answer:

risk = probability x loss

Explanation:

3 0
3 years ago
Other questions:
  • The number of weaving errors in a twenty-foot by ten-foot roll of carpet has a mean of 0.8 What is the probability of observing
    6·1 answer
  • While discussing run-flat tires: Technician A says that some are self-sealing tires and are designed to quickly and permanently
    15·1 answer
  • Cool water at 15°C is throttled from 5(atm) to 1(atm), as in a kitchen faucet. What is the temperature change of the water? What
    7·1 answer
  • The organic acid, ACOOH, reacts reversibly with the alcohol BOH, to form the ester ACOOB according to the stoichiometric equatio
    6·1 answer
  • Air at 27°C and a velocity of 5 m/s passes over the small region As (20 mm × 20 mm) on a large surface, which is maintained at T
    6·1 answer
  • 11. Technicians A and B are discussing
    12·1 answer
  • What is the purpose of O-ring and valve seals in a cylinder head?
    15·1 answer
  • 9. An elevator on a construction site is being operated at rated capacity of 6 tons, and is supported by two standard steel cabl
    7·1 answer
  • What is the gear ratio of the given train
    6·1 answer
  • Since you became discouraged not being able to find a job in the San Diego area, you enlarged the area in which you looked for a
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!