?
Solved

how do i compare array?

Posted on 2003-03-06
11
Medium Priority
?
192 Views
Last Modified: 2010-04-16
how do i compare an array with words?
i did a program to read a text file and store the words in an array. i need to check for the number of different words in the file. if "the" is in the file 3 times then
i will print out

Word   Appears
----   -------
"the"      3

etc .....

var
c:char;
FileName:text;
FileToOpen: string;
InWord: boolean;
Word: array[1..100] of char;
Word2: array[1..100] of char;
Count, i, occur: integer;
CharIN,CharOUT:boolean;

const
WordChar : set of char = ['A'..'Z', 'a'..'z', '-','.',',','''','"','$','!','?'];
{set of characters which is a word}
Max = 100;

procedure CheckChar;
begin
  CharIn := InWord;
  if c in WordChar then
     InWord := true
  else
  begin
     InWord := false;
  end;
  CharOut := InWord;

  if (CharIn) and not(CharOUT) then
      CheckChar := true;   ----> Error 85: ";" expected

end;

begin
Count := 0;
writeln('Enter the full pathname of the file : ');
readln(FileToOpen);
Assign(FileName, FileToOpen);
reset(FileName);

while (Count < Max) and not(eof(FileName)) do
begin
  Count := Count + 1;
  read(FileName, c);
  if CheckChar = true then
    Word[Count] := c;
end;

 Word2[1] := Word[1];

 for i:=2 to 100 do
  begin
   if Word[i] <> Word2[i-1] then
        Word2[i] := Word[i];
   else
      Occur := Occur + 1;
   end;

 writeln(Word, Occur);

 close(FileName);

 end.


0
Comment
Question by:daren771
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
11 Comments
 
LVL 3

Accepted Solution

by:
ILE earned 200 total points
ID: 8082538
100% working solution

label kraj;
var
fil: text;
s,s1:string;
i,i1:integer;
   a:array[1..1000] of string[30];
   n:array[1..1000] of integer;
begin
assign(fil,'c:\tp\prob.pas');
reset(fil); i1:=0;
while not eof(fil) do
begin
  readln(fil,s);

  while s>'' do
    begin
     i:=pos(' ',s); if i=0 then i:=length(s)+1;
     s1:=copy(s,1,i-1); s:=copy(s,i+1,255);
     for i:=1 to i1 do if a[i]=s1 then begin inc(n[i]); goto kraj; end;
       inc(i1); a[i1]:=s1; n[i1]:=1;
     kraj:
    end;

end;
close(fil);
for i:=1 to i1 do
writeln(a[i]:30,n[i]:5);
end.





0
 
LVL 15

Expert Comment

by:VGR
ID: 8083505
goto and label !?!
It had been a loooong time since I saw someone daring to put this in a Pascal program 8-)
0
 
LVL 5

Expert Comment

by:Kocil
ID: 8083905
Ho ho ho ... poor goto & label.
My teacher would gave me E for that ;-)
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

 
LVL 3

Expert Comment

by:ILE
ID: 8085453
but cut some code

and to be onest with you i newer lisen too much my teacher :))
0
 
LVL 5

Expert Comment

by:Kocil
ID: 8085939
Well, just a story. One of my senior wrote his first program without any procedure/function. It was his undergraduate theses, so it had more than 1000 lines. The funny thing was, he used goto/label, and gave his girl friend name as the label,i.e: girl001, girl002, and so on.
The bad thing was, I was the unlucky predecessor that had to continue his work !!! After a month I've reading the girl's name over and over again, she started to jump around in my dream.

 
0
 
LVL 15

Expert Comment

by:VGR
ID: 8086552
jumping in your dreams ... and in your bed ?
:D

Anyway, it's a "lovely" story, thank you 8-)

ILE : Continue to not listen too mucj to teachers, you're on the right path... Until you become a teacher yourself and start complaining about the very same behaviour :D :D
0
 
LVL 1

Expert Comment

by:Okey
ID: 8178221
Ey, maaan!
shall I do your homework?

Try this shot :
CONST
 MAX=1000;
 WORD_DIVOTION_CHAR='-';
VAR
 EXPRESSIONS_A,
 EXPRESSIONS_B:ARRAY[1..MAX]of CHAR;
 POS_IN_EXP_A,
 POS_IN_EXP_B,
 POS_OF_LAST_EXP_A,
 POS_OF_LAST_EXP_B:LONGINT;

 EXPRESSIONS:ARRAY[1..MAX]of CHAR;
 EXP_COUNTER,
 POS_IN_EXP,
 POS_OF_LAST_EXP:LONGINT;
 EXPRESSION_STR_1,
 EXPRESSION_STR_2:STRING
BEGIN
 ...
 Up to here your array has to be filled! OK.
 ...
 POS_IN_EXP_A:=1;
 POS_IN_EXP_B:=1;
 POS_OF_LAST_EXP_A:=1;
 POS_OF_LAST_EXP_B:=1;

 POS_IN_EXP:=1;
 POS_OF_LAST_EXP:=1;

 REPEAT
  EXPRESSION_STR:='';
  WHILE (EXPRESSION[POS_OF_LAST_EXP]<>WORD_DIVOTION_CHAR)
         AND
         (POS_OF_LAST_EXP<MAX)
   DO BEGIN
       EXPRESSION_STR_1:=EXPRESSION_STR_1+
                         EXPRESSION[POS_OF_LAST_EXP];
       INC(POS_OF_LAST_EXPRESSION,1);
      END;
  POS_IN_EXP:=POS_OF_LAST_EXP;

  REPEAT
   WHILE (EXPRESSION[POS_in_EXP]<>WORD_DIVOTION_CHAR)
          AND
          (POS_in_EXP<MAX)
    DO BEGIN
        EXPRESSION_STR_2:=EXPRESSION_STR_2+
                          EXPRESSION[POS_IN_EXP];
        INC(POS_IN_EXPRESSION,1);
       END;
   IF EXPRESSION_STR_1=EXPRESSION_STR_2
    then INC(EXP_COUNTER,1);
  UNTIL POS_IN_EXP=MAX;
 UNTIL POS_OF_LAST_EXP=MAX;
 
 



END.
 
0
 
LVL 1

Expert Comment

by:Okey
ID: 8178289
Ooopsy I used the Tabulator-key and space so I submitted before finnishing

Wait
does this do the JOB or what was it you tried to compare ?
The counting of redundant expression should run like this:

CONST
 MAX=1000;
 WORD_DIVOTION_CHAR='-';
VAR
 EXPRESSIONS:ARRAY[1..MAX]of CHAR;
 EXP_COUNTER,
 POS_IN_EXP,
 POS_OF_LAST_EXP:LONGINT;
 EXPRESSION_STR_1,
 EXPRESSION_STR_2:STRING
BEGIN
...
Up to here your array's have to be filled! OK.
...

The following code
will check for redundant expressions in the array

...

POS_IN_EXP:=1;
POS_OF_LAST_EXP:=1;

REPEAT
 EXPRESSION_STR:='';
 WHILE (EXPRESSION[POS_OF_LAST_EXP]<>WORD_DIVOTION_CHAR)
        AND
        (POS_OF_LAST_EXP<MAX)
  DO BEGIN
      EXPRESSION_STR_1:=EXPRESSION_STR_1+
                        EXPRESSION[POS_OF_LAST_EXP];
      INC(POS_OF_LAST_EXPRESSION,1);
     END;
 POS_IN_EXP:=POS_OF_LAST_EXP;
 EXP_COUNTER:=0;
 REPEAT
  WHILE (EXPRESSION[POS_in_EXP]<>WORD_DIVOTION_CHAR)
         AND
         (POS_in_EXP<MAX)
   DO BEGIN
       EXPRESSION_STR_2:=EXPRESSION_STR_2+
                         EXPRESSION[POS_IN_EXP];
       INC(POS_IN_EXPRESSION,1);
      END;
  IF EXPRESSION_STR_1=EXPRESSION_STR_2
   then INC(EXP_COUNTER,1);
 UNTIL POS_IN_EXP=MAX;
 WRITELN(EXPRESSION_STR_1,EXP_COUNTER:10);
UNTIL POS_OF_LAST_EXP=MAX;

END.

ARRAYs can be compared like RAM!
Following assembly snippet should return POSITION FROM WHERE THE RAM STARTS TO DIFFER
{__________________________________________________________}
Function REPE_CMPSB(Source,Dest:POINTER;Count:Word):Word;ASSEMBLER;
ASM
 PUSH  DS
 CLD
 MOV   CX,COUNT
 LDS   SI,SOURCE
 LES   DI,DEST
 REPE  CMPSB
 POP   DS
 MOV   AX,COUNT
 SUB   AX,CX
end;
{__________________________________________________________}


0
 

Expert Comment

by:CleanupPing
ID: 9314369
daren771:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 15

Expert Comment

by:VGR
ID: 9324114
forced answer to ILE due to lack of feedback
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An introduction to the wonderful sport of Scam Baiting.  Learn how to help fight scammers by beating them at their own game. This great pass time helps the world, while providing an endless source of entertainment. Enjoy!
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question