1
Jan 18, 2018
1
2
C/C++ Text File Functions
fopen – opens a text file.
fclose – closes a text file.
feof – detects end-of-file marker in a file.
fscanf – reads formatted input from a file.
fprintf – prints formatted output to a file.
fgets – reads a string from a file.
fputs – prints a string to a file.
fgetc – reads a character from a file.
fputc – prints a character to a file.
fflush – empties the buffer3
Review Practice 1
If You Are NotComfortable
With Doing ThisPractice It! 4
Review Practice 2
If You Are NotComfortable
With Doing ThisPractice It!
5
Standard Text Files - Program A
Mode “w” erases, the file if it exits!
If You Are NotComfortable With
Doing This Practice It!
6
Read From Text File
If You Are NotComfortable
With Doing ThisPractice It!
7
8
9
11
12
Some Applications Require More Than 24 Hours To Read A Single
File!12
13
Some Applications Require More Than 24 Hours To Read
A Single File!
Text File Read ProcessingOpen FileRead & Write To Sought DataRepeat!
Sought Data
Sought Data
Read #1 Read #2
ReadRead
13
14
Some Applications Require More Than 24 Hours To Read A Single
File!14
Text File Change ProcessingOpen Original & Destination FilesRead Original – Write Destination [up to point of change]Write Change DataRead Original – Write Destination [remaining data]Erase Original File & Rename Destination File
Change Data
Change #1
Read &
Write
15
Changed Data
Read & Write
16
17
Can Often Be Done In Less Than 1/10 Second
Hardware Dependent
DA File Read ProcessingOpen File
Seek & Read DataRepeat!
Sought Data
Sought Data
Read #1 Read #217
DA Change ProcessingOpen Original
Read Original – Write Destination [up to point of change]Write Change Data
Change Data
Change #1
Read
Make Change
Write
18
19
20
Text Files
Direct Access Files
The Choice Is Yours!
21
22
Create Three New Direct Access Files
Close File
File Ptrs
23Execute This Program?
Create File
23
What Happened As A Result Of Executing This Program?
Three Files Were Created? Where? Find Them!
24
How Big Are The Files? How Do You Know?Right Mouse Click on the file and select Properties!
25
26
Dir ls
26
27
Dir *.fil ls *.fil
Dir Wild Cards *
28
StudentFil = fopen("Student.Fil", "wb+");
Opening a file in the wb+ mode checks to see if there is a file by the name Student.Fil:
If so, it erases the contents & positions the read/write pointer at the beginning of the empty file
If not, it creates the file & positions the read/write pointer at the beginning of the empty file
About fopen
fopen Modes
Direct Access Files DAF’s
29
30
fclose(StudentFil);• All files are buffered. • The buffer size can be set by the operating
system and or the programming language. • To reduce wear and tear on the hard drives, most
operating systems write information to a buffer – when the buffer is full, it is dumped to disk.
• When a file is closed, the information, remaining in the buffer, is written to disk and the file allocation table is updated to reflect changes in the file.
• Failure to close a file can result in lost information.
About fclose
31
32
About fseekfseek(FilePtr, NoBytes, SEEK_SET);
SEEK_SET is a constant representing the beginning of the file. SEEK_END is a constant representing the end of the file.
fseek(StudentFil, 4 * sizeof(Student), SEEK_SET);
The size of each Student record is 36 Bytes.
Move the Read-Write Pointer 4 * 36 = 144 bytes from the beginning of the file referenced by the StudentFil ptr.
Valid Records To Seek Are 0 – N+1
33
fseek(StudentFil, 0 * sizeof(Student), SEEK_SET);fseek(StudentFil, 0, SEEK_SET);
Move the Read-Write Pointer to the beginning of the file [First Record]
fseek(StudentFil, (N+1) * sizeof(Student), SEEK_SET);fseek(StudentFil, 0 * sizeof(Student), SEEK_END);
fseek(StudentFil, 0, SEEK_END);
Move the Read-Write Pointer to the end of the file [EOF Marker] – to append a new record.
fseek Examples
34
35
fwrite(&Info, InfoSize(byes), # records, fp);fwrite(&NewStudent, sizeof(Student),(long)1,StudentFil);
Write the 1 record of information referenced by NewStudent, whose size is 36 Bytes, to the current Read-Write Location of the StudentFil.
Student Students[20]fwrite(&Students,sizeof(Student),(long)20,StudentFil);
Write the 20 records of information referenced by array Students to the current Read-Write Location of the StudentFil.
FWRITE can be used to write a single record or a block of records.
About fwrite
Suppose the Output Buffer holds only two records.Student LocalYear;fseek(StudentFil, 3 * sizeof(Student), SEEK_SET);
fwrite(&LocalYear,sizeof(Student),(long)1,StudentFil);
36
The information is actually written to the buffer – when the buffer is full or location is changed, it is written to disk. The buffer size can be set 0 to force immediate writes!
fwrite Examples
37
Review -Valid Writes For A File With 3 Records Replace Record 0fseek(StudentFil, 0 * sizeof(Student), SEEK_SET);
fwrite(&NewStudent,sizeof(Student),(long)1,StudentFil);
Replace Record 1fseek(StudentFil, 1 * sizeof(Student), SEEK_SET);
fwrite(&NewStudent,sizeof(Student),(long)1,StudentFil);
Replace Record 2fseek(StudentFil, 2 * sizeof(Student), SEEK_SET);
fwrite(&NewStudent,sizeof(Student),(long)1,StudentFil);
Append/Add New Record 3fseek(StudentFil, 3 * sizeof(Student), SEEK_SET);
fwrite(&NewStudent,sizeof(Student),(long)1,StudentFil);
38
Compile The Program!
Change
To
39
40
Did It Do Anything? How Do You Know?
Yes It Did Something! The File Is 4 Bytes Bigger Than It Was.
Did It Do The Right Thing?
How Do You Know?
We Don’t’! How Could You Know?
Read The File?
41
Create File – Open To Read Or Write
File Ptrs
42Execute This Program?
Open An Existing File To Read Or Write
43
44
fseek(FilePtr, NoBytes, SEEK_SET);SEEK_SET is a constant representing the beginning of the file. SEEK_END is a constant representing the end of the file.
fseek(StudentFil, 4 * sizeof(Student), SEEK_SET);
The size of each Student record is 36 Bytes.
Move the Read-Write Pointer 4 * 36 = 144 bytes from the beginning of the file referenced by the StudentFil ptr.
Can Do – Have Already Done!
About fseek
45
46
fread(&Info, InfoSize(byes), # records, fp);fread(&NewStudent,sizeof(Student),(long)1,StudentFil);
Read the 1 36-byte record of information, pointed to by the current Read-Write Pointer of the StudentFil, into the NewStudent container. Student Students[20]fread(&Students,sizeof(Student),(long)20,StudentFil);
Read twenty 36-byte records of information, pointed to by the current Read-Write Pointer of the StudentFil, into array Students.
FREAD can be used to write a single record or a block of records.
About fead
47
Suppose the Input Buffer holds only two records.Student LocalYear;fseek(StudentFil, 2 * sizeof(Student), SEEK_SET);
fread(&LocalYear,sizeof(Student),(long)1,StudentFil);
Note that both Record 2 and Record 3 are in the Input Buffer. Ifthe program logic were to need to read either of these records, they would be retrieved from the buffer as opposed to readingthem from disk Much Faster [Nanosecond vs. Millisecond]
48
Valid Reads For A File With 3 Records
Read Record 0fseek(StudentFil, 0 * sizeof(Student), SEEK_SET);
fread(&NewStudent,sizeof(Student),(long)1,StudentFil);
Read Record 1fseek(StudentFil, 1 * sizeof(Student), SEEK_SET);
fread(&NewStudent,sizeof(Student),(long)1,StudentFil);
Read Record 2fseek(StudentFil, 2 * sizeof(Student), SEEK_SET);
fread(&NewStudent,sizeof(Student),(long)1,StudentFil);
49
Compile The Program!
Change
To
50
Did It Do The Right Thing?
Absolutely!
But Will It Work For Multiple Records?
51
Compile The Program!
Change
52
53
Wonder What The File Looks Like With A Text Editor?
54
Binary Files Provide Some Additional Security InThat They Are Less Readable Than Text Files
Did It Do The Right Thing?
Absolutely!
But Does rb+ Really Allow
Reads & Writes?
55
Compile The Program!
Add This To The
Program
56
57
Will This Really Work With Class Datatypes?
58
Compile The Program!
Change
To
59
+
+
60
Works For One Student
More Extensive Auto Testing!
One 36-Byte Record
61
Compile The Program!
Change
To
62
63
Eight 36-Byte Records
64
65
Open An Existing Direct Access FileFILE
* AutoFil,
* StudentFil;
StudentFil = fopen("Student.Fil", "rb+");AutoFil = fopen("Auto.Fil", "rb+");
66
Open A New Direct Access File &Write A Record To Record 0 And A
Record To Record 1FILE
* StudentFil;
StudentNewStudent;
StudentFil = fopen("Student.Fil", "rb+");
if (NewStudent.Get())
fwrite(&NewStudent,sizeof(Student),(long)1,StudentFil);
if (NewStudent.Get())
fwrite(&NewStudent,sizeof(Student),(long)1,StudentFil);
fclose(StudentFil);
67
Open A Existing Access File Student.Fil (Has Two Records) & Add A Third
Record`
FILE
* StudentFil;
StudentNewStudent;
StudentFil = fopen("Student.Fil", "rb+");
fseek(StudentFil, 2 * sizeof(Student), SEEK_SET);
if (NewStudent.Get())
fwrite(&NewStudent,sizeof(Student),(long)1,StudentFil);
fclose(StudentFil);
Move To Record 2Third Record 0,1,2
68
Open A Existing Access File Student.Fil (Has Three Records) Display All 3 Records In Reverse Order
int
Counter;
FILE
* StudentFil;
StudentNewStudent;
StudentFil = fopen("Student.Fil", "rb+");
for (Counter = 2; Counter >= 0; Counter --)
{
fseek(StudentFil, Counter * sizeof(Student), SEEK_SET);
fread(&NewStudent,sizeof(Student),(long)1,StudentFil);
NewStudent.Display();
}
fclose(StudentFil);
Move To Record 2, 1, 0 Respectively
69
Student
NewStudent;
FILE
* StudentFil;
StudentFil = fopen("Student.Fil", "rb+");
if (NewStudent.Get() == VALID)
fwrite(&NewStudent,sizeof(Student),(long)3,StudentFil);
fclose(StudentFil);
Replace Record 3 [Fourth Record] With NewStudent
70
71
Generic FileLength Function
72
long int Filelength (FILE * FilePtr, long int NoBytesPerRecord)
{
long int
Bytes;
fseek (FilePtr, 0L, SEEK_END);
Bytes = ftell (FilePtr);
return (Bytes / NoBytesPerRecord);
}
int
Counter;
FILE
* StudentFil;
NoRecords = FileLength(StudentFil, sizeof(Student));
Belongs in Utilities.cpp
Update It Now!
Test It Now!
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
// //
// WriteRecord //
// //
// Purpose : Write the direct access record RecordNo from file *fp from //
// container Info. No error checking yet! //
// //
// Written By : Dr. Thomas E. Hicks Environment : Windows 2000 //
// Date : XX/XX/XX Compiler : Visual C++ //
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
template <class InfoType>
bool WriteRecord(InfoType *Info, long int RecordNo, FILE *fp)
{
fseek(fp, RecordNo * sizeof(InfoType), SEEK_SET);
fwrite(Info, (long)sizeof(InfoType), 1L, fp);
return (SUCCESSFUL);
}
Generic WriteRecord
73
Belongs in Utilities.hpp
Update It Now!
Student
NewStudent;
FILE
* StudentFil;
long int
Counter = 0;
StudentFil = fopen("Student.Fil", "wb+");
while (NewStudent.Get() == VALID)
fwrite(&NewStudent,sizeof(Student),Counter++,StudentFil);
fclose(StudentFil);
Test Generic WriteRecord
74
Test It Now!
Not A Great Solution – Does Not Trap ErrorsNever Returns UNSUCCESSFUL – See Chapter 11! 74
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
// //
// ReadRecord //
// //
// Purpose : Read the direct access record RecordNo from file *fp into //
// container Info. No error checking yet! //
// //
// Written By : Dr. Thomas E. Hicks Environment : Windows 2000 //
// Date : XX/XX/XX Compiler : Visual C++ //
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
template <class InfoType>
bool ReadRecord(InfoType *Info, long int RecordNo, FILE *fp)
{
fseek(fp, RecordNo * sizeof(InfoType), SEEK_SET);
fread(Info, (long)sizeof(InfoType), 1L, fp);
return (SUCCESSFUL);
}
Generic ReadRecord
75
Belongs in Utilities.hpp
Update It Now!
Student
NewStudent;
FILE
* StudentFil;
long int
Counter,
NoRecords;
StudentFil = fopen("Student.Fil", "rb+");
NoRecords = FileLength(StudentFil, sizeof(Student));
for (Counter = NoRecords - 1; Counter >=0; Counter --)
if (ReadRecord(&NewStudent,Counter,StudentFil))
NewStudent.Display();
fclose(StudentFil);
Test Generic ReadRecordFile Exists From Test Of WriteRecord
76
Test It Now!
Not A Great Solution – Does Not Trap ErrorsNever Returns UNSUCCESSFUL – See Chapter 11! 76
Selection Of The RightData Structure
SOFTWARE ENGINEERING!
Almost Every Application Starts With Some Data You Need To Manage
Students
Integers
PartsParts
class Part{public:
private:char Name [26];long int No, Quantity, DeptNo;double Cost;// . . .};
You Do A Systems Analysis & Determine What Data Is NeededYou Create Your Base Class
class Part{public:
private:char Name [26];long int No, Quantity, DeptNo;double Cost;// . . .};
You Do A Systems Analysis & Determine What Data Is NeededYou Create Your Base Class
class Part{public:
Part (char NewName[] = "", long int NewNo = 0, long int NewDeptNo = 0, long int NewQuantity = 0, double NewCost = 0.0);
~Part (void);void Set (char NewName[] = "", long int NewNo = 0, long int NewDeptNo = 0,
long int NewQuantity = 0, double NewCost = 0.0);bool Get(void);long int Key(void);long int No_(void);double Cost_(void);long int Quantity_(void);long int DeptNo_(void);
private:char
Name [26];long int
No, Quantity,DeptNo;
doubleCost;
};
Your System Analysis Will Help You Determine The Necessary Support Functions
Accessor & Mutator Functions
class Part{public:
Part (char NewName[] = "", long int NewNo = 0, long int NewDeptNo = 0, long int NewQuantity = 0, double NewCost = 0.0);
~Part (void);void Set (char NewName[] = "", long int NewNo = 0, long int NewDeptNo = 0,
long int NewQuantity = 0, double NewCost = 0.0);bool Get(void);long int Key(void);long int No_(void);double Cost_(void);long int Quantity_(void);long int DeptNo_(void);
private:char
Name [26];long int
No, Quantity,DeptNo;
doubleCost;
};
Your System Analysis Will Help You Determine The Necessary Support Functions
Accessor & Mutator Functions
friend ostream & operator << (ostream & OutputStream, Part P);
// I have decided that Name is to be the Primary Key for this record type. bool operator == (const Part & P);bool operator > (const Part & P);bool operator >= (const Part & P);bool operator < (const Part & P);bool operator <= (const Part & P);bool operator != (const Part & P);
void operator = (const Part & P);
// I have decided that Name is to be the Primary Character Key for this record type. bool operator == (const char Key[]);bool operator > (const char Key[]);bool operator >= (const char Key[]);bool operator < (const char Key[]);bool operator <= (const char Key[]);bool operator != (const char Key[]);void operator = (const char Key[]);
// I have decided that No is to be the Primary Integer Key for this record type. bool operator == (const long int Key);bool operator > (const long int Key);bool operator >= (const long int Key);bool operator < (const long int Key);bool operator <= (const long int Key);bool operator != (const long int Key);void operator = (const long int Key);
Your Specifications Will Almost Always Dictate Your Operator Overload DesignI always do overload ostream & operator << for diagnostic testing
If the specifications want reports that are in order by the Part.Name
I will have this collection
Required Only For Deep Copy
If the specifications want are going to search, or compare, the base class with a character
string, I will have this collection
If the specifications want are going to search, or compare, the base class with a long integer,
I will have this collection
There are ways to do generics with more than one char string or integer.
Specifications
Requirements
The user will be able to search 1,000,000 records by Part.No and display the sought record in no more than .5 seconds.
The user will be able to add a new part to
999,999 others in no more that
1.0 seconds.
Most Systems Should Have Some Written Requirements
Out Requirements Almost Always Require The Following Functionality
SearchAdd
Delete
1,000,000 Parts~20,000 Byte Records
1,000,000 x 20,000 =20,000,000,000 Bytes =
18.63 GB
Have To Use A File
Compute The Average Search Time
Divide The Time RequiredTo Search Each Record
By The Number Of Records
Create a direct access file with 1,000,000 Records – 20,000 Bytes Each – the content (x) does not matter.
To Calculate Average Search Time For A Computer - 1
1,000,000999,999999,998999,997
123
xxxx
xxx
Direct Access File
4 x
Maybe Write 1,000,000 Records – 20,000 Bytes Each. The data on the file (x) is unimportant.
Create an internal memory array whose integer data members are 1 1,000,000 randomly mixed.
To Calculate Average Search Time For A Computer - 2
1,000,000999,999999,998999,997
123
xxxx
xxx
Direct Access File
1,000,000999,999999,998999,997
123
2874
65341,124
32,4128
23,311
Internal Memory Array
4 x 4 2,301
Start the clock
To Calculate Average Search Time For A Computer - 3
1,000,000999,999999,998999,997
123
2874
65341,124
32,4128
23,311
A
4 2,301
For I = 1 To 1,000,000 ReadRecord(&P, A[I], fp)
Read record # 34,412Read Record # 8Read Record # 23,311Read Record # 2,301...Read Record # 34124Read Record # 65Read Record # 874Read Record # 2
Stop the clock
Compute The Total # Seconds
# SecondsAverage Search = # Records
Run The Program 10 Time &Get A Range Of Values?
Drive Speed (5200, 7600, etc.)Drive Type (Internal, External, IDE-Sata-USB-Firewire-ESata
Degree Of Optimization Optimized Fragmented
Other Programs Running
Basic Assumptions ForThis Set Of Slides
1,000,000 RecordsTime To Read/Write 1 Record
Is 1/200 Second
200 Records/Second
Design Consideration 1One Unordered
Direct Access File
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per Second
Parts
D
A
FILE
1,000,000
1
Design Consideration 1One Unordered
Direct Access File
Search
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Unordered Direct Access File
Parts – Worst Search
0X
Sec
Sequential Search1,000,000 Records1,000,000/200 = 5,000 Seconds 1
D
A
FILE
1,000,000
5,000
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Unordered Direct Access File
Parts – Average Search
Sec
Sequential Search1,000,000/2 = 500,000 Records500,000/200 = 2,500 Seconds 1
D
A
FILE
1,000,000
2,5000
Design Consideration 1One Unordered
Direct Access File
Add
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Unordered Direct Access File
Parts – Worst Add
Sec
1
D
A
FILE
999,999
Append 1 Record To End Of File1 Write1/200 = .005 Seconds
.005W
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Unordered Direct Access File
Parts – Average Add
Sec
Append 1 Record To End Of File1 Write1/200 = .005 Seconds
1
D
A
FILE
999,999
.005W
Design Consideration 1One Unordered
Direct Access File
Delete
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Unordered Direct Access File
Parts – Worst DeleteKnow Location
Sec
Read Record 1,000,000
1
D
A
FILE
1,000,000
XR
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Unordered Direct Access File
Parts – Worst DeleteKnow Location
Sec
Read Record 1,000,000Write To Record 999,9972/200 = .01 Sec 1
D
A
FILE
1,000,000W
.01
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Unordered Direct Access File
Parts – Worst DeleteDon’t Know Location
Sec
Worst Search = 5,000 Seconds
1
D
A
FILE
1,000,000
XR
R
5,000.005
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Unordered Direct Access File
Parts – Worst DeleteDon’t Know Location
Sec
Worst Search = 5,000 SecondsWrite To Record 999,999= 1/200 = .005 Sec
1
D
A
FILE
1,000,000W
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Unordered Direct Access File
Parts – Average DeleteKnow Location
Sec
Read Record 1,000,000
1
D
A
FILE
1,000,000
XR
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Unordered Direct Access File
Parts – Average DeleteKnow Location
Sec
Read Record 1,000,000Write To Record 999,9972/200 = .01 Sec 1
D
A
FILE
1,000,000W
.01
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Unordered Direct Access File
Parts – Average DeleteDon’t Know Location
Sec
Average Search = 2,500 Seconds
1
D
A
FILE
1,000,000
XR
R
2,500.005
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Unordered Direct Access File
Parts – Average DeleteDon’t Know Location
Sec
Average Search = 2,500 SecondsWrite To Record 999,999= 1/200 = .005 Sec
1
D
A
FILE
1,000,000W
The user will be able to search 1,000,000 records by Part.No and display the sought record in no more than .5 seconds.
The user will be able to add a new part to
999,999 others in no more that
1.0 seconds.
Can’t Satisfy These Requirements Specifictions With One UnOrdered Array
Sec5,000
Sec.005
Binary SearchReview
You Have Done WithArrays But Many Of You
Have Not With Files
On Exam/Quiz, You May Calculate Log2 (N)
113
1 12 23 44 85 166 327 648 1289 25610 1,02411 2,04812 4,096
13 8,19214 16,38415 32,76816 65,536
If There Are N Nodes At This Level Of A Complete Tree, There Are N-1 Nodes Above This Level
Ball Park = ~16 114
Computer A Search 1 RecordOrdered/Sorted Direct Access Files “Ordered List”
Record Quantity = 100,000 Avr Read/Write Time = 20,000 Byte Record = .01 Sec
Worst Case: Sorted Read ~Log2(100,000) Records
~16.6 sec
115
Computer A Search 1 RecordOrdered/Sorted Direct Access Files “Ordered List”
Record Quantity = 100,000Avr Read/Write Time = 20,000 Byte Record = .01 Sec
Average Case: Sorted Read ~Log2(100,000)-1 Records
~16.6 – 1 – 15.6 sec
??-1??
116
To Compute Binary Exact?
Avr Search = Randomly Search Each Element Once
1
2 2
3 333
= 1
= 4
= 12
Total Searches = 17
Avr Search = 17/7 = 2.43
Log2(15)=____2.8
117
To Compute Binary Exact?
Avr Search = Randomly Search Each Element Once
1
2 2
3 333
4 4 4 4 4 4 4 4
= 1
= 4
= 12
= 32
Total Searches = 49
Avr Search = 49/15 = 3.27
Log2(15)=____3.9
118
To Compute Binary Exact?
Avr Search = Randomly Search Each Element Once
Total Searches = 129
Avr Search = 129/31 = 4.16
Log2(31)=____ = 1
= 4
= 12
= 32
= 80
4.9
119
120Complete
Binary Treeor
Binary Search
Worst Search Log2(N) Reads
Ave Search Log2(N)-1 Reads
120
Design Thought
Ready For A Sorted Direct Access File
121
Direct Access Files - Will Be Times We Delete (21)
122
8
21
25
32
58
85
7
555
0 0
21
25
32
58
85
6
555
There Will Be Times When Not All Of The Records Are Used - Record 0 Provides A Great Place To Store The Actual Number Of Valid Records - We Will Have To Keep
This Number Accurate! I Will Not Include It In Many Of The Sketches!
Design Consideration 2
One Ordered Direct Access File
Design Consideration 2
One Ordered Direct Access File
Search
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Ordered Direct Access File
Parts – Worst Search
SecD
A
FILE
R Binary Search- Bigger
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Ordered Direct Access File
SecD
A
FILE
R
Binary Search- BiggerBinary Search- Smaller
Parts – Worst Search
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Ordered Direct Access File
SecD
A
FILE
Binary Search - Smaller
RBinary Search- Bigger
Got It!
Parts – Worst Search
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Ordered Direct Access File
SecD
A
FILE
Binary SearchLog2 (1,000,000) = 20 20/200 = .1 Seconds
R Binary Search- Bigger
.1
Parts – Worst Search
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Ordered Direct Access File
SecD
A
FILE
Half The Data At The BottomBinary Search - 1Log2 (1,000,000) = 20 - 119/200 = .1 Seconds
R
.095
Parts – Average Search
Design Consideration 2
One Ordered Direct Access File
Add
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Ordered Direct Access File
Parts – Worst Add
SecD
A
FILE
100 200 300 400 500 600
New 50
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Ordered Direct Access File
Parts – Worst Add
SecD
A
FILE
100 200 300 400 500 600
New 50
R
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Ordered Direct Access File
Parts – Worst Add
SecD
A
FILE
100 200 300 400 500
New 50
W 600
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Ordered Direct Access File
Parts – Worst Add
SecD
A
FILE
100 200 300 400 500
New 50
600
R
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Ordered Direct Access File
Parts – Worst Add
SecD
A
FILE
100 200 300 400
500
New 50
W
600
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Ordered Direct Access File
Parts – Worst Add
SecD
A
FILE
100 200 300 400
500
New 50
600
R
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Ordered Direct Access File
Parts – Worst Add
SecD
A
FILE
100 200 300
400 500
New 50
W
600
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Ordered Direct Access File
Parts – Worst Add
SecD
A
FILE
100 200 300
400 500
New 50
600
R
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Ordered Direct Access File
Parts – Worst Add
SecD
A
FILE
100 200
300 400 500
New 50
W
600
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Ordered Direct Access File
Parts – Worst Add
SecNew 50
R
D
A
FILE
100 200
300 400 500 600
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Ordered Direct Access File
Parts – Worst Add
SecNew 50
W
D
A
FILE
100
200 300 400 500 600
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Ordered Direct Access File
Parts – Worst Add
SecNew 50
R
D
A
FILE
100
200 300 400 500 600
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Ordered Direct Access File
Parts – Worst Add
SecNew 50
W
D
A
FILE
100 200 300 400 500 600
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Ordered Direct Access File
Parts – Worst Add
SecD
A
FILE
100 200 300 400 500
New 50 W
600
Read 999,999 = 4,999.995 Seconds Write 999,999 = 4,999.995 SecondsWrite New = .005 Seconds
10,000.000
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Ordered Direct Access File
Parts – Worst Add
Sec
New 50 100 200 300 400 500 600
Read 999,999 = 4,999.995 Seconds Write 999,999 = 4,999.995 SecondsWrite New = .005Update Record 0 = .005 = 10,000.000 Seconds
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Ordered Direct Access File
Parts – Average Add
SecD
A
FILE
100 200 300 400 500 600
New 350
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Ordered Direct Access File
Parts – Average Add
SecD
A
FILE
100 200 300 400 500 600
New 350
R
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Ordered Direct Access File
Parts – Average Add
SecD
A
FILE
100 200 300 400 500
New 350
W 600
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Ordered Direct Access File
Parts – Average Add
SecD
A
FILE
100 200 300 400 500
New 350
600
R
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Ordered Direct Access File
Parts – Average Add
SecD
A
FILE
100 200 300 400
500
New 350
W
600
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Ordered Direct Access File
Parts – Average Add
SecD
A
FILE
100 200 300 400
500
New 350
600
R
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Ordered Direct Access File
Parts – Average Add
SecD
A
FILE
100 200 300
400 500
New 350
W
600
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Ordered Direct Access File
Parts – Average Add
SecD
A
FILE
100 200 300
400 500
New 350 W
600
Read Half + 1 = 2,500.005 Seconds Write Half = 2,500 Seconds
5,000.01
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Ordered Direct Access File
Parts – Average Add
SecD
A
FILE
100 200 300
400 500
New 350
600
Read Half + 1 = 2,500.005 Seconds Write Half = 2,500 SecondsWrite New = .005Update Rec 0 = .005 = 5,000.015 Seconds
Design Consideration 2
One Ordered Direct Access File
Delete
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Ordered Direct Access File
Parts – Worst Delete
Sec
To Delete 50
Worst BinarySearch To Find Location Log2 (1,000,000) = 20 20/200 = .1 Seconds 100
200 300 400 500 600
Location
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Ordered Direct Access File
Parts – Worst Delete
Sec
To Delete 50
Worst Binary Search To Find Location Log2 (1,000,000) = 20 20/200 = .1 Seconds 100
200 300 400 500 600
R
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Ordered Direct Access File
Parts – Worst Delete
SecWorst Binary Search To Find Location Log2 (1,000,000) = 20 20/200 = .1 Seconds
200 300 400 500 600
W 100
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Ordered Direct Access File
Parts – Worst Delete
SecWorst Binary Search To Find Location Log2 (1,000,000) = 20 20/200 = .1 Seconds
200 300 400 500 600
100
R
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Ordered Direct Access File
Parts – Worst Delete
SecWorst Binary Search To Find Location Log2 (1,000,000) = 20 20/200 = .1 Seconds 200
300 400 500 600
W
100
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Ordered Direct Access File
Parts – Worst Delete
SecWorst Binary Search To Find Location Log2 (1,000,000) = 20 20/200 = .1 Seconds 200
300 400 500 600
100
R
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Ordered Direct Access File
Parts – Worst Delete
SecWorst Binary Search To Find Location Log2 (1,000,000) = 20 20/200 = .1 Seconds 200
300
400 500 600
W
100
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Ordered Direct Access File
Parts – Worst Delete
SecWorst Binary Search To Find Location Log2 (1,000,000) = 20 20/200 = .1 Seconds 200
300
400 500 600
100
R
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Ordered Direct Access File
Parts – Worst Delete
SecWorst Binary Search To Find Location Log2 (1,000,000) = 20 20/200 = .1 Seconds 200
300 400
500 600
W
100
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Ordered Direct Access File
Parts – Worst Delete
SecWorst Binary Search To Find Location Log2 (1,000,000) = 20 20/200 = .1 Seconds 200
300 400
500 600
100
R
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Ordered Direct Access File
Parts – Worst Delete
SecWorst Binary Search To Find Location Log2 (1,000,000) = 20 20/200 = .1 Seconds 200
300 400 500
600W
100
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Ordered Direct Access File
Parts – Worst Delete
SecWorst Binary Search To Find Location Log2 (1,000,000) = 20 20/200 = .1 Seconds 200
300 400 500
600
100
R
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Ordered Direct Access File
Parts – Worst Delete
SecWorst Binary Search To Find Location Log2 (1,000,000) = 20 20/200 = .1 Seconds 200
300 400 500 600
W
100
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Ordered Direct Access File
Parts – Worst Delete
SecWorst Binary Search To Find Location Log2 (1,000,000) = 20 20/200 = .1 Seconds
Read 999,999 = 4,999.995 SecWrite 999,999 = 4,999.995 SecUpdate Rec 0 = .005 Sec
200 300 400 500 600
100
10,000.095
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Ordered Direct Access File
Parts – Average Delete
Sec
Average Binary Search To Find Location Log2 (1,000,000)-1 = 20 -1 = 1919/200 = .095 Seconds
100 200 300 400 500 600
Location
50
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Ordered Direct Access File
Parts – Average Delete
SecAverage Binary Search To Find Location Log2 (1,000,000)-1 = 20 -1 = 1919/200 = .095 Seconds 200
300
400 500 600
100
R
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Ordered Direct Access File
Parts – Average Delete
SecAverage Binary Search To Find Location Log2 (1,000,000)-1 = 20 -1 = 1919/200 = .095 Seconds 200
300 400
500 600
W
100
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Ordered Direct Access File
Parts – Average Delete
SecAverage Binary Search To Find Location Log2 (1,000,000)-1 = 20 -1 = 1919/200 = .095 Seconds 200
300 400
500 600
100
R
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Ordered Direct Access File
Parts – Average Delete
SecAverage Binary Search To Find Location Log2 (1,000,000)-1 = 20 -1 = 1919/200 = .095 Seconds 200
300 400 500
600W
100
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Ordered Direct Access File
Parts – Average Delete
SecAverage Binary Search To Find Location Log2 (1,000,000)-1 = 20 -1 = 1919/200 = .095 Seconds 200
300 400 500
600
100
R
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Ordered Direct Access File
Parts – Average Delete
SecAverage Binary Search To Find Location Log2 (1,000,000)-1 = 20 -1 = 1919/200 = .095 Seconds 200
300 400 500 600
W
100
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Ordered Direct Access File
Parts – Average Delete
SecAverage Binary Search To Find Location Log2 (1,000,000)-1 = 20 -1 = 1919/200 = .095 Seconds
Read 500,000 = 2,500 SecWrite 499,999 = 2,499.995 SecUpdate Rec 0 = .005 Sec
200 300 400 500 600
100
5,000.095
Design Consideration 2
One Ordered Direct Access File
To Get It Ordered We Often Need
To Sort
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Ordered Direct Access File
Parts – Sort – Order N2
Days1,000,000 x 1,000,000 = 1,000,000,000,0001,000,000,000,000/200 = 5,000,000,000 Sec 5,000,000,000/60 = 83,333,333 Minutes83,333,333/60 = ~1,388,889 Hours1,388,889/24 = ~57,870 Days
~5,000.090
1
D
A
FILE
1,000,000
Assume We Have 1,000,000 Data ItemsAssume That For Our Hardware & Data Item Size
We Can Read 200 Data Items Per SecondAssume Our Data Is In One Ordered Direct Access File
Parts – Sort – Order N Log2N
Days1,000,000 x 20 = 20,000,00020,000,000/200 = 100,000 Seconds100,000/60 = 1,667 Minutes1,667 /60 = ~27.78 Hours27.78/24 = ~1.16 Days
~1.16
1
D
A
FILE
1,000,000
The user will be able to search 1,000,000 records by Part.No and display the sought record in no more than .5 seconds.
The user will be able to add a new part to
999,999 others in no more that
1.0 seconds.
Can’t Satisfy These Requirements Specifictions With One Ordered Array
Sec
Sec9,999.995
.1
Need Better!
Come To Next Class!
182