0
reduce() function
what does reduce() function do in C++?
8 Answers
+ 4
Ramisa,
I can't figure out what that reduce() is about as I don't have a copy of the book, and it seems there's not present any info on the implementation.
My guess only, it might be a function that is used to remove whitespace from a string, which results in a word, to be counted for, in order to generate word frequency counter.
But it's only a guess, so you might want to wait for a comment from someone who also have the book, hopefully they can assist you further.
+ 2
Where did you see this function Ramisa, any info from which header it came from? or is it a custom function?
+ 2
@Martin Thank you
+ 1
@Ipang thanks ..I have also searched about it on the internet but didn't get the direct definition about it
0
int main()
{ ifstream in("Pr0907.in");
string s;
const int SIZE=1000; // assume at most 1000 different words
string word[SIZE]; // holds words read
int lines=0,words=0,n=0,freq[SIZE]={0},i;
char c;
while (in >> s)
{ reduce(s);
if (s.length() == 0) continue;
++words;
in.get(c);
if (c == '\n') ++lines; // count line
for (i=0; i<n; i++)
if (word[i] == s) break;
if (i == n) word[n++] = s; // add word to list
++freq[i]; // count word
}
cout << "The input had " << lines << " lines and " << words
<< " words,\nwith the following frequencies:\n";
for (int i=0; i<n; i++)
{ s = word[i];
if (i > 0 && i%3 == 0) cout << endl; // print 3 to a line
cout << setw(16) << setiosflags(ios::right)
<< s.c_str() << ": " << setw(2) << freq[i];
}
cout << endl;
the code taken from a book...after the while loop there is this reduce().
0
the question of this code
9.8
Modify your program from Problem 9.6 so that it counts the frequencies of words instead of
letters. For example, the input
[I] then went to Wm. and Mary college,to wit in the spring of
1760,where I continued 2 years. It was my great good fortune,
and what probably fixed the destinies of my life that Dr. Wm.
Small of Scotland was then professor of Mathematics,a man
profound in most of the useful branches of science,with a happy
talent of communication,correct and gentlemanly manners,& an
enlarged & liberal mind. He,most happily for me,became soon
attached to me & made me his daily companion when not engaged in
the school; and from his conversation I got my first views of the
expansion of science & of the system of things in which we are
placed.
would produce the output
The input had 11 lines and 120 words,
with the following frequencies:
i: 3 then: 2 went: 1
to: 3 wm: 2 and: 4
mary: 1 college: 1 wit: 1
in: 4 the: 6 spring: 1
of: 11 : 6 wher