Hello! I'm new to the DaniWeb, so I'm not sure what I'm allowed to ask, and I don't have enough time to search for all rules right now. I have to write a program for tomorrow, otherwise I won't be able to pass the year. It's a bit difficult to explain the organization with professors, so I got to write a program, before I even had a chance to learn the entire Pascal syntax - my prof. didn't teach my class everything so he will, but the day after I need this.

I have to write two programs in Turbo Pascal:
1. The first program should create a binary file and than write to it. It should read two values from a keyboard, the first representing the x coordinate, and the second representing the y coordinate of a point in a plane, and than write that to the file.
2. The second program should read from that binary file, and put its values into a single linked list, in the same order as it was in the file. After that program should remove the coordinates of the farthest point from the point (x=0 y=0), and remove linked list from memory.

First of all, I'm not quite sure how to create a binary file. I was thinking to do that the same way I would do for text files. Here's what I've done:
1st program:

PROGRAM create;

TYPE
    point = RECORD
          x: real;
          y: real;
    END;

VAR
   output_file: FILE OF point;
   number, i: integer;
   ordinate, abscissa: point;

BEGIN
     assign (output_file, 'out.dat');
     rewrite (output_file);
     write (output, 'Enter a number of points in a plane: ');
     readln (input, number);
     FOR i := 1 TO number DO BEGIN
         write (output, 'Enter a coordinate of the ', i, '. point (x, y): ');
         readln (input, ordinate.x, abscissa.y);
         write (output_file, ordinate);
         write (output_file, abscissa);
     END;
     close (output_file);
END.

Is the above code all right? Did I miss something?

And About the 2nd program, I don't know how to call the file out.dat and to read it's content.
I've tried something like this:

(** TYPE - The same as in first file **)
PROCEDURE call_file(input_file: FILE OF point);
VAR 
      ordinate, abscissa: point;
BEGIN
         assign (input_file, 'out.dat');
         reset (input_file);
(**  Because I don't know how to create single list, I tried just to read the data **)
         WHILE NOT eof DO BEGIN
                   read (input_file, ordinate.x);  (** line problem **)
                   read (input_file, abscissa.y);
         END;        
END;

When I used TP's debugger, program stops working in the line marked as line problem, but after it reads all values from the binary file - I get error message "Error 100 - Disk read error".

After that single linked list is another problem. It looks very confusing to me, and if anyone could type me those subprograms (procedures) I would appreciate very much. I'm trying to write and learn this for 5 whole days, and no luck.
Please help me! This means a lot to me.

Recommended Answers

All 3 Replies

You've actually hit on the right idea. You've just made a couple of simple mistakes.

Firstly, you have a full point object

TYPE
    point = RECORD
          x: real;  { This is the abscissa }
          y: real;  { This is the ordinate }
    END;

Remember, the word "abscissa" means the same as the x-coordinate; likewise "ordinate" means the y-coordinate.

You have opened the file correctly, and your file I/O is technically correct, but your confusion over the coordinate pairs has tripped you up. Try:

var
    output_file:         file of point;
    a_point:             point;
    number_of_points, i: integer;

begin
    ...
    for i := 1 to number_of_points do begin
        writeln( 'Enter the ', i, 'th coordinate as: X Y' );
        readln( a_point.x, a_point.y );
        write( output_file, a_point )
    end;
    ...
end.

Reading a point from file is just the opposite:

read( input_file, a_point );
writeln( 'The point just read is (', a_point.x, ',', a_point.y, ')' );

A linked list is always of the form:

type
    pNode = ^tNode;
    tNode = record
        ...           { my data goes here }
        next: pNode   { the next node in the list, or nil }
    end;

var
    head_node: pNode;
    { The head node always points to the very beginning of the linked list. }
    { If there aren't any nodes, then it should have the value: nil }

I recommend that you also have some functions which help you add (and/or append) and remove nodes from the list.

Hope this helps.

Thanks for reply. Meanwhile, I understood linked lists, and I'm writing a second program right now. I'll post the entire code when/if I finish it.

EDIT: Sorry, I accidentally done this, thinking the post wasn't submitted - I didn't reloaded the page.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.