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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Ho ho ho ... poor goto & label.
My teacher would gave me E for that ;-)
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 :))
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.
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
: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]o f 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_EX P]<>WORD_D IVOTION_CH AR)
AND
(POS_OF_LAST_EXP<MAX)
DO BEGIN
EXPRESSION_STR_1:=EXPRESSI ON_STR_1+
EXPRESSION[POS_OF_LAST_EXP ];
INC(POS_OF_LAST_EXPRESSION ,1);
END;
POS_IN_EXP:=POS_OF_LAST_EX P;
REPEAT
WHILE (EXPRESSION[POS_in_EXP]<>W ORD_DIVOTI ON_CHAR)
AND
(POS_in_EXP<MAX)
DO BEGIN
EXPRESSION_STR_2:=EXPRESSI ON_STR_2+
EXPRESSION[POS_IN_EXP];
INC(POS_IN_EXPRESSION,1);
END;
IF EXPRESSION_STR_1=EXPRESSIO N_STR_2
then INC(EXP_COUNTER,1);
UNTIL POS_IN_EXP=MAX;
UNTIL POS_OF_LAST_EXP=MAX;
END.
shall I do your homework?
Try this shot :
CONST
MAX=1000;
WORD_DIVOTION_CHAR='-';
VAR
EXPRESSIONS_A,
EXPRESSIONS_B:ARRAY[1..MAX
POS_IN_EXP_A,
POS_IN_EXP_B,
POS_OF_LAST_EXP_A,
POS_OF_LAST_EXP_B:LONGINT;
EXPRESSIONS:ARRAY[1..MAX]o
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_EX
AND
(POS_OF_LAST_EXP<MAX)
DO BEGIN
EXPRESSION_STR_1:=EXPRESSI
EXPRESSION[POS_OF_LAST_EXP
INC(POS_OF_LAST_EXPRESSION
END;
POS_IN_EXP:=POS_OF_LAST_EX
REPEAT
WHILE (EXPRESSION[POS_in_EXP]<>W
AND
(POS_in_EXP<MAX)
DO BEGIN
EXPRESSION_STR_2:=EXPRESSI
EXPRESSION[POS_IN_EXP];
INC(POS_IN_EXPRESSION,1);
END;
IF EXPRESSION_STR_1=EXPRESSIO
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]o f 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_EX P]<>WORD_D IVOTION_CH AR)
AND
(POS_OF_LAST_EXP<MAX)
DO BEGIN
EXPRESSION_STR_1:=EXPRESSI ON_STR_1+
EXPRESSION[POS_OF_LAST_EXP ];
INC(POS_OF_LAST_EXPRESSION ,1);
END;
POS_IN_EXP:=POS_OF_LAST_EX P;
EXP_COUNTER:=0;
REPEAT
WHILE (EXPRESSION[POS_in_EXP]<>W ORD_DIVOTI ON_CHAR)
AND
(POS_in_EXP<MAX)
DO BEGIN
EXPRESSION_STR_2:=EXPRESSI ON_STR_2+
EXPRESSION[POS_IN_EXP];
INC(POS_IN_EXPRESSION,1);
END;
IF EXPRESSION_STR_1=EXPRESSIO N_STR_2
then INC(EXP_COUNTER,1);
UNTIL POS_IN_EXP=MAX;
WRITELN(EXPRESSION_STR_1,E XP_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:POI NTER;Count :Word):Wor d;ASSEMBLE R;
ASM
PUSH DS
CLD
MOV CX,COUNT
LDS SI,SOURCE
LES DI,DEST
REPE CMPSB
POP DS
MOV AX,COUNT
SUB AX,CX
end;
{_________________________ __________ __________ __________ ___}
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]o
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_EX
AND
(POS_OF_LAST_EXP<MAX)
DO BEGIN
EXPRESSION_STR_1:=EXPRESSI
EXPRESSION[POS_OF_LAST_EXP
INC(POS_OF_LAST_EXPRESSION
END;
POS_IN_EXP:=POS_OF_LAST_EX
EXP_COUNTER:=0;
REPEAT
WHILE (EXPRESSION[POS_in_EXP]<>W
AND
(POS_in_EXP<MAX)
DO BEGIN
EXPRESSION_STR_2:=EXPRESSI
EXPRESSION[POS_IN_EXP];
INC(POS_IN_EXPRESSION,1);
END;
IF EXPRESSION_STR_1=EXPRESSIO
then INC(EXP_COUNTER,1);
UNTIL POS_IN_EXP=MAX;
WRITELN(EXPRESSION_STR_1,E
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:POI
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.
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
It had been a loooong time since I saw someone daring to put this in a Pascal program 8-)