473,322 Members | 1,494 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

C++ Giants .. Please help me complete this

NewYorker
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
3 1941
DeMan
1,806 1GB
Please enlighten us as to what the output you are currently getting, or what error messages the compiler reports.......
Dec 10 '06 #2
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
1,806 1GB
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

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

Similar topics

4
by: Dennis M. Marks | last post by:
I have multiple functions that dynamically build parts of a page. It can take 15-30 seconds for this process to complete. In IE nothing appears until the page is complete. In Netscape parts of the...
21
by: Applebrownbetty | last post by:
Hi, I've run into a problem with IE 6 vs. Mozilla when displaying a completely CSS positioned page, and was wondering if any resident CSS guru might quickly be able to find the problem(s). Thank...
4
by: Gary Hughes | last post by:
Hi all, sometime I posted a problem in here where I was getting the following error from the linker in VS C++ 2003. Linking... GCClass.obj : error LNK2022: metadata operation failed (80131188)...
2
by: Frawls | last post by:
Hi Can any one give me some help with this problem please? Here is the scenario: A user submits page 1 which is a web form.
1
by: Esteban Felipe | last post by:
Hi, thanks for reading. I hope to find some help here before I commit suicide because this is driving me crazy. Please excuse me if this looks like a long post, but I hope that a complete...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
1
by: htpc_2006 | last post by:
Hi everyone, i have a vb.net program ive converted most of the code but i know events dont translate well, the original vb.net code uses withevents and .handles and i can't get the events in c#...
0
by: Lillian | last post by:
Dear all: We are researchers from Shih Hsin University and we are currently launching the 2006 New Energy and Environmental Media Issues Survey, this is a non-profit academic research. In this...
11
by: James Kanze | last post by:
On May 30, 8:02 pm, kwikius <a...@servocomm.freeserve.co.ukwrote: The duration type is pretty much a simple example of a classical "units" type. The first time I saw something along...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.