Pascal Files as Subprogram Parameter - Computer Programming

Latest

C C++ Java Python Perl Programs Examples with Output -useful for Schools & College Students

Saturday, September 23, 2017

Pascal Files as Subprogram Parameter

Write an  Pascal Program For Files as Subprogram Parameter

Program:
program addFiledata;
const
MAX = 4;
type
raindata = file of real;

var
rainfile: raindata;
filename: string;
procedure writedata(var f: raindata);

var
data: real;
i: integer;

begin
rewrite(f, sizeof(data));
for i:=1 to MAX do

begin
writeln('Enter rainfall data: ');
readln(data);
write(f, data);
end;

close(f);
end;

procedure computeAverage(var x: raindata);
var
d, sum: real;
average: real;

begin
reset(x);
sum:= 0.0;
while not eof(x) do

begin
read(x, d);
sum := sum + d;
end;

average := sum/MAX;
close(x);
writeln('Average Rainfall: ', average:7:2);
end;

begin
writeln('Enter the File Name: ');
readln(filename);
assign(rainfile, filename);
writedata(rainfile);
computeAverage(rainfile);
end.


Output:
Enter the File Name:
rainfall.txt
Enter rainfall data:
34
Enter rainfall data:
45
Enter rainfall data:
56
Enter rainfall data:
78

Average Rainfall: 53.25

No comments:

Post a Comment