weird behavior with terminal program, does not run as expected lazarus / 21-General ---------------------------------------------------------------------- v12power · 2023-11-15 19:10 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure agregarOcurrencia(p:palabra; var pals:Ocurrencias);var   paux,pnueva:Ocurrencias;   comp:Comparacion;   i:integer;begin    i:=1;   paux:=pals;   if pals=NIL then        begin        new(pnueva);       pnueva^.palc.pal:=p;       pnueva^.palc.cant:=1;     pnueva^.sig:=nil;        pals:=pnueva;       i:=4       end;    while (pals<>NIL) and (i<3) do       begin            comp:=comparaPalabra(pals^.palc.pal,p);       if comp=igual then   // if theyre equal we add +1 to the counter            begin                 pals^.palc.cant:=pals^.palc.cant+1;                i:=5          end         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            begin              i:=2            end;      pals:=pals^.sig            end;         if i=2 then      // this adds the current word p to the end of the list pals      begin              writeln('añado elemento');          new(pnueva);          pnueva^.palc.pal:=p;         pnueva^.palc.cant:=1;          pnueva^.sig:=nil;          while paux^.sig<>NIL do              paux:=paux^.sig;           paux^.sig:=pnueva;                end;    end; ---------------------------------------------------------------------- MarkMLl · 2023-11-16 09:09 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 ---------------------------------------------------------------------- v12power · 2023-11-16 09:53 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 · 2023-11-16 10:27 Quote from: v12power on November 16, 2023, 10:53:37 amim 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 ---------------------------------------------------------------------- dbannon · 2023-11-16 22:15 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 ---------------------------------------------------------------------- TRon · 2023-11-17 04:31 Quote from: dbannon on November 16, 2023, 11:15:08 pmI 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. ---------------------------------------------------------------------- MarkMLl · 2023-11-17 07:43 Quote from: TRon on November 17, 2023, 05:31:13 amMight 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 ---------------------------------------------------------------------- v12power · 2023-11-17 10:51 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function hash(semilla,paso,N:Natural; p:palabra): Natural;var     i:integer;  hash1:Natural;begin    hash1:= semilla;    i:=1;    repeat        hash1:= (hash1*3+ord(p.cadena[i])) mod N;        i:=i+1;    until i=p.tope+1;      if hash1>MAXHASH then        hash1:=MAXHASH;    hash:=hash1;end;            function comparaPalabra(p1,p2:Palabra):Comparacion; var   i,limite:Natural;  comp:Comparacion;begini:=1; if (p1.tope-p2.tope)<=0 then   limite:=p1.topeelse    limite:=p2.tope;while (i<=limite) do    begin      if (ord(p1.cadena[i])=ord(p2.cadena[i])) and (p1.tope=p2.tope) then        begin              comp:=igual       end       else if (ord(p1.cadena[i])ord(p2.cadena[i])) then       begin               comp:=mayor;           i:=limite+1       end;    i:=i+1       end; comparaPalabra:=comp   end; function mayorPalabraCant(pc1,pc2:PalabraCant):boolean;var   comp:Comparacion;begin comp:=comparaPalabra(pc1.pal,pc2.pal);if (pc2.cant<=pc1.cant) and (comp=mayor) then   mayorPalabraCant:=true else   mayorPalabraCant:=falseend;  procedure agregarOcurrencia(p:palabra; var pals:Ocurrencias);var   paux,pnueva:Ocurrencias;   comp:Comparacion;   i:integer;begin    i:=2;   paux:=pals;   while (paux<>NIL) and (i<3) do       begin         comp:=comparaPalabra(paux^.palc.pal,p);       if comp=igual then           begin                   paux^.palc.cant:=paux^.palc.cant+1;                i:=5            end         else if comp<>igual then          begin                 i:=2;            end;      paux:=paux^.sig            end;         if i=2 then        begin            new(pnueva);          pnueva^.palc.pal:=p;           pnueva^.palc.cant:=1;          pnueva^.sig:=nil;          if pals=NIL then              begin             pals:=pnueva             end        else           begin             while pals^.sig<>NIL do               begin                pals:=pals^.sig                end;           pals^.sig:=pnueva           end;         end;    end;      procedure inicializarPredictor(var pred:Predictor);var   i:1..MAXHASH;begin    for i:=1 to MAXHASH do       pred[i]:=NILend;      procedure entrenarPredictor(txt:Texto; var pred:Predictor);var   i:1..MAXHASH;begin    i:=hash(SEMILLA,PASO,MAXHASH,txt^.info);   txt:=txt^.sig;   while txt<>NIL do   begin       agregarOcurrencia(txt^.info,pred[i]);      i:=hash(SEMILLA,PASO,MAXHASH,txt^.info);      txt:=txt^.sig   end;end;