hi all..
ive got a C++ assignment to do.

this is what it says:

Write a program in C++ that takes input from a text file specified by the user and outputs the following to the screen:
• Number of single letter words.
• The average word length for the file.
• Number of words whose word length is below the average word length for the file (small words)
• Number of words whose word length is above the average word length for the file (big words)
• The actual single letter words found (do not print duplicates, print 10 to a line)
• The actual small words found (do not print duplicates, one per line)
• The actual big words found (do not print duplicates, one per line)


A word is a series of consecutive alphanumeric characters delimited by space(s), tabs, the newline character or any other "whitespace" character. The words can also be delimited by the usual punctuation characters like “.”, “,”, “?”, “;”, “:”, .. etc.

In calculating the average word length, single letter words do not contribute to the average. If the average calculation produces a number which is not a whole number (i.e. a floating point number), truncate the number to get a whole number.

The only class that you do not have to write for this assignment is the string class from #include <string>. You may use iostream and file handling classes and objects in C++.

You will need to write a vector class. There will be at least 3 vectors (objects) used in your program (one for the single letter words, one for small words and one for big words) but only one vector class. You may write any other classes that you may need and you need to justify each one.

The vector class that you write should be general enough to be used in other contexts and both methods and attributes should permit this. The class would enable the storage of a list of items and provide access to those items.

is there anyone who could help.
thankss very much. this is due on 7th July .

Dani AI

Generated

Brief, practical plan (builds on ’s tokenization example and ’s pointer to similar threads):

Treat a "word" as any maximal run of ASCII alphanumeric characters (use isalnum() while scanning). That handles letters and digits consistently; note this is an assignment ambiguity — decide whether a single digit counts as a "single letter word" and document that choice in a header comment. Compute the average word length by summing lengths of words with length > 1 and dividing by their count; truncate the floating result to an integer (cast to int or use floor for positives). Classify words as small when length < truncated_average, big when length > truncated_average; length == average is neither. Always normalize (e.g., convert to lowercase) before deduplication so "Apple" and "apple" are treated the same.

Recommended processing steps (linear pass, minimal memory churn):

  • Read file char-by-char, build a token while isalnum(c) is true; on break, if token nonempty process it.
  • Maintain three custom Vector<string> objects (single, small, big). For each token: determine length, update totals for average, then insert into the appropriate vector only if not already present (use a contains() helper to avoid duplicates).
  • After the file is finished compute truncated average, then (if needed) re-scan stored non-single tokens to move any misclassified item if the average changed.
  • Print single-letter words ten per line; print small and big words one-per-line.

Vector class interface (keep it generic; implement dynamic growth and a contains method):

template<typename T>
class Vector {
public:
  Vector();
  ~Vector();
  void push_back(const T& item);
  bool contains(const T& item) const;
  T& operator[](size_t idx);
  size_t size() const;
  void clear();
private:
  T* data_;
  size_t size_, capacity_;
  void grow();
};

Test with edge cases: empty file, file with only punctuation, only single-letter tokens (avoid divide-by-zero), hyphenated words (treated as two words), mixed case duplicates. Document all assumptions clearly at the top of the source — graders value clear, correct assumptions and a compact, well-tested Vector implementation.

Recommended Answers

All 3 Replies

>is there anyone who could help.
thankss very much. this is due on 7th July .

Seems like someone is thinking that we will do it for him.

Could you maybe first post what you have so far?

Edit:: If you had searched the forums before posting your homework assignment, then you would have found a thread (a recent one) which addresses a very similar problem:
http://www.daniweb.com/forums/thread198519.html :)

I found this snippit a while ago that isolates all words using a vector

not my code:

#include <iostream>
#include <vector>
#include <string>
using namespace std;

// Returns true if text contains ch
inline bool TextContains(char *text, char ch) {
	while ( *text ) {
		if ( *text++ == ch )
			return true;
	}

	return false;
}

void Split(char *text, char *delims, vector<string> &words) {
	int beg;
	for (int i = 0; text[i]; ++i) {

		// While the current char is a delimiter, inc i
		while ( text[i] && TextContains(delims, text[i]) )
			++i;

		// Beginning of word
		beg = i;

		// Continue until end of word is reached
		while ( text[i] && !TextContains(delims, text[i]) )
			++i;

		// Add word to vector
		words.push_back( string(&text[beg], &text[i]) );
	}
}

int main() {
	vector<string> words;

	// Split up words
	Split( "Hello, my name is william!", " ,!", words );

	// Display each word on a new line
	for (size_t i = 0; i < words.size(); ++i)
		cout << words[i] << '\n';

	cin.ignore();
}

I found this snippit a while ago that isolates all words using a vector

not my code:

#include <iostream>
#include <vector>
#include <string>
using namespace std;

// Returns true if text contains ch
inline bool TextContains(char *text, char ch) {
	while ( *text ) {
		if ( *text++ == ch )
			return true;
	}

	return false;
}

void Split(char *text, char *delims, vector<string> &words) {
	int beg;
	for (int i = 0; text[i]; ++i) {

		// While the current char is a delimiter, inc i
		while ( text[i] && TextContains(delims, text[i]) )
			++i;

		// Beginning of word
		beg = i;

		// Continue until end of word is reached
		while ( text[i] && !TextContains(delims, text[i]) )
			++i;

		// Add word to vector
		words.push_back( string(&text[beg], &text[i]) );
	}
}

int main() {
	vector<string> words;

	// Split up words
	Split( "Hello, my name is william!", " ,!", words );

	// Display each word on a new line
	for (size_t i = 0; i < words.size(); ++i)
		cout << words[i] << '\n';

	cin.ignore();
}

Hey, you don't have to copy-paste the whole code, please just provide a link instead: http://www.daniweb.com/code/snippet1068.html :P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.