473,659 Members | 2,690 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C++ Giants .. Please help me complete this

NewYorker
13 New Member
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(s tring sentences[], int &numSentence s)
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(s tring 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(in t 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(in t 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(stri ng 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 1964
DeMan
1,806 Top Contributor
Please enlighten us as to what the output you are currently getting, or what error messages the compiler reports.......
Dec 10 '06 #2
NewYorker
13 New Member
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 Top Contributor
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
3293
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 page appear as built. Is there any way to display a "Please Wait" message that displays as soon as the first javascript begins and disappears when the javascripts have completed? Some scripts are in the header and some in the body. I have...
21
2801
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 you. In IE, the page looks how I want it to look (picture below): www.sunbadgeco.com/sunmetal/ie.jpg In Mozilla Firefox, somehow it's not quite right (pic below): www.sunbadgeco.com/sunmetal/mozillafirefox.jpg
4
3011
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) : Inconsistent field declarations in duplicated types (types: GCClass; fields: m_blah): (0x04000001). LINK : fatal error LNK1215: metadata operation failed (80131130) :
2
3840
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
1451
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 explanation help you to help me :=) ....Let's start with some background: ..- I'm building an asp.net application that requires users to upload text files in cvs format with data exported from an AS-400. ..- The files will be something between 800kb...
1
9623
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 and I was wondering if anyone here would be able to give me some tips for young players such as myself, for learning the language. Is this the best Newsgroup for support with JAVA?
1
1026
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# to work 100% they seem to hook up ok but when the eventhandler is called from the method its null and fails. or maybe im reading it wrong?
0
1119
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 survey, we would like to know your opinions about the topics of adoption and safety of new energy related technology, and your opinion about Kyoto Protocol related media issues. Your answers will produce valuable information for our researchers....
11
224
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 those lines was at least 15 years ago. It's hardly a new idea. (I actually suspect that it's been "invented" independently many times.)
0
8427
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8332
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8746
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7356
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6179
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2750
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.