Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

how do i compare array?

Posted on 2003-03-06
11
Medium Priority
?
196 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
  • 3
  • 2
  • 2
  • +2
10 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: 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.

 
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

I’m willing to make a bet that your organization stores sensitive data in your Windows File Servers; files and folders that you really don’t want making it into the wrong hands.
How do you create a user-centered user experience on your website? And what are some things you should consider in the process?
Loops Section Overview
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

580 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