Chapter 9:
More About Data, Arrays, and Files
Extended Prelude to Programming
Concepts & Design, 3/e
by
Stewart Venit and Elizabeth Drake
9.1 Random Numbers
• Random numbers are numbers whose values form
an unpredictable sequence
• Many languages contain a Random Number
Generator function to generate a sequence of
random numbers
• Random numbers are often used in gaming
programs, simulations, etc.
• The pseudocode Random(N) represents a function
which causes the program to generate a random
integer in the range 1 to N
2
Random Numbers (continued)
• Random(3) generates 1, 2, or 3
• To change the range of the numbers
generated, shift the output of the function
[Random(3) + 5] generates 6, 7, or 8
[Random(5) – 4] generates -3, -2, -1, 0, or 1
[Random(3) * 5] generates 5, 10, or 15
3
The Pseudorandom Number
Random numbers in a computer program are not truly
random. They are generated by a mathematical
algorithm.
The beginning value – the number that the algorithm uses
to start with – is called the seed.
To ensure that the algorithm does not generate the same
numbers each time, it is necessary to use a different
seed each time.
An example of a good seed value is the number of
milliseconds since the beginning of the current year
since this value will only occur once every year.
For our purposes, pseudorandom numbers are just as
useful as true random numbers.
4
9.2 ASCII Code
• A character is a single number, letter or symbol that can
be typed at the keyboard.
• Each character is represented internally by a numeric
code. The code for these numbers is called ASCII.
• ASCII stands for American Standard Code for
Information Interchange.
• Lower and upper case letters are represented by
different numbers.
• Even a space or blank is represented by a number!
• Enclose characters in double quotes.
• A string is a sequence of characters.
• Enclose strings in double quotes.
5
Ordering Arbitrary Strings
• Two strings are equal if they have the same
length and the same characters, in the same
order.
• “Blue” and “B L U E” are not equal.
• “c%$$” and “c%$$” are equal.
• Enclosing a number in quotes causes the
number to be regarded as a string. For example:
Write “1 + 2”
will not display 3, but will display the character string:
1 + 2
6
9.3 Additional Searching
and Sorting Techniques
Binary Search:
• Searching for data can be done with the sequential search, as seen in
Chapter 5. For large amounts of data, it is more efficient to use a binary
search algorithm.
• Data must be sorted before a binary search can be used.
Suppose you are looking for a word in the dictionary. Using a sequential search
you would look at the first word, then the second, then the third until you find
the word you are looking for. Here’s how it would work using a binary
search:
1. Open the dictionary to the target word's approximate location.
2. Check the target word against an entry on that page to determine if you
have gone too far or not far enough.
3. Repeat steps 1 and 2 until you have located the target word.
7
Pseudocode for the Binary
Search
Set Low = 0
Set High = N
Set Index = Int(N/2)
Set Found = 0
While (Found = 0) AND (Low <= High)
If Key = Array[Index] Then
Set Found = 1
End If
If Key > Array[Index] Then
Set Low = Index + 1
Set Index = Int((High + Low)/2)
End If
If Key < Array[Index] Then
Set High = Index - 1
Set Index = Int((High + Low)/2)
End If
End While
8
A Walk Through a Binary
Search
9
The Selection Sort
• The selection sort is a more efficient way to sort data than the
bubble sort presented in Chapter 5.
• Here’s how we would use it to sort an array in ascending order
(from smallest to largest). We make several passes through the
array:
– On the first pass, we locate the smallest array element and swap it with
the first array element.
– On the second pass, we locate the smallest element after the first one
and swap it with the second element of the array.
– On the third pass through the array, we locate the smallest element after
the second one and swap it with the third element of the array.
– And so on. If the array contains N elements, it will be completely sorted
after at most N - 1 passes.
10
Pseudocode for the Selection
Sort
To fully understand the selection sort algorithm, you must walk through the
code using example data.
This pseudocode will sort an array consisting of N + 1 elements.
For K
Set
Set
For
= 0 Step 1 To N - 1
Min = Array[K]
Index = K
J = K + 1 Step 1 To N
If Array[J] < Min Then
Set Min = Array[J]
Set Index = J
End If
End For (J)
If K <> Index Then
Set Temp = Array[K]
Set Array[K] = Array[Index]
Set Array[Index] = Temp
End If
End For (K)
11
9.4 Introduction to Direct-Access
Files
A direct-access file is often called a random access file.
It is different from a sequential file in that any record can
be read independently from the rest. A sequential file
must be read from the beginning.
A sequential file is analogous to a tape player: if you want
to listen to the 5th song, you must fast-forward through
the first 4 songs.
A direct-access file is analogous to a CD: if you want to
hear the 5th song on a CD, simply select Track 5 and you
can jump right to it.
12
Defining Records for DirectAccess Files
A direct-access file must have a known structure for its
records.
Record structures will be defined in our pseudocode
language as a new data type. Consider the following a
the definition of a data type:
Record Type StudentInfo
Code As String[4]
Name As String[12]
End Record
We can then declare a variable of this new type as follows:
Declare Student as StudentInfo
13
Records of Data in a DirectAccess File
Declare Student as StudentInfo
The variable Student has 2 fields, both of them are Strings, and
their names are:
Student.Code
Student.Name
The record size for a direct-access file must be known exactly so it is
possible to Seek to a particular record position in the file.
SeekGet filename, record number, record variable
For example:
SeekGet DataFile, 5, Student
will read the 5th record from the DataFile, and store the data in the
variable called Student. There is a corresponding SeekPut for
adding data to a direct-access file.
14
Using a Direct-Access File
The following program creates and displays a direct-access
file with records of the form:
Student ID (4-character string)
Student name (12 character string)
Units earned (integer)
Grade point average (real)
Record Type StudentInfo
Code As String[4]
Name As String[12]
Units As Integer
GPA As Real
End Record
Continued…
15
Using a Direct-Access File
16
9.5 Using Direct-Access Files:
Indexed Files
Direct-access files are very useful for maintaining files of data that are
frequently modified. For example, in a file of student data, we might want to
add, delete, and update student information.
A realistic example of manipulating a direct-access file is developed in Ch. 8.
We start with the record definition, and we open a direct-access file for
Random Access.
Record Type StudentInfo
Code As String[4]
Name As String[12]
Units As Integer
GPA As Real
End Record
Declare Student As StudentInfo
Open “studata” For Random As DataFile, Len=Length(Student)
17
Adding, Deleting, Updating
Records
To manipulate a direct-access file, we need to know the length of each
record, and how many records are currently in the file.
In our pseudocode language, we will specify the length of each record
at the time we open the file.
This calculates the bytes in each record:
Len = Length(Student)
This calculates the current length of the file in bytes:
LOF(DataFile)
This calculates how many records are in the file:
LOF(DataFile) / Length(Student)
18
Adding, Deleting, Updating
Records (continued)
• Adding a record
Write “Enter data for new student in the form:”
Write “ID Code, Name, Units Earned, GPA”
Input Student.Code, Student.Name, Student.Units,
Student.GPA
Set Last = LOF(DataFile) / Length(Student)
SeekPut DataFile, Last + 1, Student
• Deleting a record
Write “Record number of record to be deleted:”
Input Number
Set Student.Code = “”
Set Student.Name = “”
Set Student.Units = 0
Set Student.GPA = 0
SeekPut DataFile, Number, Student
19
Adding, Deleting, Updating
Records (continued)
• Modifying a record
This program segment changes the data in the units earned and GPA fields of
the studata file
Write “Record number of record to be modified:”
Input Number
SeekGet DataFile, Number, Student
Write “Student name: ”, Student.Name
Write “Enter total units earned, new GPA”
Write “or enter 0,0 to leave the record unchanged.”
Input Student.Units, Student.GPA
If Student.Units <> 0 Then
SeekPut DataFile, Number, Student
End If
20
Indexed Files
•
•
•
From the previous code, it might be noted that the difficulty with the directaccess file is that you must know the record number of the record you wish
to delete, or modify.
This is addressed by indexed files. The basic idea of an indexed file is that
a separate file is maintained that contains a key item in the records along
with the record number for that item.
For example, the Student.Code is a unique identifier for each student,
and a file can be created that contains the student codes and their
associated record number. This file is called the index file, and its purpose
is to make the access of the direct-access file more friendly and efficient.
21
Pseudocode Language (Ch 9)
Random Number Generator
Random(N)  generates a
pseudorandom number from 1 to N
Create a Record for a direct-access file
Record Type RecordInfo
OneThing As String[4]
AnotherThing As Real
End Record
Calculate bytes in each record:
Len = Length(Student)
Calculate the length of file in bytes:
LOF(DataFile)
Calculates number of records in the file:
LOF(DataFile) / Length(Student)
To find a record in a direct-access file
Declare a Variable of the new Type
Declare Variable As RecordInfo
SeekGet filename, record
number, record variable
To add a record to a direct-access file
SeekPut filename, record
number, record variable
22
Descargar

Chapter 9