Recent

Author Topic: weird behavior with terminal program, does not run as expected  (Read 1898 times)

v12power

  • Newbie
  • Posts: 3
weird behavior with terminal program, does not run as expected
« on: November 15, 2023, 08:10:33 pm »
hey all, i've got this task to do approve one of the pascal subjects in my university. honestly my pc broke down so lost like 2 weeks there, then on top of that add the fact that I also work so my time was very limited to make it.

they ask us to make a few procedures and functions, for a simple text prediction program. This program is fed a text and it will go through 2 words at once, it will hash the first word, and take in the second word.
The hash is the index of an array of words (a node list) in which all the words that come after the hashed word will be shown, if there are repeated ones, it will show up as one occurence with quantity of 2, they have a counter variable for this.

like this, lets focus on the world hello, but remember it does for every word pair.

hello whats your name beautiful. hello there. hello whats your name

for the first 2 words, 'hello' and 'whats'
hash hello = 11437
feed 'whats'

if the list at index 11437 is empty, add 'whats' to it, with quantity=1

for the worlds 'hello' and 'there'
hash hello = 11437
feed 'there'

in this case the word 'there' is compared with the words in the list at index 11437, one by one till it reaches the end of the list, if 'there' doesnt exist in the list, it will be added to the end of it, with quantity = 1
...
words 'hello' and 'whats' again
hash hello = 11437
feed 'whats'

now it will compare 'whats' with the words in the list 11437, but now, instead of adding it again, it will add 1 to the counter of the word 'whats'
ignoring all the other words, the program output should be

(11437) -(whats,2),(there,1)

for some reason my program works only if all the words in the text are different. If there is just one repeated word it will not add it to the existing indexed list and it wont show this index in the terminal at all.

We were asked to program the functions that 1) hash the word 2) compare a pair of words, 3) add it to the list, 4) initialize the indexed list (just adding nil to all the pointers) 5) run the program

I have tried to trace the problem and it goes to the function that adds nodes to the list, specially when it has to add one to a non empty list ie when you come across the same words in the text, but im just lost on why this is happening...this is the function if anyone wants to take a look.

the file with all the other functions and procedures is in the attachment, this is the one that first checks if list is empty and if it isnt then it will compare, then depending on this we will either add the word to the list or add 1 to the counter.

Code: Pascal  [Select][+][-]
  1. procedure agregarOcurrencia(p:palabra; var pals:Ocurrencias);
  2. var
  3.    paux,pnueva:Ocurrencias;
  4.    comp:Comparacion;
  5.    i:integer;
  6. begin
  7.    i:=1;
  8.    paux:=pals;
  9.    if pals=NIL then
  10.        begin
  11.        new(pnueva);
  12.        pnueva^.palc.pal:=p;
  13.        pnueva^.palc.cant:=1;
  14.        pnueva^.sig:=nil;
  15.        pals:=pnueva;
  16.        i:=4
  17.        end;
  18.    while (pals<>NIL) and (i<3) do
  19.       begin      
  20.       comp:=comparaPalabra(pals^.palc.pal,p);
  21.       if comp=igual then   // if theyre equal we add +1 to the counter
  22.             begin    
  23.                pals^.palc.cant:=pals^.palc.cant+1;
  24.                i:=5
  25.             end
  26.          else if comp<>igual then  //if they're different all the way to the end we use i as a flag to modify the list
  27.             begin  
  28.                i:=2
  29.             end;
  30.       pals:=pals^.sig      
  31.       end;      
  32.    if i=2 then      // this adds the current word p to the end of the list pals
  33.       begin    
  34.           writeln('añado elemento');
  35.           new(pnueva);
  36.           pnueva^.palc.pal:=p;
  37.           pnueva^.palc.cant:=1;
  38.           pnueva^.sig:=nil;
  39.           while paux^.sig<>NIL do
  40.              paux:=paux^.sig;
  41.           paux^.sig:=pnueva;          
  42.       end;    
  43. end;      


MarkMLl

  • Hero Member
  • *****
  • Posts: 8572
Re: weird behavior with terminal program, does not run as expected
« Reply #1 on: November 16, 2023, 10:09:24 am »
Before anything else, are you using FPC? Are you using Lazarus as the IDE? Have you tried stepping through your program using the IDE?

Much as we would like to help, it's not at all easy to do so without a compilable program that demonstrates the problem. You've not even provided definitions for the data types in your function declarations... please bear in mind that the rest of us have things to do as well.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

v12power

  • Newbie
  • Posts: 3
Re: weird behavior with terminal program, does not run as expected
« Reply #2 on: November 16, 2023, 10:53:37 am »
im not allowed to use any IDE, just fpc. well here they are, the rest of the files. Im gonna try stepping through anyways, didnt even know that could be done

MarkMLl

  • Hero Member
  • *****
  • Posts: 8572
Re: weird behavior with terminal program, does not run as expected
« Reply #3 on: November 16, 2023, 11:27:02 am »
im not allowed to use any IDE, just fpc. well here they are, the rest of the files. Im gonna try stepping through anyways, didnt even know that could be done

If you have the standard FPC stuff you should have the fp character-based editor etc. In any event, I'd expect that your system- whatever it is- will have gdb etc. which should work with the binary fpc produces.

Bear in mind that your lecturer might be reading this, and that in any event this discussion will turn up in response to a trivial Google search. You'll gain marks if you work out your problem using a debugger, but lose them if you rely on people to do your work for you.

So let us know how you get on with the debugging, and if you make any changes post the changed files so that we can see where you're at before giving you any hints.

Oh, and as somebody with experience in the area please tell your lecturer that "not allowed to use an IDE" is pretty lame. A modern IDE makes life easier, but does not pretend to give you magic answers in the style of e.g. ChatGPT.

MarkMLl
« Last Edit: November 16, 2023, 12:48:14 pm by MarkMLl »
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

dbannon

  • Hero Member
  • *****
  • Posts: 3826
    • tomboy-ng, a rewrite of the classic Tomboy
Re: weird behavior with terminal program, does not run as expected
« Reply #4 on: November 16, 2023, 11:15:08 pm »
I agree completely with Mark, but there is a very basic and always available debugging technique. You sprinkle write('In Some_Procedure and Count=', Some_count) type things in the critical part of your code. That way, you see what is happening at the spot you are focused on, if it disagrees with your intended action, look closer !

No IDE, no debugger needed.

Davo
Lazarus 4, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: weird behavior with terminal program, does not run as expected
« Reply #5 on: November 17, 2023, 05:31:13 am »
I agree completely with Mark, but there is a very basic and always available debugging technique. You sprinkle write('In Some_Procedure and Count=', Some_count) type things in the critical part of your code. That way, you see what is happening at the spot you are focused on, if it disagrees with your intended action, look closer !

No IDE, no debugger needed.
+1000

Might be considered old school and not 'of this day and age' but it is the best technique I've ever learned (and still use to this day at times). You have no idea how many times wrong assumptions are made on the state or contents of some variables that are buried deep in some lengthy code. Sometimes it is not practical to use the debugger and directing some output to a file for analysis is easier/quicker to accomplish.
Today is tomorrow's yesterday.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8572
Re: weird behavior with terminal program, does not run as expected
« Reply #6 on: November 17, 2023, 08:43:38 am »
Might be considered old school and not 'of this day and age' but it is the best technique I've ever learned (and still use to this day at times). You have no idea how many times wrong assumptions are made on the state or contents of some variables that are buried deep in some lengthy code. Sometimes it is not practical to use the debugger and directing some output to a file for analysis is easier/quicker to accomplish.

I agree, and it's particularly appropriate when "old-school" pointers and dynamic allocation is being used.

However if it were one of my students I'd have definitely awarded extra marks for working out how to use the debugger.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

v12power

  • Newbie
  • Posts: 3
Re: weird behavior with terminal program, does not run as expected
« Reply #7 on: November 17, 2023, 11:51:56 am »
yeah i've been doing that trick of writing text or stuff for some time already. I just changed the referencing pointers, not using an auxiliary list anymore and it works, somewhat. I think im gonna leave it like that cause im really short on time however.

It's now even more strange to me why it works on some texts and others dont lol, anyway here's what I did if youre wondering

Code: Pascal  [Select][+][-]
  1. function hash(semilla,paso,N:Natural; p:palabra): Natural;
  2. var
  3.     i:integer;
  4.     hash1:Natural;
  5. begin
  6.     hash1:= semilla;
  7.     i:=1;
  8.     repeat
  9.         hash1:= (hash1*3+ord(p.cadena[i])) mod N;
  10.         i:=i+1;
  11.     until i=p.tope+1;  
  12.     if hash1>MAXHASH then
  13.         hash1:=MAXHASH;
  14.     hash:=hash1;
  15. end;      
  16.      
  17. function comparaPalabra(p1,p2:Palabra):Comparacion;
  18. var
  19.   i,limite:Natural;
  20.   comp:Comparacion;
  21. begin
  22. i:=1;
  23. if (p1.tope-p2.tope)<=0 then
  24.    limite:=p1.tope
  25. else
  26.    limite:=p2.tope;
  27. while (i<=limite) do
  28.    begin
  29.        if (ord(p1.cadena[i])=ord(p2.cadena[i])) and (p1.tope=p2.tope) then
  30.        begin  
  31.            comp:=igual
  32.        end
  33.        else if (ord(p1.cadena[i])<ord(p2.cadena[i])) then
  34.        begin  
  35.            comp:=menor;
  36.            i:=limite+1
  37.        end
  38.        else if (ord(p1.cadena[i])>ord(p2.cadena[i])) then
  39.        begin    
  40.            comp:=mayor;
  41.            i:=limite+1
  42.        end;
  43.    i:=i+1    
  44.    end;
  45. comparaPalabra:=comp  
  46. end;
  47. function mayorPalabraCant(pc1,pc2:PalabraCant):boolean;
  48. var
  49.    comp:Comparacion;
  50. begin
  51. comp:=comparaPalabra(pc1.pal,pc2.pal);
  52. if (pc2.cant<=pc1.cant) and (comp=mayor) then
  53.    mayorPalabraCant:=true
  54. else
  55.    mayorPalabraCant:=false
  56. end;
  57.  
  58. procedure agregarOcurrencia(p:palabra; var pals:Ocurrencias);
  59. var
  60.    paux,pnueva:Ocurrencias;
  61.    comp:Comparacion;
  62.    i:integer;
  63. begin
  64.    i:=2;
  65.    paux:=pals;
  66.    while (paux<>NIL) and (i<3) do
  67.       begin      
  68.       comp:=comparaPalabra(paux^.palc.pal,p);
  69.       if comp=igual then
  70.             begin    
  71.                paux^.palc.cant:=paux^.palc.cant+1;
  72.                i:=5
  73.             end
  74.          else if comp<>igual then
  75.             begin  
  76.                i:=2;
  77.             end;
  78.       paux:=paux^.sig      
  79.       end;      
  80.    if i=2 then  
  81.       begin    
  82.           new(pnueva);
  83.           pnueva^.palc.pal:=p;
  84.           pnueva^.palc.cant:=1;
  85.           pnueva^.sig:=nil;
  86.           if pals=NIL then
  87.              begin
  88.              pals:=pnueva
  89.              end
  90.           else
  91.           begin
  92.              while pals^.sig<>NIL do
  93.                 begin
  94.                 pals:=pals^.sig
  95.                 end;
  96.              pals^.sig:=pnueva
  97.           end;  
  98.       end;    
  99. end;      
  100. procedure inicializarPredictor(var pred:Predictor);
  101. var
  102.    i:1..MAXHASH;
  103. begin
  104.    for i:=1 to MAXHASH do
  105.        pred[i]:=NIL
  106. end;      
  107.  
  108. procedure entrenarPredictor(txt:Texto; var pred:Predictor);
  109. var
  110.    i:1..MAXHASH;
  111. begin
  112.    i:=hash(SEMILLA,PASO,MAXHASH,txt^.info);
  113.    txt:=txt^.sig;
  114.    while txt<>NIL do
  115.    begin
  116.       agregarOcurrencia(txt^.info,pred[i]);
  117.       i:=hash(SEMILLA,PASO,MAXHASH,txt^.info);
  118.       txt:=txt^.sig
  119.    end;
  120. end;    
  121.        
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  

 

TinyPortal © 2005-2018