Link to home
Start Free TrialLog in
Avatar of daren771
daren771

asked on

how do i compare array?

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.


ASKER CERTIFIED SOLUTION
Avatar of ILE
ILE

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of VGR
VGR

goto and label !?!
It had been a loooong time since I saw someone daring to put this in a Pascal program 8-)
Ho ho ho ... poor goto & label.
My teacher would gave me E for that ;-)
but cut some code

and to be onest with you i newer lisen too much my teacher :))
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.

 
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
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.
 
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;
{__________________________________________________________}


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.
forced answer to ILE due to lack of feedback