Question

Exercise 5.10. Write a program that reads strings from stdin and computes the integer mean of their lengths.code help. mine's logic is not working

in C language THANKS!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>
#include <string.h>
int main()
{
char s[100];
int eachStringLength = 0,i;
int totalLength = 0;
int count = 0;
double mean = 0;
printf("Enter the string(bye to quit): ");
scanf("%s", &s);
while (strcmp(s, "bye") != 0){
eachStringLength = 0;
for(i=0; s[i]!='\0' ; i++){
eachStringLength++;
}
totalLength = totalLength + eachStringLength;
count++;
printf("Enter the string(bye to quit): ");
scanf("%s", &s);
}
mean = totalLength/(double)count;
printf("Mean of all strings length: %lf\n", mean);

return 0;
}

Output:

sh-4.2$  gcc -o main *.c                                                                                                                                                                                                                                               

sh-4.2$ main                                                                                                                                                                                                                                                           

Enter the string(bye to quit): suresh                                                                                                                                                                                                                                  

Enter the string(bye to quit): sekhar                                                                                                                                                                                                                                  

Enter the string(bye to quit): anshu                                                                                                                                                                                                                                   

Enter the string(bye to quit): revathi                                                                                                                                                                                                                                 

Enter the string(bye to quit): hi                                                                                                                                                                                                                                      

Enter the string(bye to quit): bye                                                                                                                                                                                                                                     

Mean of all strings length: 5.200000

Add a comment
Know the answer?
Add Answer to:
code help. mine's logic is not working in C language THANKS! Write a program that reads...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • if it is possible help me with the code in C language and I could learn...

    if it is possible help me with the code in C language and I could learn it, thanks ! Write a function that computes the integer mean of an array of integers. For example, the integer mean of -1, 4, 2 is (-1 + 4 + 2)/3 = 5/3 = 1, where/des integer division. The function should implement the following specification:/* Computes the integer mean of an array and stores it * where mn references. Returns -1 for erroneous input...

  • This program is in C language: Write a statement that reads a decimal integer from standard...

    This program is in C language: Write a statement that reads a decimal integer from standard input and stores it into a variable named boxes. -------------------------------- Everything I enter is wrong, can someone help me out.

  • code needs to be written in c language. Write a program that prints the type of...

    code needs to be written in c language. Write a program that prints the type of the integer representation string provided by stdin. The possible types are "Decimal", "Octal", "Hexadecimal", or "Illegal". Example inputs: 118 rightarrow Decimal 074 rightarrow Octal 0xD5 rightarrow Hexadecimal cat rightarrow Illegal You can assume the input is between 1 and 9 characters (inclusive) long.

  • JAVA CODE FOR BEGINNERS!! Write a program that reads three strings from the keyboard. Although the...

    JAVA CODE FOR BEGINNERS!! Write a program that reads three strings from the keyboard. Although the strings are in no particular order, display the string that would be second if they were arranged lexicographically.

  • Programming language is C++. 9. Write a program that reads digits and composes them into integers....

    Programming language is C++. 9. Write a program that reads digits and composes them into integers. For example, 123 is read as the characters 1, 2, and 3. The program should output 123 is 1 hundred and 2 tens and 3 ones. The number should be output as an int value Handle numbers with one, two, three, or four digits. Hint: To get the integer value 5 from the character '5' subtract '0' that is, '5'-'0'

  • Write a program that first reads an integer for the array size, then reads numbers into...

    Write a program that first reads an integer for the array size, then reads numbers into the array, computes their average, and finds out how many numbers are above the average. Make sure to include pointer syntax. Example output: Enter array size: 10 10 random numbers between 1 and 10 generated are: 2 8 5 1 10 5 9 9 3 5 The average is: 5.7 Number of items above the average = 4 C++ Code only.

  • C programming help! Write a program that reads the electricity consumption (in kWh) and computes the...

    C programming help! Write a program that reads the electricity consumption (in kWh) and computes the electricity bill based on the following information: • The first 50 kWh costs 8.5 cents/kWh • Next 100 kWh costs 9.5 cents/kWh • Next 100 kWh costs 9.8 cents/kWh • The consumption above 250 kWh costs 10.25 cents/kWh Additional surcharge 20% is added to the bill. The input of the program is one integer and the output will be “The total bill is $?.”,...

  • Please help and answer in a PSEUDO Code format in assembly language! Thanks PROJECT (a) write...

    Please help and answer in a PSEUDO Code format in assembly language! Thanks PROJECT (a) write a program that will convert the number 23567 => n, where (b) Write an algorithm that will convert a number n, to ne where b , c < 10.

  • write the program with the language NODEJS need help Programming challenge, screenshot your work and output.  ...

    write the program with the language NODEJS need help Programming challenge, screenshot your work and output.   Description A positive integer is a palindrome if its decimal representation (without leading zeros, is a palindromic string (a string that reads the same forwards and backwards). For example, the number 5, 77, 363, 4884, 11111, 12121 and 349943 are palindromes A range of integers is interesting if it contains an even number of palindromes. The range [L, R], with L <= R, is...

  • Write the code in python programming Language String Statistics: Write a program that reads a string...

    Write the code in python programming Language String Statistics: Write a program that reads a string from the user and displays the following information about the string: (a) the length of the string, (b) a histogram detailing the number of occurrences of each vowel in the string (details provided below), (c) the number of times the first character of the string occurs throughout the entire string, (d) the number of times the last character of the string occurs throughout the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT