CS 0007 Quiz 3 Name: ___________________________
11/15/1999
Part 1: Program Segment Questions
1. (30 points)
Write a procedure called
sort that sorts an array of 20 integers in ascending order using the bubble sort algorithm. You’ll also need to write a swap procedure, defined within the sort procedure. Make sure you pass parameters correctly.Hints:
swap takes two parameters passed by variable, has one local variable, and the body of swap contains three statements. The body of sort contains just one statement, consisting of a nested for loop and an if statement.
2. (30 points)
Write a complete program that reads (use readln()) 20 integers from a text file called
inputfile, sorts them using the sort procedure from #1 (you don’t have to rewrite the sort procedure, just assume that it is already declared for you), then writes the sorted array to another text file called outputfile. Be sure you open your files before you use them. Hint: this program has two open statements, two independent for loops , and a call to sort.3. (40 points)
Write a function called
readdata that will read (use read()) ALL records from a nontext file called datafile. The function will fill in an array passed to it by variable and return the number of entries read.Assume the main program has the following declarations:
CONST
maxrecords=100;
TYPE
entry=record
firstname,lastname:array[1..80] of char;
type,status:integer;
end;
database=array[1..maxrecords] of entry;
VAR
data:database;
datafile:file of entry;
The function must have the following header:
function readdata (var myarray:data) : integer;
datafile
is declared globally and the function has full knowledge of its name, but the function is responsible for opening the file for reading.Hints: Use a variable to keep track of the number of entries read in (and make sure you initialize that variable to 1!). Use a
while not eof (datafile) do loop.