By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,149 Members | 1,064 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,149 IT Pros & Developers. It's quick & easy.

C++ Giants .. Please help me complete this

NewYorker
P: 13
Hello brothers and sisters,
Please help me complete this program and get the output shown below.
Here is all I have

__________________________________________________ _________

This homework involves reading sentences from a file and determining the makeup of the sentences, words and characters.

1. First, you will read in the sentences with the function
void readSentences(string sentences[], int &numSentences)
This will read in the sentences from a file named Sentences.txt (see below for the input)
Each line ends with a blank space after the last letter. This is necessary to make sure you get all the words correctly.


The function will fill up an array named sentences and set a variable named numSentences (which is passed by reference), which will be used in later functions as well. The function will also print out each sentence as it is read in.

2. The next line in main calls the function countNumWords(string sentences[], int numSentences)
which will count how many words there are in all the sentences. Remember that there is a blank space after each word.

3. Once the sentences are in the array sentences, call the function
countAlphaChars(string sentences[], int numSentences, int letters[])
which will go through each sentence, character by character, find where each letter is in the global string alphabet and use that position as the index for the array letters which which then be increased by 1. Thus, letters[0] represents the letter A. Whenever an A is encountered in a sentence, letters[0] is increased by 1. We use an index of 0 because in the string alphabet, the letter A is in position 0.

4. Once the array letters has been populated, the function printLetters(int letters[]) will be called to print each letter and how many there were in all the sentences. Remember that letter [i] corresponds to alphabet[i] so you can first print the letter and then the count in the same loop. Skip any letters that have a count of 0.

5. Next the function
extractWords(int wordCount[], string words[], string sentences[],
int numSentences, int &wordsInList)
is called. This function goes through the sentences, extracts the words (use the substr string function)one at a time. When a word is extracted, the array words is checked to see if the word is in the array already. This can be done by looping through the array and comparing word[i] with the extracted word to see if they are equal. The number of words in the list is stored in wordsInList which is passed by reference to the function and initialized to 0. If there is no match, then set words[wordsInList] to that word, set wordCount[wordsInList] to 1 and finally increase wordsInList by 1. If the word is found in words[i] then just increase wordCount[i] by 1.

6. Finally, the function printWords(string words[], int wordCount[], int wordsInList)
is called to print the words stored in the array words and how many there are of each word from the array wordCount. The variable wordsInList has the actual number of words stored.

__________________________________________________ ______________

Expand|Select|Wrap|Line Numbers
  1.  PROGRAM OUTLINE: 
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. using namespace std;
  6.  
  7. void readSentences(string[], int &);
  8. void countAlphaChars(string[], int, int[]);
  9. int countNumWords(string[], int);
  10. void extractWords( int[], string[], string[], int, int &);
  11. void printWords(string[], int [], int);
  12. void printLetters(int[]);
  13.  
  14.  
  15. ifstream infile;
  16. string alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  17.  
  18. int main()
  19. {
  20. string sentences[10];
  21. int numSentences=0;
  22. int wordsInList=0;
  23. int letters[52] = {0}; //quick way to initialize the entire array to 0
  24. int wordCount[100] = {0};
  25. string words[100];
  26.  
  27. infile.open("C:\\Sentences.txt");
  28. readSentences(sentences, numSentences);
  29. cout<<"The number of words is "<<countNumWords(sentences,numSentences)<<endl<<endl;
  30.  
  31. countAlphaChars(sentences, numSentences, letters);
  32. printLetters(letters);
  33.  
  34.  
  35. extractWords(wordCount, words, sentences, numSentences, wordsInList);
  36. printWords(words, wordCount, wordsInList);
  37.  
  38. infile.close();
  39. system ("pause");
  40. return 0;
  41. }
  42.  
  43. void readSentences(string sentences[], int &numSentences){
  44. }
  45.  
  46. void printLetters(int letters[]){
  47. }
  48.  
  49. int countNumWords(string sentences[], int numSentences, char c){
  50. }
  51.  
  52. void countAlphaChars(string sentences[], int numSentences, int letters[]){
  53. }
  54.  
  55. void extractWords(int wordCount[], string words[], string sentences[], int numSentences, int &wordsInList){
  56. }
  57.  
  58. void printWords(string words[], int wordCount[], int wordsInList){
  59. }
  60.  
  61.  
__________________________________________________ _____________

INPUT:
Sentences.txt
The number of words in a sentence is to be counted
Each sentence ends with a period
Each word is followed by a space or period

__________________________________________________ ____________

OUTPUT:
sentence 0: The number of words in a sentence is to be counted
sentence 1: Each sentence ends with a blank space
sentence 2: Each word is followed by a one space

There are 3 sentences

The number of words is 27

Letter Count
E 2
T 1
a 8
b 4
c 7
d 5
e 15
f 2
h 4
i 4
k 1
l 3
m 1
n 10
o 8
p 2
r 3
s 8
t 5
u 2
w 4
y 1
Word Count
The 1
number 1
of 1
words 1
in 1
a 3
sentence 2
is 2
to 1
be 1
counted 1
Each 2
ends 1
with 1
blank 1
space 2
word 1
followed 1
by 1
one 1
Dec 9 '06 #1
Share this Question
Share on Google+
3 Replies


DeMan
100+
P: 1,806
Please enlighten us as to what the output you are currently getting, or what error messages the compiler reports.......
Dec 10 '06 #2

NewYorker
P: 13
Please enlighten us as to what the output you are currently getting, or what error messages the compiler reports.......

Thank you .. All you see above is given .. He started the main and gave us the fuction prototypes and he wants us to use these 6 functions to obtain the output shown .. Can you help me start this ?
Dec 10 '06 #3

DeMan
100+
P: 1,806
1) ifstream has a function geline() which returns teh next line in the stream (and infile is globally visible) so you can call infile.getline(); giving something like:
Expand|Select|Wrap|Line Numbers
  1.  
  2. int counter=0;
  3. string temp=infile.getline());
  4. while(temp !=null){
  5.   cout<<temp;
  6.   sentenes[counter]=temp;
  7.   counter++;
  8.   if(counter>10) //You may like to make this a global variable of how big you set your array)
  9.   {
  10.     cout<<"Error, array not big enough to store contents of file";
  11.     break;
  12.   }
  13. }
  14.  
Now that we are over iour fear of coding, I will explain in plain english the process involved in the other sections....

2) Iterate through your array, testing how many spaces.
for each sentence while we are not at the end of the line, if this character is a space increment counter. (A puritan may add that we have not considered the case here that someone has multipkle spaces between words, but that is a relatively trivial modification - set up a flag that is set depending on whether the current char is a space or not, and check against it when we find a space).

3)There are many approaches here. The simplest is given as a code excerpt below (I leave it to you to decide exactly how to use this code):
Expand|Select|Wrap|Line Numbers
  1.  
  2. if((currentChar>='a'&&currentChar<='z'))
  3. {
  4.   letters[currentChar-'a'+26]++;
  5. }
  6. else if((currentChar>='A'&&currentChar<='Z')){
  7.   letter[currentChar-'A']++;
  8. }
  9.  
4) Just a matter of iterating through an array and a String at the same time, printing the character from the String and the integer from the array if the integer != 0. (probably simplest to print all of them in the first instance, and then refine your code to remove those that =0)

5) Follow the exact instruction here, because the method is described fairly detailed.

6) This is very similar to 4.
Dec 10 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.