Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 202
  • Last Modified:

Compute the average length of sentences?

I am not familiar when dealing with file in pascal.The question is how do I construct a program that reads a file containing a love letter or something familiar and computes the avarege length of sentences in it.Sentences are terminated by '.'(full stop).The length of a sentence is defined to be the number of words in it.Words in sentences are separated by at least a ' '(space).

This is how far I can go.I use Pascal 7.0

Program Average;
var
 inputdata:text;
 word:string;
count:integer;
 
begin
Assign(inputdata,'A:Newrec.dat')
reset(inputdata);
while not seekEOF (inputdata) do
begin
count:=count +1;
.....
....
End.

0
ktpule
Asked:
ktpule
  • 2
  • 2
1 Solution
 
dbruntonCommented:


Roughly the code should be

var
 count : integer;
 words : integer;
 character : char;
 fullstops : integer;

count := 0;
fullstops := 0;
words := 0;
while not eof (inputdata) do
begin
read(inputdata, character);
inc(count);
if character = '.' then
 inc(fullstops);
if character = ' ' then
 inc(words);
end;


That'll give you the number of words, number of fullstops (sentences), and number of characters.  And you can do the average calculations from that info.  Code still needs tidying up because there can be strange combos in there but that's enough to get you started.
0
 
ShivshankarCommented:
I'll modify the program given by dbrunton a bit so that if there are multiple spaces between words, it should'nt count more.

var
  count : integer;
  words : integer;
  character : char;
  fullstops : integer;
  prevchar : char;
begin
  count := 0;
  fullstops := 0;
  words := 0;
  prevchar := Chr(0);
  Assign(inputdata,'A:Newrec.dat')
  reset(inputdata);
  while not eof (inputdata) do
  begin
    read(inputdata, character);
    inc(count);
    if character = '.' then
      inc(fullstops);
    if character = ' ' and prevchar <> ' ' then
      inc(words);
    prevchar = character;
  end;

And change the assignment of Words:= 0; to words:= 1;

If not even 1 character is read from inputdata, then decrement words by 1. That should do.

Hope this helps.
0
 
MoondancerCommented:
You have asked 11 questions at this site and NEVER closed/awarded any of them.  ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.

Question(s) below appears to have been abandoned. Your options are:
 
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you. You must tell the participants why you wish to do this, and allow for Expert response.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question. Again, please comment to advise the other participants why you wish to do this.

For special handling needs, please post a zero point question in the link below and include the question QID/link(s) that it regards.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click the Help Desk link on the left for Member Guidelines, Member Agreement and the Question/Answer process.  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Please click you Member Profile to view your question history and keep them all current with updates as the collaboration effort continues, to track all your open and locked questions at this site.  If you are an EE Pro user, use the Power Search option to find them.

To view your open questions, please click the following link(s) and keep them all current with updates.
http://www.experts-exchange.com/questions/Q.20186611.html
http://www.experts-exchange.com/questions/Q.20188338.html
http://www.experts-exchange.com/questions/Q.20253034.html
http://www.experts-exchange.com/questions/Q.20253103.html
http://www.experts-exchange.com/questions/Q.20253032.html
http://www.experts-exchange.com/questions/Q.20254643.html
http://www.experts-exchange.com/questions/Q.20274803.html
http://www.experts-exchange.com/questions/Q.20275020.html
http://www.experts-exchange.com/questions/Q.20275204.html


To view your locked questions, please click the following link(s) and evaluate the proposed answer.
http://www.experts-exchange.com/questions/Q.20194313.html
http://www.experts-exchange.com/questions/Q.20275908.html

PLEASE DO NOT AWARD THE POINTS TO ME.  
 
------------>  EXPERTS:  Please leave any comments regarding your closing recommendations if this item remains inactive another seven (7) days.  Also, if you are interested in the cleanup effort, please click this link http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643
 
Thank you everyone.
 
Moondancer
Moderator @ Experts Exchange

P.S.  For any year 2000 questions, special attention is needed to ensure the first correct response is awarded, since they are not in the comment date order, but rather in Member ID order.
0
 
dbruntonCommented:
PAQ and zero points this one.
0
 
MoondancerCommented:
Thank you, dbrunton.  100 points refunded, moved to PAQ at zero to close.
Moondancer - EE Moderator
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now