PASCAL Program need help

I have a Pascal Program that I want to compile. Im not sure how to compile it to test to make sure that it works. I will paste it in here.

This is what it is supposed to do:

Write a program in each of the selected languages that:
A. Queries the user for the name of a file containing up to 100 integers.
B. reads the integers from the file.
C. Runs (and preferably times) either an insertion sort or a selection sort, with results displayed or written to a file.

D. runs (and preferably times) a quick sort. Calls to an internal sort are not permitted except in APL.


File sort1.doc
program Sort_Sort;

uses dos; {DOS unit for reading the time }

{This program reads the integer numbers from a
file and compares some wellknown sorting metods.}

const Max_num=100; { number of integers in the file}

type f_int=file of integer; {file type}

arr_int=array[1..Max_num] of integer;

var n:integer;

Start_hour, Start_min, Start_sek, Start_ms:word;

End_hour, End_min, End_sek, End_ms:word;

ch:char;

f_name:string[20];

f1, f2:f_int;

a:arr_int;













procedure Create_File(var f1:f_int);

{Create new file.}

var d:integer;

begin

assign(f1, f_name);

rewrite(f1);

writeln('Enter the numbers>');

while not eof do

begin

read(d);

write(f1, d)

end;

close(f1)
end;














procedure read_file(var f1:f_int);

{Read integers from the file and put them in an array.}

var curr_el:integer;

begin

assign(f1, f_name);

reset(f1);

n:=0;

while not eof(f1) do

begin

n:=n+1;

read(f1,curr_el);

a[n]:=curr_el

end;

close(f1)

end;











procedure Selection_Sort( var a:arr_int; N : integer);

{Sort array using selection.}

var i, j, min, min_el: integer;

begin

for i := 1 to N-1 do
begin

{Elements from 1 to i-1 are in the correct place}

min := i;
for j := i+1 to N do
if a[j] < a[min] then min := j;

min_el:=a[min]; a[min]:=a[i]; a[i]:=min_el;

{Elements from 1 to i are in the correct place}

end;

end;
















procedure Insertion_Sort( var a : arr_int; N : integer);

{Sort array using insertion.}

var i, j, v : integer;

begin

for i := 2 to N do
begin

{Elements from 1 to i-1 are in the correct place}

v := a[i];
j := i;

while (j > 1) AND (a[j-1] > v) do
begin
a[j] := a[j-1];
j := j-1
end;

a[j] := v;

{Elements from 1 to i are in the correct place}

end;

end;











procedure Quick_Sort(VAR X: arr_int; N: Integer);

{ Sort array using a recursive quick sort.}

PROCEDURE Qsort(VAR X: arr_int; M, N: Integer);
VAR I, J: Integer;

PROCEDURE Partit (VAR A: arr_int; VAR I, J: Integer; Left, Right: Integer);

VAR Pivot, Swap: integer;

BEGIN
Pivot := A[(Left + Right) DIV 2];
I := Left;
J := Right;
WHILE I <= J DO
BEGIN
WHILE A[I] < Pivot DO
I := I + 1;
WHILE Pivot < A[J] DO
J := J - 1;
IF I <= J THEN
BEGIN
Swap:=A[I]; A[I]:=A[J]; A[J]:= Swap;
I := I + 1;
J := J - 1
END
END { WHILE }
END; { Partit }
BEGIN { Qsort }
IF M < N THEN
BEGIN
Partit(X, I, J, M, N); { divide in two }
Qsort(X, M, J); { Sort left part }
Qsort(X, I, N) { Sort right part }
END
END; { Qsort }
BEGIN { Sort }
Qsort(X, 1, N)
END; { Sort }
procedure Arr_to_Screen(a:arr_int; n:integer);

{Output sorted integer on the screen.}

var i:integer;

begin
for i:=1 to n do
if i mod 5 = 0 then writeln(a[i]:6)
else write(a[i]:6);
writeln;
end;




























procedure Arr_to_File(var f:f_int; a:arr_int; n:integer);

{Saves sorted integers in a file.}

var i:integer;

begin

assign(f, 'Sorted_File');

rewrite(f);

for i:=1 to n do
write(f,a[i]);

close(f)

end;






















begin

writeln('Enter file''s name>');

readln(f_name);

writeln('Does the file exist Y/N?');

readln(ch);

if (ch='n') or (ch='N') then create_file(f1);

read_file(f1);

gettime(Start_hour, Start_min, Start_sek, Start_ms);

Selection_Sort(a,n);

gettime(End_hour, End_min, End_sek, End_ms);

write('Selection_Sort Run Time for ', n, ' numbers is ');
write(End_hour-Start_hour:3, End_min-Start_min:3);
writeln(End_sek-Start_sek:3, End_ms-Start_ms:3);

Arr_to_Screen(a,n);
Arr_to_file(f2, a, n);

read_file(f1);

gettime(Start_hour, Start_min, Start_sek, Start_ms);

Insertion_Sort(a,n);

gettime(End_hour, End_min, End_sek, End_ms);

write('Insertion_Sort Run Time for ', n, ' numbers is ');
write(End_hour-Start_hour:3, End_min-Start_min:3);
writeln(End_sek-Start_sek:3, End_ms-Start_ms:3);

Arr_to_Screen(a,n);
Arr_to_file(f2, a, n);


read_file(f1);

gettime(Start_hour, Start_min, Start_sek, Start_ms);

Quick_Sort(a,n);

gettime(End_hour, End_min, End_sek, End_ms);

write('Quick_Sort Run Time for ', n, ' numbers is ');
write(End_hour-Start_hour:3, End_min-Start_min:3);
writeln(End_sek-Start_sek:3, End_ms-Start_ms:3);

Arr_to_Screen(a,n);
Arr_to_file(f2, a, n)

end.





















File f1.doc
2 3 67 89 87 567 354 564 60 34 98 76 29 35 29 92 34 54
5 74 89 76 90 991 234 546 783 23 45 5 3 719




















































File sort2.doc

{In one non-object-oriented language Pascal write a program that:
A. Queries the user for the name of a file containing up to 100 integers.
B. Reads the integers from the file.
C. Runs (and preferably times) either an insertion sort or a selection sort, with results displayed or written to a file.
D. Runs (and preferably times) a quick sort.
}


program Sort_Sort;

{This program reads the integer numbers from a
file and compares some well known sorting methods
using Turbo Pascal 6.0.}

uses dos; {DOS unit for reading the time }

CONST Max_num=100; { number of integers in the file}

TYPE f_int=file of integer; {file type}

arr_int=array[1..Max_num] of integer;

VAR n:integer; {number of integers in the file}

Start_hour, Start_min, Start_sek, Start_ms:word;
{Begin Sorting}

End_hour, End_min, End_sek, End_ms:word;
{Finish Sorting}

ch:char;

f_name:string[20];
f1, f2:f_int;
{f1 is not a sorted file, f2 is a sorted file}

a:arr_int;
{If the number of integers is less then 100 it is easy to use array to sort them.}

procedure Create_File(var f1:f_int);
{Create new file.}

procedure read_file(var f1:f_int);
{Read integers from the file and put them in an array.}
procedure Selection_Sort( var a:arr_int; N : integer);

{Sort array using selection.}
Let elements from 1 to i-1 be in the correct place. Chose the minimum element from i to n. Put in the i place. Elements from 1 to i are in the correct place.


procedure Insertion_Sort( var a : arr_int; N : integer);

{Sort array using insertion.}
Let elements from 1 to i-1 be in the correct place. Insert element i in the correct place j from 1 to i and move elements from j to i-1 to the next place from j+1 to i. Elements from 1 to i are in the correct place





procedure Quick_Sort(VAR X: arr_int; N: Integer);
{ Sort array using a recursive quick sort.}
The procedure Partit divides array in three parts. The middle element goes to the correct place. All elements before (from 1 to k-1) are smaller then it and all elements after (from k+1 to n) are bigger then it. Call the same procedure Qsort to sort the parts. It is recursive call.
PROCEDURE Qsort(VAR X: arr_int; M, N: Integer);
PROCEDURE Partit (VAR A: arr_int; VAR I, J: Integer; Left, Right: Integer);

procedure Arr_to_Screen(a:arr_int; n:integer);
{Displays sorted integer on the screen.}
procedure Arr_to_File(var f:f_int; a:arr_int; n:integer);
{Saves sorted integers in a file.}

begin
{Ask for file name.}
writeln('Enter file''s name>');

readln(f_name);

writeln('Does the file exist Y/N?');

{If there is a file, it can be used. If there is not it, has to be created. It can be done by calling create_file, or it can be done with any text editor like Word before starting Pascal compiler.}
readln(ch);
if (ch='n') or (ch='N') then (f1);
read_file(f1);
gettime(Start_hour, Start_min, Start_sek, Start_ms);
{Getting start sorting time.}

Selection_Sort(a,n);

gettime(End_hour, End_min, End_sek, End_ms);
{Getting End sorting time.}

write('Selection_Sort Run Time for ', n, ' numbers is ');
write(End_hour-Start_hour:3, End_min-Start_min:3);
writeln(End_sek-Start_sek:3, End_ms-Start_ms:3);
{This is the time to complete the sorting.}
Arr_to_Screen(a,n);
Arr_to_file(f2, a, n);

read_file(f1);

gettime(Start_hour, Start_min, Start_sek, Start_ms);

Insertion_Sort(a,n);

gettime(End_hour, End_min, End_sek, End_ms);

write('Insertion_Sort Run Time for ', n, ' numbers is ');
write(End_hour-Start_hour:3, End_min-Start_min:3);
writeln(End_sek-Start_sek:3, End_ms-Start_ms:3);

Arr_to_Screen(a,n);
Arr_to_file(f2, a, n);


read_file(f1);

gettime(Start_hour, Start_min, Start_sek, Start_ms);

Quick_Sort(a,n);

gettime(End_hour, End_min, End_sek, End_ms);

write('Quick_Sort Run Time for ', n, ' numbers is ');
write(End_hour-Start_hour:3, End_min-Start_min:3);
writeln(End_sek-Start_sek:3, End_ms-Start_ms:3);

Arr_to_Screen(a,n);
Arr_to_file(f2, a, n)

end.

Comments

  • why don't you compile it on some freeware pascal compiler? like freepascal, www.freepascal.org
    [hr][red][italic][b]N[/b][/red][blue]et[/blue][red][b]G[/b][/red][blue]ert[/italic][/blue][hr]

  • : why don't you compile it on some freeware pascal compiler? like freepascal, www.freepascal.org
    : [hr][red][italic][b]N[/b][/red][blue]et[/blue][red][b]G[/b][/red][blue]ert[/italic][/blue][hr]
    :
    :
    Ok i got the compiler, I copied the code into the compiler and then hit compile. and it came up with some errors.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories