Thread: Exercise 4.1 ("Accelerated C++"): Please help to improve the program

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    32

    Exercise 4.1 ("Accelerated C++"): Please help to improve the program

    Hi,

    I just have done this exercise from "Accelerated C++". Several things I'm not sure about:

    (1) Deal with "times" or "time", i.e. if <= 1, then choose "time", and else choose "times". I tried conditional operator, (a>b)? c : d..., but somehow it gives errors all the time

    (2) Does this program meet the requirements of the exercise (see the verbatim description of the exercise at the top of the program)

    (3) I don't understand much about istream& thing that the textbook uses to read words... Why is it used instead of the usual cin>>sth?

    (4) How can I initialize a vector? I've read and tried all possiblities: vector<int> vec(10, 1) for 10 elements, each = 1. But it doesn't work here somehow. Error warnings immediately. In this program, I've tried
    wp.count(wp.v.size(), 1) to inititiaze every element of the vector count to 1. Errors always!

    (5) Lastly, if possible, please give me a hint/suggestions on how to improve this program.

    Thanks a lot

    Code:
    // Exercise 4.6
    // Textbook: Accelerated C++, by Andrew Koenig and Barbara E.Moo
    /*
    	Write a function that reads words from an input stream and stores them in a vector. 
    	Use that function BOTH to write programs that count the number of words in the input
    	and to count how many times each word occurs
    
    */
    #include <iostream>
    #include <string>
    #include <vector>
    using namespace std;
    
    struct wordplay {
    	int num;
    	vector<int> count;
    	vector<string> v;
    };
    
    // write a function that (1) reads words, (2) stores words in a vector, (3) computes 
    // the number of words in the input, and (4) counts how many times each word occurs
    
    wordplay myf(string s)
    {
    	wordplay wp;
    	while (cin >> s) // reads in words
    		wp.v.push_back(s); // stores in a vector
    	
    	// # of words
    	wp.num = wp.v.size(); 
    
    	// # count how many times each word occurs
    	for (int k = 0; k != wp.v.size(); ++k)
    		wp.count.push_back(1); // initialize each element of the count vector 1
    
    	for (int i = 0; i != wp.v.size(); ++i) {
    		for (int j = 0; j != wp.count.size(); ++j) {
    			if (j != i && wp.v[j] == wp.v[i]) {
    				wp.count[i] += 1;
    			}
    		}
    	}
    	return wp;
    }
    
    int main()
    {
    	cout << "Enter some words: " << endl;
    	string s;
    	wordplay wp = myf(s);
    	cout << "The number of words entered is: " << wp.num << endl << endl;
    	for (int i = 0; i != wp.count.size(); ++i) {
    		cout << "This word, " << "\"" << wp.v[i] << "\"" << " occurs " << wp.count[i] << " times" << endl;
    	}
    	cout << endl << endl;
    
    	return 0;
    }
    Last edited by pantera; 05-02-2010 at 01:18 AM.

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    1) I'd suggest you learn to deal with translation issues now. I wouldn't use an inline ternary operator. You can use an `if'/`else' easily enough.

    2) That's a good question. Does it?

    3) Not everyone will have the textbook on hand. What does it use?

    4) You are over-complicating things; see number two.

    Soma

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    (1) Deal with "times" or "time", i.e. if <= 1, then choose "time", and else choose "times". I tried conditional operator, (a>b)? c : d..., but somehow it gives errors all the time
    That has issues with precedence with surrounding <<s. Put the whole ternary thing in brackets.

    (2) Does this program meet the requirements of the exercise (see the verbatim description of the exercise at the top of the program)
    Doesn't seem to do so.

    Write a function that reads words from an input stream and stores them in a vector. - Your function does much more than that.

    Use that function BOTH to write programs that count the number of words in the input and to count how many times each word occurs - This seems to indicate that you write two programs?

    (3) I don't understand much about istream& thing that the textbook uses to read words... Why is it used instead of the usual cin>>sth?
    std::cin is a global instance of istream. What you are expected to write is a function that might look like this:

    Code:
    std::vector<std::string> read_words(std::istream& is);
    which can be used to read from std::cin, a file or a stringstream

    Code:
    std::vector<std::string> from_console = read_words(std::cin);
    
    std::ifstream fin("input_file.txt");
    std::vector<std::string> from_file = read_words(fin);
    
    std::string s("one two three");
    std::stringstream ss(s);
    std::vector<std::string> from_string = read_words(ss);
    (4) How can I initialize a vector? I've read and tried all possiblities: vector<int> vec(10, 1) for 10 elements, each = 1. But it doesn't work here somehow. Error warnings immediately. In this program, I've tried
    wp.count(wp.v.size(), 1) to inititiaze every element of the vector count to 1. Errors always!
    You can't initialize a vector after it's been already created. You could only initialize it in the constructor of the struct you have.

    But you can resize it:
    Code:
    wp.count.resize(wp.v.size(), 1);
    (5) Lastly, if possible, please give me a hint/suggestions on how to improve this program.
    Do you like the output if words occur more than once?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  3. Interesting little exercise...
    By biterman in forum C Programming
    Replies: 13
    Last Post: 10-18-2001, 01:22 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM