Question

In: Computer Science

Please, write code in c++. Using iostream library Most modern text editors are able to give...

Please, write code in c++. Using iostream library

Most modern text editors are able to give some statistics about the text they are editing. One nice statistic is the average word length in the text. A word is a maximal continuous sequence of letters ('a'-'z', 'A'-'Z'). Words can be separated by spaces, digits, and punctuation marks. The average word length is the sum of all the words' lengths divided by the total number of words.

For example, in the text "This is div2 easy problem". There are 5 words: "This"is"div"easy" and "problem". The sum of the word lengths is 4+2+3+4+7 = 20, so the average word length is 20/5 = 4.

Given a text, return the average word length in it. If there are no words in the text, return 0.0.

Input
The first line will contain the text of length between 0 and 50 characters inclusive. Text will contain only letters ('a'-'z', 'A'-'Z'), digits ('0'-'9'), spaces, and the following punctuation marks: ',', '.', '?', '!', '-'. The end of text will be marked with symbol '#' (see examples for clarification).

Output
Output should contain one number - the average length. The returned value must be accurate to within a relative or absolute value of 10-9.


Samples:

Input Output
1 This is div2 easy problem.# 4.0
2 a bc# 1.5
3 w84lFC1hD2ot2?43 Jnw67Kmt8KhOQn# 2.714285714

Solutions

Expert Solution


#include <string>
#include <vector>
#include <functional>
#include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std;

int calcMin(int * array, int n){
   int min= INT_MAX;
  
   for(int i=0;i<n;i++)
       if((array[i] != -1) && min > array[i])
       min= array[i];

   return min == INT_MAX ? -1 : min;
}
int findMin(const string& s, int q){
   int a = s.find(' ',q);
   int b = s.find(',',q);
   int c = s.find('.',q);
   int d = s.find('?',q);
   int e = s.find('!',q);
   int f = s.find('-',q);
   int g = s.find('0',q);
   int h = s.find('1',q);
   int i = s.find('2',q);
   int j = s.find('3',q);
   int k = s.find('4',q);
   int l = s.find('5',q);
   int m = s.find('6',q);
   int n = s.find('7',q);
   int o = s.find('8',q);
   int p = s.find('9',q);
   int r = s.find('#',q);
   int array [1000] = {a,b,c,d,e,f, g,h,i,j,k,l,m,n,o,p, r};
   int w = calcMin(array, 17);
   return w;
}

void split(const string& s,
vector<string>& v) {
string::size_type i = 0;
string::size_type j = findMin(s, 0);

while (j != string::npos) {
v.push_back(s.substr(i, j-i));
i = ++j;
// j = s.find('|', j);
   j =   findMin(s, j);

if (j == string::npos)
v.push_back(s.substr(i, s.length()));
}
}

string convertToString(char* a, int size)
{
int i;
string s = "";
for (i = 0; i < size; i++) {
   if(a[i]=='#')
       break;
      
s = s + a[i];
}
return s;
}

int main() {
   vector<string> v;
   char a[50];
   scanf("%[^\n]%*c", a);
string s = convertToString(a,50);
split(s, v);

   float count = 0;
   float sum = 0;
  
for (int i = 0; i < v.size(); ++i) {
    if(v[i].size()){
        count++;
        sum += v[i].size();
   }
}
float result = 0.0;
if(count)
   result = sum/count;
   int f = result;
    if(f== result){
        printf("%.1f", result);
   } else {
       printf("%.9f", result);
   }
  
   return 0;
}


Related Solutions

Please, write code in c++. Using iostream and cstring library. Your friend is the person who...
Please, write code in c++. Using iostream and cstring library. Your friend is the person who does not like any limitations in the life. And when you said to him that it is totally impossible to work with integer numbers bigger than 4 294 967 296 in C++ he blamed you in time-wasting during the university study.So to prove that you hadn't waste 2 months of your life studying C++ in university you have to solve this issue. Your task...
Please write code in c++ using iostream library. Also you can use any string library. Create...
Please write code in c++ using iostream library. Also you can use any string library. Create structure plane with the following: 1)plane's ID[int type] 2) location[char*] 3) departure time[char*] Your task is to find the plane with the smallest departure time for the selected city. Pointer, dynamic array and structures have to be used in this code. Input: NOTE: It's just an example of input, you should write code generally for any inputs. First line contains n(0 < n <...
Please write code in c++. Use iostream (and any string library if you need it). Create...
Please write code in c++. Use iostream (and any string library if you need it). Create s structure plane : First line contains n(0 < n < 1001). Then n lines inputed in given format:   First - ID[int type]   Second - FromLocation[char*]   Third - ToLocation[char*]   Fourth - DepartureTime[char*] Output: Sorted list of planes should be in UPPER CASE. Example of input:(it's just one of an examples, you need to write code generally) 10 40 Shuch Satp 05:47 89 Kyzy Taldy  07:00...
DISCUSS COMPARING TEXT EDITORS IN LINUX. Research the different text editors available in Linux and write...
DISCUSS COMPARING TEXT EDITORS IN LINUX. Research the different text editors available in Linux and write up a comparison of each. Your comparison should include a recommendation as to the editor you feel would be most useful and why.
write this program in c++ using iostream library.( cin>>n; cin>> arr[n] and so on) Write a...
write this program in c++ using iostream library.( cin>>n; cin>> arr[n] and so on) Write a function that converts an array so that in the first half settled with elements from odd positions, and in the second half - with elements from the even positions.Positions are counted from the first index.The program have to use pointer. example: input: 7 1 2 3 4 5 6 7 output: 1 3 5 7 2 4 6 8
Please write variables and program plan(pseudocode) of this C++ programming code: #include <iostream> using namespace std;...
Please write variables and program plan(pseudocode) of this C++ programming code: #include <iostream> using namespace std; void leapYear(int x); int main() { int x; cout << "Enter a year: "; cin >> x; leapYear (x);   return 0; } void leapYear(int x ) {    if (x % 400 == 0)    {    cout << "This is a leap Year";}    else if    ((x % 4 == 0) && (x % 100 != 0))    {    cout <<...
Using C++, write a code that this program always stores text file output into a text...
Using C++, write a code that this program always stores text file output into a text file named "clean.txt". -The program should read one character at a time from "someNumbers.txt", and do the following. -If it is a letter, print that letter to the screen, AND also store it in the text file. All letters should be converted to lowercase beforehand. -If it is a number, print that number to screen, but do NOT store it in the text file....
please submit the C code( no third party library). the C code will create output to...
please submit the C code( no third party library). the C code will create output to a file, and iterate in a loop 60 times and each iteration is 1 second, and if any key from your keyboard is pressed will write a 1 in the file, for every second no key is pressed, will write a 0 into the output file.
Write a C++ program using produces Huffman code for a string of text entered by the...
Write a C++ program using produces Huffman code for a string of text entered by the user. Must accept all ASCII characters.
C++ Code You will write a program to process the lines in a text file using...
C++ Code You will write a program to process the lines in a text file using a linked list and shared pointers. You will create a class “Node” with the following private data attributes: • Line – line from a file (string) • Next (shared pointer to a Node) Put your class definition in a header file and the implementation of the methods in a .cpp file. The header file will be included in your project. If you follow the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT