CSIT 208, Section Instructor: P eter C hen Introduction to Programming with QBasic to Visual Basic Lecture 8
CSIT 208, Section CLS INPUT “Enter the name of a corporation: “, corp$ LET foundFlag = 0 LET first = 1 LET last = 100 DO WHILE (first <= last) AND (foundFlag = 0) LET middle = INT(first + last)/2) SELECT case firm&(middle) CASE corp$ LET foundFlag = 1 CASE IS > corp$ LET first = middle – 1 CASE IS < corp$ LET first = middle + 1 END SELECT LOOP IF foundFlag = 1 THEN PRINT corp$; “ found” ELSE PRINT corp$; “ not found” END IF END Binary Search * Page 267 Assume the array firm$() contains the Alphabetized names Of 100 corporations
CSIT 208, Section Two-Dimensional Arrays CLS DIM rm ( 1 TO 4, 1 TO 4) CALL ReadMileages(rm()) CALL ShowCitites CALL ShowMileage(rm(), row, col) DATA 0, 2054, 802, 738 DATA 2054, 0, 2786, 2706 DATA 802, 2786, 0,100 DATA 738, 2706, 100, 0 END SUB InputCities (row, col) INPUT “Origin”; row INPUT “Destination”; col END SUB Continue … * Page 276
CSIT 208, Section SUB ReadMileages (rm()) FOR row = 1 TO 4 FOR col = 1 TO 4 READ rm (row, col) NEXT col NEXT row END SUB SUB ShowCities PRINT “1. Chicago” PRINT “2. Los Angeles” PRINT “3. New York” PRINT “4. Philadelphia” END SUB SUB ShowMileage (rm(), row, col) PRINT “ The road mileage is”; rm(row, col) END SUB
CSIT 208, Section Exercise Please add one more destination, (5. San Francisco) Distance: San Francisco Chicago, 1500 San Francisco Los Angeles, 200 San Francisco New York, 4000 San Francisco Philadelphia, 3500
CSIT 208, Section Chapter 8 - WRITE Statement CLS WRITE “Eric” WRITE 1946 WRITE “Eric”, 1946 LET a$ = “John” LET b$ = “Smith” WRITE 14 * 139, “J.P “ + a$, b$, “Amy” END * Page 303
CSIT 208, Section Sequential File OPEN “YOB.DAT FOR OUTPUT AS #1 READ name$, year DO WHILE name$ <> “EOD” WRITE #1, name$, year READ name$, year LOOP CLOSE #1 DATA Barbara, 1942 DATA Ringo, 1940 DATA Sylvester, 1946 DATA EOD, 0 END * Page 304
CSIT 208, Section Exercise Write a program to create the file “student.dat”. Use EOD as a sentinel to indicate that all the data have been read. Please input three student’s first name, last name, and ID Number.
CSIT 208, Section Adding Items to a Sequential File CLS OPEN “YOB.DAT” FOR APPEND AS #1 INPUT “Name, Year of Birth”; name$, year WRITE #1, name$, year CLOSE #1 END
CSIT 208, Section Exercise Write a program to append John Smith, ID number: 7773 into the student.dat file.