Recent

Author Topic: Compiles, but doesn't execute. Problem with lists  (Read 3390 times)

medianna

  • Newbie
  • Posts: 4
Compiles, but doesn't execute. Problem with lists
« on: November 12, 2018, 01:30:34 am »
Hi,

I have a homework problem where I need to fill in parts of a given program. The program is intended to sort by insertion. Lists have just been introduced in the last part of the lecture but I haven't understood the entire concept. I'm guessing that the while loop goes to an infinite loop, but I can't quite figure out why. Any help is appreciated!
Here is the code (I have marked the parts where my codes starts and ends):
Code: Pascal  [Select][+][-]
  1. program TesteSortiereListe(input, output);
  2. { sortiert eine Liste durch Einfügen }
  3. { das Programm testet, ob die Liste sortiert ist }
  4. { Liste wird in der Prozedur sortiert }
  5.  
  6.   type
  7.   tNatZahl = 0..maxint;
  8.   tRefListe = ^tListe;
  9.   tListe = record
  10.              wert : tNatZahl;
  11.              next : tRefListe;
  12.            end;
  13.  
  14.   var
  15.   RefListe : tRefListe;
  16.  
  17.   procedure SortiereListe (var ioRefListe : tRefListe);
  18.   { variable ioRefListe zeigt auf das erste Element der Liste RefListe }
  19.   { sortiert eine nicht-leere lineare Liste aufsteigend }
  20.  
  21.     var
  22.     { Listenobjekte }
  23.     EinfuegePosition,
  24.     WirdJetztSortiert,
  25.     IstSchonSortiert : tRefListe;
  26.  
  27.    
  28.     istEingefuegt : boolean;
  29.  
  30.   begin
  31.     { die einelementige Liste ist immer sortiert }
  32.     IstSchonSortiert := ioRefListe;
  33.     { wir beginnen das zweite Element zu sortieren }
  34.     WirdJetztSortiert := ioRefListe^.next;
  35.     { solange es noch etwas zu sortieren gibt }
  36.     while WirdJetztSortiert <> nil do
  37.     begin
  38.       istEingefuegt := false;
  39.    
  40.  
  41. {THIS IS WHERE MY CODE STARTS }
  42.       { Fall 1: WirdJetztSortiert ist das neue kleinste Element }
  43.           if WirdJetztSortiert^.wert < ioRefListe^.wert then
  44.           begin
  45.                 IstSchonSortiert^.next := WirdJetztSortiert^.next;
  46.                 WirdJetztSortiert^.next := ioRefListe;
  47.                 ioRefListe^.next := WirdJetztSortiert
  48.           end;
  49.  
  50.           {Fall 2: WirdJetztSortiert bleibt wo es ist }
  51.  
  52.           if WirdJetztSortiert^.wert > IstSchonSortiert^.wert then
  53.           begin
  54.                 IstSchonSortiert := WirdJetztSortiert
  55.           end;
  56.  
  57.       { Fall 3: richtige EinfuegePosition im sortieten Teil suchen und verschieben }
  58.       EinfuegePosition := ioRefListe;
  59.       while istEingefuegt = false do
  60.       begin
  61.                   if WirdJetztSortiert^.wert < EinfuegePosition^.next^.wert then
  62.                   begin
  63.                         IstSchonSortiert := WirdJetztSortiert^.next;
  64.                         WirdJetztSortiert^.next := EinfuegePosition^.next;
  65.                         EinfuegePosition^.next := WirdJetztSortiert;
  66.                        
  67.                         istEingefuegt := true
  68.                   end;
  69.                  
  70.                   EinfuegePosition := EinfuegePosition^.next
  71.           end;
  72.  
  73. { THIS IS WHERE MY CODE ENDS }
  74.  
  75.  
  76.         WirdJetztSortiert := IstSchonSortiert^.next
  77.     end
  78.   end;
  79.  
  80.   procedure Anhaengen(inZahl : tNatZahl; var ioListe : tRefListe);
  81.   { Haengt inZahl an ioListe an }
  82.  
  83.     var
  84.     Zeiger : tRefListe;
  85.    
  86.   begin
  87.     Zeiger := ioListe;
  88.     if Zeiger = nil then
  89.     begin
  90.       new(ioListe);
  91.       ioListe^.wert := inZahl;
  92.       ioListe^.next := nil;
  93.     end
  94.     else
  95.     begin
  96.       while Zeiger^.next <> nil do
  97.         Zeiger := Zeiger^.next;
  98.       new(Zeiger^.next);
  99.       Zeiger := Zeiger^.next;
  100.       Zeiger^.wert := inZahl;
  101.       Zeiger^.next := nil;
  102.     end;
  103.   end;
  104.  
  105.   procedure ListeEinlesen(var outListe:tRefListe);
  106.   { liest eine Folge von Zahlen ein }
  107.  
  108.     var
  109.     Liste : tRefListe;
  110.     Zahl : integer;
  111.    
  112.   begin
  113.     Liste := nil;
  114.     read(Zahl);
  115.     while Zahl<>-1 do
  116.     begin
  117.       Anhaengen(Zahl, Liste);
  118.       read(Zahl)
  119.     end;
  120.     outListe := Liste
  121.   end;
  122.  
  123.   procedure GibListeAus(inListe : tRefListe);
  124.   { Gibt die eine nicht-leere Liste aus }
  125.  
  126.     var
  127.     Zeiger : tRefListe;
  128.    
  129.   begin
  130.     Zeiger := inListe;
  131.     write(Zeiger^.wert);
  132.     Zeiger := Zeiger^.next;
  133.     while Zeiger <> nil do
  134.     begin
  135.       write(', ');
  136.       write(Zeiger^.wert);
  137.       Zeiger := Zeiger^.next;
  138.     end;
  139.     writeln('')  
  140.   end;
  141.  
  142. begin
  143.   ListeEinlesen(RefListe);
  144.   SortiereListe(RefListe);
  145.   GibListeAus(RefListe)
  146. end.
  147.  
« Last Edit: November 12, 2018, 01:40:01 am by Martin_fr »

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9792
  • Debugger - SynEdit - and more
    • wiki
Re: Compiles, but doesn't execute. Problem with lists
« Reply #1 on: November 12, 2018, 01:40:42 am »
Added "code tags" for formatting/readability.

medianna

  • Newbie
  • Posts: 4
Re: Compiles, but doesn't execute. Problem with lists
« Reply #2 on: November 12, 2018, 01:41:37 am »
Added "code tags" for formatting/readability.

Thanks! Will remember that the next time!

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9792
  • Debugger - SynEdit - and more
    • wiki
Re: Compiles, but doesn't execute. Problem with lists
« Reply #3 on: November 12, 2018, 01:48:28 am »
Well you should at least get to the input prompt. You can add extra writeln to see where the program goes (or start using the debugger).

What OS are you on?

On Windows, you should get a black console window, as soon as you run. (If you run from a console then the console is already open, use writeln, to check something happens)
If not, you may have the wrong app type. Either
-google {$APPTYPE} 
-change project options => target => unselect GUI app (Lazarus only)
- create a new project => type program (Lazarus only)

In Lazarus on Linux you need to open menu > view > Debugger > Console
« Last Edit: November 12, 2018, 01:50:27 am by Martin_fr »

medianna

  • Newbie
  • Posts: 4
Re: Compiles, but doesn't execute. Problem with lists
« Reply #4 on: November 12, 2018, 02:01:02 am »
Well you should at least get to the input prompt. You can add extra writeln to see where the program goes (or start using the debugger).

What OS are you on?

On Windows, you should get a black console window, as soon as you run. (If you run from a console then the console is already open, use writeln, to check something happens)
If not, you may have the wrong app type. Either
-google {$APPTYPE} 
-change project options => target => unselect GUI app (Lazarus only)
- create a new project => type program (Lazarus only)

In Lazarus on Linux you need to open menu > view > Debugger > Console

I'm on windows and do get the black console window but it doesn't ask me to input anything - which is what I at least expected to happen. When I type in writeln I get this error:

writeln()
Runtime error 106 at $004015A8
  $004015A8
  $00401742
  $00407701



------------------
(program exited with code: 106)

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9792
  • Debugger - SynEdit - and more
    • wiki
Re: Compiles, but doesn't execute. Problem with lists
« Reply #5 on: November 12, 2018, 02:16:28 am »
https://www.freepascal.org/docs-html/user/userap4.html
Quote
106 Invalid numeric format
    Reported when a non-numeric value is read from a text file, and a numeric value was expected.
Not sure why.


I just started your app on windows, I can enter numbers, once I type -1 the app outputs some numbers. In some cases it repeats one number forever. So some next pointer, points to its own node.
(2 3 9 -1 triggers this endless output)

Others stay forever in sort.

Keep an eye what happens to the next pointer of nodes that you just sorted. So the new list, does not get any next that redirect in a loop.

Do you use Lazarus or fp.exe as ide?
Both should allow you do debug (single step) your app.

Note: if it does not go into endless output, then the console may close before you can even see the result (depending how you started the app)
« Last Edit: November 12, 2018, 02:22:22 am by Martin_fr »

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9792
  • Debugger - SynEdit - and more
    • wiki
Re: Compiles, but doesn't execute. Problem with lists
« Reply #6 on: November 12, 2018, 02:25:52 am »
Code: Pascal  [Select][+][-]
  1.               if WirdJetztSortiert^.wert < ioRefListe^.wert then
  2.               begin
  3.                     IstSchonSortiert^.next := WirdJetztSortiert^.next;
  4.                     WirdJetztSortiert^.next := ioRefListe;
  5.                     ioRefListe^.next := WirdJetztSortiert  // zeigt auf das NEUE WirdJetztSortiert
  6.   // das NEUE WirdJetztSortiert.next zeigt auf ioRefListe
  7.   // Wenn du danach den next pointern folgst, geht es immer im Kreis
  8.               end;
  9.  
above creates a loop

There may be more, but I am off

medianna

  • Newbie
  • Posts: 4
Re: Compiles, but doesn't execute. Problem with lists
« Reply #7 on: November 12, 2018, 07:09:09 am »
Code: Pascal  [Select][+][-]
  1.               if WirdJetztSortiert^.wert < ioRefListe^.wert then
  2.               begin
  3.                     IstSchonSortiert^.next := WirdJetztSortiert^.next;
  4.                     WirdJetztSortiert^.next := ioRefListe;
  5.                     ioRefListe^.next := WirdJetztSortiert  // zeigt auf das NEUE WirdJetztSortiert
  6.   // das NEUE WirdJetztSortiert.next zeigt auf ioRefListe
  7.   // Wenn du danach den next pointern folgst, geht es immer im Kreis
  8.               end;
  9.  
above creates a loop

There may be more, but I am off

Thanks for nudging me into the right direction. I have managed to make it work :)

 

TinyPortal © 2005-2018