?
Solved

Reading in random integer files

Posted on 2003-03-14
18
Medium Priority
?
200 Views
Last Modified: 2010-04-16
Thanks to help from mimcc I now have my FIFO page replacement simulation, based on keyboard input of integers fully working.  But I’ve now gone astray trying to run it from the random integer files I’ve created and am needing a second rescue.

The 5 random integer files I’m using are RanInt1.dat to RanInt5.dat.  Each contains 50 integers.

I want the program to cycle through the files in turn, reading in the integers and have the program first check for duplicates in the queue.  If no duplicate is found, the integer is added.  If a duplicate is found the First In is removed and then the new integer added to the end of the queue.  I also want the program to display the events on screen and generate a print out.

This latest version compiles, but that’s all.

Could it be a defective gene that has me keep setting myself new tasks too far? :-)

WTA

LRH
--------------------------------------------------
{Unit to simulate First In First Out Page Replacement }

unit FIFOCls9;

interface

uses crt, printer;

type TQptr = ^QRec; {define Q record structure}
QRec = record
     element: integer;
     next: TQptr;
end;

QType = object {define Q structure and methods}
      head, tail : TQptr;
      Qmax, numInQ, Counter: integer;
 
      procedure Init;
      procedure GetData(fName : string);
      procedure RandomInput;
      procedure Add(nData : integer);
      procedure Retrieve;
      procedure DisplayQ;
      procedure Replace(jData : integer);
      procedure Search(item : integer);
     

end;

implementation
var Data : array[1..50] of integer; {Set up array to hold random integers}
    Qmax, n :integer;
    fname, s : string;
    DataList : QType;

{
Method : Used to initialise Q
precondition : Q is defined
postcondition: maxsize of Q, counter, no.in Q and head & tail properties set
}

procedure QType.Init;
begin
     Qmax := 50;
     NumInQ := 0;
     head := nil;
     tail := nil;
     Counter := 0;
end;

{
Method : Used to read random integers from file
precondition : Data index must be integer and > 0 and < maximum size
postcondition: Data accessed and placed in an array for reading into
               page replacement simulation
}

Procedure QType.GetData(fName : string);
var DataFile : file of integer;
    index : integer;
begin
   assign (DataFile, fName);
   reset (DataFile);
   index := 0;
     while not EOF(DataFile) do
     begin
     read (DataFile, Data[index]);
     inc(index);
     end;
   Qmax := index;
   close (DataFile);
end;

{
Method : Used to input random integer files
Pre-Condition : Range is set between 1 and 5
Post-Condition : The program will cycle through the random integer files
}

procedure QType.RandomInput;
begin
n := 1;
   while (n >=1) and (n+1 <= 5) do
   begin
   str (n,s);
   DataList.GetData ('RanInt'+s+'.dat');
   n:= n+1;
   end;
end;

{
Method : Used to ADD data to Q
precondition : Q is initialised, and input data is an integer
postcondition: data added to Q and tail referenced to new element
}

procedure QType.Add(nData : integer);
var i : integer;
newElem : TQptr;
begin
   new(newElem);
   newElem^.element := nData; {set data to new element}
   newElem^.next := nil; {as element will be tail then set to nil}
      if tail <> nil then tail^.next := newElem; {point current tail of Q to new element}
      tail := newElem; {set tail to new element}
         if head = nil then head := tail; {set head to tail if nohing in Q}
         Inc(numInQ);
end;

{
Method : Used to RETRIEVE data from Q
precondition : Q has integer data present
postcondition: head of Q is removed and head moved to next item
}

procedure QType.Retrieve;
var i : integer;
tempQ : TQptr;
begin
   tempQ := head;
   head := head^.next; {point head of Q to next element}
   dispose(tempQ); {release memory}
   Dec(numInQ); {reduce number in Q}
      if numInQ = 0 then tail := nil; {reset tail if no data in Q}
   Inc(Counter);
end;

{
Method : Used to OUTPUT data from Q
precondition : Q has integer data present
postcondition: data output from head onwards.
}

PROCEDURE QType.DisplayQ;
var tempQ : TQptr;
begin
   tempQ:= head;
      while tempQ <> nil do
      begin
      writeln (tempQ^.element);
      writeln (LST, tempQ^.element);
      tempQ := tempQ^.next;
      end
end;

{
Method : Used to REPLACE data in Q
precondition : the data is not in Q
postcondition: head of Q is removed and
             the new data added to Q
}

procedure QType.Replace(jData:integer);
begin
   if NumInQ < Qmax then
   begin
   QType.Add(jData);
   writeln ('Memory not full : Page added : ',jData);
   writeln ('No. of pages in memory : ', NumInQ);
   writeln (LST, 'Memory not full : Page added : ',jData);
   writeln (LST, 'No. of pages in memory : ', NumInQ);
   DisplayQ;
   end
      else
      begin
      QType.Retrieve;
      writeln('Memory full : Page removed');
      writeln ('No. of pages in memory : ', NumInQ);
      writeln(LST, 'Memory full : Page removed');
      writeln (LST, 'No. of pages in memory : ', NumInQ);
      QType.Add(jData);
      writeln('New page added : ',jData);
      writeln ('No. of pages in memory : ', NumInQ);
      writeln(LST, 'New page added : ',jData);
      writeln (LST, 'No. of pages in memory : ', NumInQ);
      DisplayQ;
      end;
writeln('Current page faults = ', counter);
writeln;
writeln(LST, 'Current page faults = ', counter);
writeln(LST, '');
end;

{
Method : Used to SEARCH for data
precondition : Q must be initialised and item to search for must be an integer
postcondition: if item found, confirmation message is displayed
               if item not found, Replace procedure intiated
}

Procedure QType.Search(item : integer);
Var
  i : integer;
  Found : boolean;
  currPos : TQptr;
begin
RandomInput;
for i := 1 to Qmax do
begin
read (data[i]);
Found := false;
currPos := head;
while (currPos <> nil) and (Not Found) do
   begin
      if item = currPos^.element then
         Found := true
      else
         currPos := currPos^.next;
   end;
   if found then
   begin
     writeln ('Page found : No change');
     writeln;
     writeln (LST, 'Page found : No change');
     writeln (LST, '');
   end
   else
      Replace(item);
   end;
end;
end.
------------------------------------------------------
{Program which simulates First In First Out Page Replacement}

program FIFOprg9;

uses crt, printer, FIFOcls9;

CONST LST = '';

var Q1 : QType;
    item : integer;

  begin
  clrscr;

  Q1.Init;

  writeln (LST, '');
  writeln (LST, 'PROGRAM TO SIMULATE FIRST IN FIRST OUT PAGE REPLACEMENT');
  writeln (LST, '=======================================================');
  writeln (LST, '');
  writeln;
  writeln ('PROGRAM TO SIMULATE FIRST IN FIRST OUT PAGE REPLACEMENT');
  writeln ('=======================================================');
  writeln;

  Q1.Search(item);
 
  writeln('Press return to exit');
  readln;
  writeln (LST, #12);
  read;

end.  
0
Comment
Question by:LittleRedHat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 5
  • 2
18 Comments
 
LVL 49

Expert Comment

by:dbrunton
ID: 8137044
Is this procedure correct?


procedure QType.RandomInput;
begin
    n := 1;
    while (n >=1) and (n+1 <= 5) do
    begin
    str (n,s);
     DataList.GetData ('RanInt'+s+'.dat');
     n:= n+1;
     end;
end;

Each time you call it it starts off with the value of n = 1.  The n never changes.  Possibly what you is.
This will give you a random file to open.

procedure QType.RandomInput;
var
  n : integer;
begin
    n := Random(4) + 1;
    str (n, s);
     DataList.GetData ('RanInt'+s+'.dat');
end;
0
 

Author Comment

by:LittleRedHat
ID: 8137602
Thanks for your reply.  I was trying to base this on the working procedure below that I used in an earlier program ... where the procedure automatically read through a series of files that incremented by a factor of 2, i.e. Ran1000, Ran2000, Ran4000 etc.

begin
 n := 1000;
   while (n >=1000) AND (n*2 <=32000) do
   begin
     str(n,s);
     DataList.GetData('Ran'+s+'.dat');
     Time.StartTimer;
     DataList.Sort;
     Time.EndTimer;
     writeln (+s+' random integers took ', Time.TimeDiff, ' centi-seconds to sort');
     writeln;
     n := n*2;

As the files I'm trying to read for this program are RanInt1, RanInt2, RanInt3, etc.  I was hoping my code of n+1 would iteratively increment and read the next file until RanInt5 had been read.

It seems like either the program doesn't read/find the data, or if it does it then doesn't read it into the search procedure.  I've been through my code countless times, have tried lots of permutaions, but obviously am either not changing the right parts or if I am, am not changing them to what is needed.

Regards.

LRH
0
 
LVL 49

Expert Comment

by:dbrunton
ID: 8137759
Hmm.  OK.  Misread your code.

Try dropping lot's of writeln statements into your code such as.

procedure QType.RandomInput;
              begin
                 n := 1;
                 while (n >=1) and (n+1 <= 5) do
                 begin
                 str (n,s);
Writeln(s);
                  DataList.GetData ('RanInt'+s+'.dat');
                  n:= n+1;
                  end;
              end;

Procedure QType.GetData(fName : string);
              var DataFile : file of integer;
                 index : integer;
              begin
                assign (DataFile, fName);
                reset (DataFile);
                index := 0;
                  while not EOF(DataFile) do
                  begin
                  read (DataFile, Data[index]);
                  inc(index);
Writeln(index);
                  end;
                Qmax := index;
                close (DataFile);
              end;

This will show you what is happening and see if it is going into an endless loop.
0
Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

 

Author Comment

by:LittleRedHat
ID: 8140109
I've tried what you suggest, but when I try to run the program I still get nothing other than the FIFO title line ... I presume I have errors in there too.

I don't know if this version is the best approach.  My original idea was to have the program run from the RanodomInput procedure and "pull in" the search procedure from there ...

procedure QType.RandomInput;
begin
n := 1;
   while (n >=1) and (n+1 <= 6) do
   begin
   str (n,s);
   DataList.GetData ('RanInt'+s+'.dat');
   Search(???);
   end;

... but lost my way at Search (???)

With thanks again.

LRH

0
 

Author Comment

by:LittleRedHat
ID: 8140141
Sorry the 4th line should read
while (n >=1) and (n+1 <= 5) do

LRH
0
 

Author Comment

by:LittleRedHat
ID: 8140951
Ive had another attempt to resolve my problem.  Its not right, but at least it now produces something! .....

Memory not full : page added : 21448 { writeln ('Memory not full : Page added : ',jData); / Replace procedure}
 
21448  {writeln ('No. of pages in memory : ', NumInQ);/ Replace procedure}

Current page faults = 1 { Should be zero in the first instance/ writeln('Current page faults = ', counter);/Replace procedure}

Then forever and ever  Page Found : No change { writeln ('Page found : No change');/Search Procedure}
--------------------------------------------------------
{Unit to simulate First In First Out Page Replacement }

unit FIFOClsA;

interface

uses crt, printer;

type TQptr = ^QRec; {define Q record structure}
     QRec = record
     element: integer;
     next: TQptr;
end;

QType = object {define Q structure and methods}
        head, tail : TQptr;
        Qmax, numInQ, Counter : integer;
 
      procedure Init;
      procedure GetData(fName : string);
      procedure Add(nData : integer);
      procedure Retrieve;
      procedure DisplayQ;
      procedure Replace(jData : integer);
      procedure Search (item : integer);
      procedure RandomInput;

end;

implementation
var Data : array[1..50] of integer; {Set up array to hold random integers}
    Qmax, n :integer;
    fname, s : string;
    DataList : QType;

{
Method : Used to initialise Q
precondition : Q is defined
postcondition: maxsize of Q, num in Q and head & tail properties set
}

procedure QType.Init; {Set maximum Q size}
begin
     Qmax := 50;
     NumInQ := 0;
     head := nil;
     tail := nil;
     Counter := 0;
end;

{
Method : Used to read random integers from file
precondition : Data index must be integer and > 0 and < maximum size
postcondition: Data accessed and placed in an array for reading into
               page replacement simulation
}

Procedure QType.GetData(fName : string);
var DataFile : file of integer;
    index, i : integer;
begin
   assign (DataFile, fName);
   reset (DataFile);
   index := 0;
     while not EOF(DataFile) do
     begin
     read (DataFile, Data[index]);
     inc(index);
     end;
   Qmax := index;
   close (DataFile);
end;

{
Method : Used to ADD data to Q
precondition : Q is initialised, and input data is an integer
postcondition: data added to Q and tail referenced to new element
}

procedure QType.Add(nData : integer);
var i : integer;
newElem : TQptr;
begin
   new(newElem);
   newElem^.element := nData; {set data to new element}
   newElem^.next := nil; {as element will be tail then set to nil}
      if tail <> nil then tail^.next := newElem; {point current tail of Q to new element}
      tail := newElem; {set tail to new element}
         if head = nil then head := tail; {set head to tail if nohing in Q}
         Inc(numInQ);
end;

{
Method : Used to RETRIEVE data from Q
precondition : Q has integer data present
postcondition: head of Q is removed and head moved to next item
}

procedure QType.Retrieve;
var i : integer;
tempQ : TQptr;
begin
   tempQ := head;
   head := head^.next; {point head of Q to next element}
   dispose(tempQ); {release memory}
   Dec(numInQ); {reduce number in Q}
      if numInq = 0 then tail := nil; {reset tail if no data in Q}
   Inc(Counter);
end;

{
Method : Used to OUTPUT data from Q
precondition : Q has integer data present
postcondition: data output from head onwards.
}

PROCEDURE QType.DisplayQ;
var tempQ : TQptr;
begin
   tempQ:= head;
      while tempQ <> nil do
      begin
      writeln (tempQ^.element);
      writeln (LST, tempQ^.element);
      tempQ := tempQ^.next;
      end
end;

{
Method : Used to REPLACE data in Q
precondition : the data is not in Q
postcondition: head of Q is removed and
             the new data added to Q
}

procedure QType.Replace(jData:integer);
begin
   if NumInQ < Qmax then
   begin
   QType.Add(jData);
   writeln ('Memory not full : Page added : ',jData);
   writeln ('No. of pages in memory : ', NumInQ);
   writeln (LST, 'Memory not full : Page added : ',jData);
   writeln (LST, 'No. of pages in memory : ', NumInQ);
   DisplayQ;
   end
      else
      begin
      QType.Retrieve;
      writeln('Memory full : Page removed');
      writeln ('No. of pages in memory : ', NumInQ);
      writeln(LST, 'Memory full : Page removed');
      writeln (LST, 'No. of pages in memory : ', NumInQ);
      QType.Add(jData);
      writeln('New page added : ',jData);
      writeln ('No. of pages in memory : ', NumInQ);
      writeln(LST, 'New page added : ',jData);
      writeln (LST, 'No. of pages in memory : ', NumInQ);
      DisplayQ;
      end;
writeln('Current page faults = ', counter);
writeln;
writeln(LST, 'Current page faults = ', counter);
writeln(LST, '');
end;

{
Method : Used to SEARCH for data
precondition : Q must be initialised and item to search for must be an integer
postcondition: if item found, confirmation message is displayed
               if item not found, Replace procedure intiated
}

Procedure QType.Search(item : integer);
Var
  Found : boolean;
  currPos : TQptr;
  n : integer;
begin
  Found := false;
  currPos := head;
      while (currPos <> nil) and (Not Found) do
      begin
        if item = currPos^.element then
          Found := true
        else
          currPos := currPos^.next;
        end;
   if found then
   begin
     writeln ('Page found : No change');
     writeln;
     writeln (LST, 'Page found : No change');
     writeln (LST, '');
   end
   else
      Replace(item);
end;

{
Method : Used to input random integer files
Pre-Condition : Range is set between 1 and 5
Post-Condition : The results of the FIFO page replacement simulation
                 will be displayed
}

procedure QType.RandomInput;
var rData : integer;
begin
n := 1;
   while (n >=1) and (n+1 <= 6) do
   begin
   str (n,s);
   DataList.GetData ('RanInt'+s+'.dat');
   Search(rData);
   end;

end;

end.
-----------------------------------------------------
{Program which simulates First In First Out Page Replacement}

program FIFOprgA;

uses crt, printer, FIFOclsA;

CONST LST = '';

var DataList : QType;

  begin
  CLRSCR;
  DataList.Init;
  writeln (LST, '');
  writeln (LST, 'PROGRAM TO SIMULATE FIRST IN FIRST OUT PAGE REPLACEMENT');
  writeln (LST, '=======================================================');
  writeln (LST, '');
  writeln;
  writeln ('PROGRAM TO SIMULATE FIRST IN FIRST OUT PAGE REPLACEMENT');
  writeln ('=======================================================');
  writeln;

  DataList.RandomInput;

  writeln('Press return to exit');
  readln;
  writeln (LST, #12);
  read;
end.
0
 

Author Comment

by:LittleRedHat
ID: 8141046
Hope all the postings aren't a problem ... I'm trying to put in my share of work on this, but suffering from new thoughts and after thoughts.  This time I forgot to include the extra writelns.  Sorry! I have now and the screen display runs from 1-50, then gives the Page Found : no change message and starts all over again. New thoughts to mull over. : -)

Thanks a lot for that suggestion. I'd included all my writelns to keep a check on progress, but never thought of using them where you suggested.

LRH
0
 
LVL 49

Expert Comment

by:dbrunton
ID: 8141104
Probably can't get into it for about another 24 hrs due to work committments.

But writelns are the best debugging tool I found in Pascal.  Items like

Writeln('Position ZZ in code');  

give you an idea of where the code is executing.
0
 

Author Comment

by:LittleRedHat
ID: 8141138
No problems.  Thanks for helping.  I'll keep thinking and trying more writelns. :-)

Hope work goes well.

LRH
0
 

Author Comment

by:LittleRedHat
ID: 8145245
Hi!

I dont want to waste your time, so my latest progress report   

I now have the data reading directly into the program and displaying, but havent got it running with the Search procedure.  Using your writeln suggestion :- ) this is what is happening 

writeln('DataFile',s);/RandomInput procedure

The 50 integers of  RanInt1 are displayed

Then writeln('start search procedure');/search procedure --- but only produces

Memory not full : page added : 21448 { writeln ('Memory not full : Page added : ',jData); / Replace procedure}
 
21448  {writeln ('No. of pages in memory : ', NumInQ);/ Replace procedure}

Current page faults = 0 { writeln('Current page faults = ', counter);/Replace procedure}

Then writeln('pull in search');/RandomInput procedure

Immediately followed by RanInt2

Repeated until all 5 RanInt files have been read.
-------------------------------------------------
{Unit to simulate First In First Out Page Replacement }

unit FIFOClsC;

interface

uses crt, printer;

type TQptr = ^QRec; {define Q record structure}
     QRec = record
     element: integer;
     next: TQptr;
end;

QType = object {define Q structure and methods}
        head, tail : TQptr;
        Qmax, numInQ, Counter : integer;
 
      procedure Init;
      procedure GetData(fName : string);
      procedure Add(nData : integer);
      procedure Retrieve;
      procedure DisplayQ;
      procedure Replace(jData : integer);
      procedure Search (item : integer);
      procedure RandomInput;

end;

implementation
var {Qmax,} n :integer;
    {fileName,} s : string;
    DataList : QType;

{
Method : Used to initialise Q
precondition : Q is defined
postcondition: maxsize of Q, num in Q and head & tail properties set
}

procedure QType.Init; {Set maximum Q size}
begin
     Qmax := 10;
     NumInQ := 0;
     head := nil;
     tail := nil;
     Counter := 0;
end;

{
Method : Used to read random integers from file
precondition : Data index must be integer and > 0 and < maximum size
postcondition: Data accessed for reading into page replacement simulation
}

Procedure QType.GetData(fName : string);
var DataFile : file of integer;
    data, i : integer;
begin
Assign(DataFile, fname);
  Reset(DataFile);
     For i:= 1 to Qmax do;
        While not eof(DataFile) Do
         Begin
         Read(DataFile,Data);
          writeln(Data);
          readln;
          Writeln (LST, data);
      End;
  close (DataFile);
end;

{
Method : Used to ADD data to Q
precondition : Q is initialised, and input data is an integer
postcondition: data added to Q and tail referenced to new element
}

procedure QType.Add(nData : integer);
var i : integer;
newElem : TQptr;
begin
   new(newElem);
   newElem^.element := nData; {set data to new element}
   newElem^.next := nil; {as element will be tail then set to nil}
      if tail <> nil then tail^.next := newElem; {point current tail of Q to new element}
      tail := newElem; {set tail to new element}
         if head = nil then head := tail; {set head to tail if nohing in Q}
         Inc(numInQ);
end;

{
Method : Used to RETRIEVE data from Q
precondition : Q has integer data present
postcondition: head of Q is removed and head moved to next item
}

procedure QType.Retrieve;
var i : integer;
tempQ : TQptr;
begin
   tempQ := head;
   head := head^.next; {point head of Q to next element}
   dispose(tempQ); {release memory}
   Dec(numInQ); {reduce number in Q}
      if numInq = 0 then tail := nil; {reset tail if no data in Q}
   Inc(Counter);
end;

{
Method : Used to OUTPUT data from Q
precondition : Q has integer data present
postcondition: data output from head onwards.
}

PROCEDURE QType.DisplayQ;
var tempQ : TQptr;
begin
   tempQ:= head;
      while tempQ <> nil do
      begin
      writeln (tempQ^.element);
      writeln (LST, tempQ^.element);
      tempQ := tempQ^.next;
      end
end;

{
Method : Used to REPLACE data in Q
precondition : the data is not in Q
postcondition: head of Q is removed and
             the new data added to Q
}

procedure QType.Replace(jData:integer);
begin
   if NumInQ < Qmax then
   begin
   QType.Add(jData);
   writeln ('Memory not full : Page added : ',jData);
   writeln ('No. of pages in memory : ', NumInQ);
   writeln (LST, 'Memory not full : Page added : ',jData);
   writeln (LST, 'No. of pages in memory : ', NumInQ);
   DisplayQ;
   end
      else
      begin
      QType.Retrieve;
      writeln('Memory full : Page removed');
      writeln ('No. of pages in memory : ', NumInQ);
      writeln(LST, 'Memory full : Page removed');
      writeln (LST, 'No. of pages in memory : ', NumInQ);
      QType.Add(jData);
      writeln('New page added : ',jData);
      writeln ('No. of pages in memory : ', NumInQ);
      writeln(LST, 'New page added : ',jData);
      writeln (LST, 'No. of pages in memory : ', NumInQ);
      DisplayQ;
      end;
writeln('Current page faults = ', counter);
writeln;
writeln(LST, 'Current page faults = ', counter);
writeln(LST, '');
end;

{
Method : Used to SEARCH for data
precondition : Q must be initialised and item to search for must be an integer
postcondition: if item found, confirmation message is displayed
               if item not found, Replace procedure intiated
}

Procedure QType.Search(item : integer);
Var
  Found : boolean;
  currPos : TQptr;
  n : integer;
begin
  Found := false;
  currPos := head;
  writeln('start search procedure');
      while (currPos <> nil) and (Not Found) do
      begin
        if item = currPos^.element then
          Found := true
        else
          currPos := currPos^.next;
      end;
       if found then
   begin
     writeln ('Page found : No change');
     writeln;
     writeln (LST, 'Page found : No change');
     writeln (LST, '');
   end
   else
      Replace(item);
end;

{
Method : Used to input random integer files
Pre-Condition : Range is set between 1 and 5
Post-Condition : The results of the FIFO page replacement simulation
                 will be displayed
}

procedure QType.RandomInput;
var Data : integer;
begin
n := 1;
   while (n >=1) and (n+1 <= 6) do
   begin
   str (n,s);
writeln('DataFile',s);
   DataList.GetData ('RanInt'+s+'.dat');
   Search(Data);
   writeln('pull in search');
   n:= n+1;
   end;

  end;

end.
0
 
LVL 49

Expert Comment

by:dbrunton
ID: 8145850
Post code plus 5 data files to the following

db   at   kcbbs.gen.nz


This here

Procedure QType.Search(item : integer);
              Var
               Found : boolean;
               currPos : TQptr;
               n : integer;
              begin
               Found := false;
               currPos := head;
               writeln('start search procedure');
 ***                  while (currPos <> nil) and (Not Found) do    ***

shouldn't it be

while (currPos <> nil) or (Not Found) do

otherwise it's going to go into an endless loop.  You need to bail out if you hit a nil pointer which you
are not doing at the moment.

                   begin
                     if item = currPos^.element then
                       Found := true
                     else
                       currPos := currPos^.next;
                   end;
                    if found then
                begin
                  writeln ('Page found : No change');
                  writeln;
                  writeln (LST, 'Page found : No change');
                  writeln (LST, '');
                end
                else
                   Replace(item);
              end;


Look at the code above.  Test and still problems then  post code plus 5 data files to the following

db   at   kcbbs.gen.nz
0
 

Author Comment

by:LittleRedHat
ID: 8146181
Afraid that didn't resolve, so have taken up your kind offer to email.

With thanks.

LRH
0
 

Author Comment

by:LittleRedHat
ID: 8162237
Apols for messy, not yet tidied code … wanted to let you know asap that you can now “bin” and forget the files I e-mailed as I’ve now resolved the problem other than for a couple of minor amendments. :-)))  Thanks so much for all your endeavours.

If I may ask one more favour.  Please could you advise me what the protocol is for closing the question under these circumstances.  I certainly want points to go to you for giving your time and for that really useful writeln tip, which I can see myself making great use of. :-)

With best regards  

LRH
-------------------------------------------------
Procedure QType.GetData(fName : string);
var DataFile : file of integer;
    data, i, j : integer;
begin
Assign(DataFile, fname);
  Reset(DataFile);
  For i:= 1 to Qmax do;
  j:=1;
    While not eof(DataFile) Do
      Begin
      Read(DataFile,Data);
            fileArray[j]:=Data;
            Inc(j);
        {With DataFile Do}
          {Begin}
          writeln(Data);
          readln;
          Writeln (LST, data);
          {End;}
      End;
   close (DataFile);
end;
--------------------------------------------------------
Procedure QType.Search(item : integer);
Var
  Found : boolean;
  currPos : TQptr;
  n : integer;
begin
  Found := false;
  currPos := head;
  writeln('start search procedure');
      while (currPos <> nil) and (Not Found) do
      begin
        if item = currPos^.element then
          Found := true
        else
          currPos := currPos^.next;
        end;
   if found then
   begin
     writeln ('Page found : No change');
     writeln;
     writeln (LST, 'Page found : No change');
     writeln (LST, '');
   end
   else
      Replace(item);
end;
-------------------------------------------------------
procedure QType.RandomInput;
var i : integer;
begin
n := 1;
   while (n >=1) and (n+1 <= 6) do
   begin
   str (n,s);
writeln('DataFile',s);
   DataList.GetData ('RanInt'+s+'.dat');
   for i:= 1 to 50 do
   begin
        Search(fileArray[i]);
   end;
   writeln('pull in search');
   n:= n+1;
   end;
end;

0
 

Author Comment

by:LittleRedHat
ID: 8213991
Having solved the problem I'm now asking for this question to be closed.  In case anyone else may find it of help, my fully working program ...

LRH

{Unit to simulate First In First Out Page Replacement }

unit FIFOClsR;

interface

uses crt, printer;

type TQptr = ^QRec; {define Q record structure}
     QRec = record
     element: integer;
     next: TQptr;
end;

QType = object {define Q structure and methods}
        head, tail : TQptr;
        Qmax, numInQ, Counter : integer;
 
        procedure Init;
        procedure GetData(fName : string);
        procedure Add(nData : integer);
        procedure Retrieve;
        procedure DisplayQ;
        procedure Replace(jData : integer);
        procedure Search (item : integer);
        procedure RandomInput;

end;

implementation
var n :integer;
    s : string;
    DataList : QType;
    dataArray : array[1..50] of integer;

{
Method : Used to initialise Q
precondition : Q is defined
postcondition: maxsize of Q, num in Q and head & tail properties set
}

procedure QType.Init;
begin
     Qmax := 10;
     NumInQ := 0;
     head := nil;
     tail := nil;
     Counter := 0;
end;

{
Method : Used to read random integers from file
precondition : Data index must be integer and > 0 and < maximum size
postcondition: Data accessed for reading into page replacement simulation
}

Procedure QType.GetData(fName : string);
var DataFile : file of integer;
    data, i, j : integer;
begin
Assign(DataFile, fname);
Reset(DataFile);
   for i:= 1 to Qmax do;
   j:=1;
      while not eof(DataFile) do
      begin
      read(DataFile,Data);
      dataArray[j]:=Data;
      Inc(j);
      write(Data);
      readln;
      end;
Close (DataFile);
end;

{
Method : Used to ADD data to Q
precondition : Q is initialised, and input data is an integer
postcondition: data added to Q and tail referenced to new element
}

procedure QType.Add(nData : integer);
var i : integer;
newElem : TQptr;
begin
   new(newElem);
   newElem^.element := nData; {set data to new element}
   newElem^.next := nil; {as element will be tail then set to nil}
      if tail <> nil then tail^.next := newElem; {point current tail of Q to new element}
      tail := newElem; {set tail to new element}
         if head = nil then head := tail; {set head to tail if nohing in Q}
         Inc(numInQ);
end;

{
Method : Used to RETRIEVE data from Q
precondition : Q has integer data present
postcondition: head of Q is removed and head moved to next item
}

procedure QType.Retrieve;
var i : integer;
tempQ : TQptr;
begin
   tempQ := head;
   head := head^.next; {point head of Q to next element}
   dispose(tempQ); {release memory}
   Dec(numInQ); {reduce number in Q}
      if numInq = 0 then tail := nil; {reset tail if no data in Q}
   Inc(Counter);
end;

{
Method : Used to OUTPUT data from Q
precondition : Q has integer data present
postcondition: data output from head onwards.
}

PROCEDURE QType.DisplayQ;
var tempQ : TQptr;
begin
   tempQ:= head;
      while tempQ <> nil do
      begin
      writeln (tempQ^.element);
      write (LST, tempQ^.element, ',');
      tempQ := tempQ^.next;
      end;
writeln(LST,'');
end;

{
Method : Used to REPLACE data in Q
precondition : the data is not in Q
postcondition: head of Q is removed and
             the new data added to Q
}

procedure QType.Replace(jData:integer);
begin
   if NumInQ < Qmax then
   begin
   QType.Add(jData);
   writeln ('Memory not full : Page added = ',jData);
   writeln ('No. of pages in memory : ', NumInQ);
   writeln (LST, 'Memory not full : Page added = ', jData);
   DisplayQ;
   end
      else
      begin
      QType.Retrieve;
      writeln('Memory full : FIRST IN page removed');
      writeln ('No. of pages in memory = ', NumInQ);
      writeln(LST, 'Memory full : FIRST IN page removed');
      QType.Add(jData);
      writeln('New page added = ',jData);
      writeln ('No. of pages in memory = ', NumInQ);
      writeln(LST, 'New page added = ',jData);
      DisplayQ;
      end;
writeln;
writeln('CURRENT PAGE FAULTS = ', counter);
writeln;
readln;
writeln(LST, 'CURRENT PAGE FAULTS = ', counter);
writeln(LST,'');
end;

{
Method : Used to SEARCH for data
precondition : Q must be initialised and item to search for must be an integer
postcondition: if item found, confirmation message is displayed
               if item not found, Replace procedure intiated
}

Procedure QType.Search(item : integer);
Var
   Found : boolean;
   currPos : TQptr;
   n : integer;
begin
   Found := false;
   currPos := head;
      while (currPos <> nil) and (Not Found) do
      begin
         if item = currPos^.element then
         Found := true
            else
            currPos := currPos^.next;
        end;
      if found then
      begin
      writeln ('Page found = ',item,' : No change');
      writeln;
      writeln (LST, 'Page found = ',item,' : No change');
      writeln (LST, '');
      end
         else
         Replace(item);
end;

{
Method : Used to input random integer files
Pre-Condition : Range is set between 1 and 5
Post-Condition : The results of the FIFO page replacement simulation
                 will be displayed
}

procedure QType.RandomInput;
var i : integer;
begin
n := 1;
   while (n >=1) and (n+1 <= 6) do
   begin
   str (n,s);
   writeln('DataFile',s);
   writeln(LST, 'RANDOM INTEGER FILE NO: ',s);
   writeln(LST, '-------------------------');
   writeln(LST, '');
   DataList.GetData ('RanInt'+s+'.dat');
      for i:= 1 to 50 do
      begin
      Search(dataArray[i]);
      end;
   n:= n+1;
   Init;
   end;
end;

end.
-----------------------------------------------------
{Program which simulates First In First Out Page Replacement}

program FIFOprgR;

uses crt, printer, FIFOclsR;

{CONST LST = '';}

var DataList : QType;

  begin
  CLRSCR;
  DataList.Init;

  writeln (LST, '');
  writeln (LST, '');
  writeln (LST, 'PROGRAM TO SIMULATE FIRST IN FIRST OUT PAGE REPLACEMENT');
  writeln (LST, '=======================================================');
  writeln (LST, '');
  writeln;
  writeln ('PROGRAM TO SIMULATE FIRST IN FIRST OUT PAGE REPLACEMENT');
  writeln ('=======================================================');
  writeln;

  DataList.RandomInput;

  writeln('Press return to exit');
  readln;
  writeln (LST, #12);
  read;
end.
0
 
LVL 1

Expert Comment

by:Computer101
ID: 8214164
A request to refund points and make a seperate question for dbrunton has been made.  If no objections, I will handle

Computer101
E-E Admin
0
 
LVL 49

Expert Comment

by:dbrunton
ID: 8215482
No objections
0
 

Author Comment

by:LittleRedHat
ID: 8216899
Thanks.

As promised, have posted points for you ....

http://www.experts-exchange.com/Programming/Programming_Languages/Pascal/Q_20564503.html

Hope they're ok.

LRH
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 8221373
Points refunded and placed in PAQ

Computer101
E-E Admin
0

Featured Post

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

New style of hardware planning for Microsoft Exchange server.
Hey fellow admins! This time, I have a little fairy tale for you. As many tales do, it starts boring and then gets pretty gory. I hope you like it. TL;DR: It is about an important security matter, you should read it if you run or administer Windows …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

762 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