Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSIT 208, Section 32301 Instructor: P eter C hen Introduction to Programming with QBasic to Visual Basic Lecture 8.

Similar presentations


Presentation on theme: "CSIT 208, Section 32301 Instructor: P eter C hen Introduction to Programming with QBasic to Visual Basic Lecture 8."— Presentation transcript:

1 CSIT 208, Section 32301 Instructor: P eter C hen Introduction to Programming with QBasic to Visual Basic Lecture 8

2 CSIT 208, Section 32302 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

3 CSIT 208, Section 32303 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

4 CSIT 208, Section 32304 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

5 CSIT 208, Section 32305 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

6 CSIT 208, Section 32306 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

7 CSIT 208, Section 32307 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

8 CSIT 208, Section 32308 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.

9 CSIT 208, Section 32309 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

10 CSIT 208, Section 323010 Exercise Write a program to append John Smith, ID number: 7773 into the student.dat file.


Download ppt "CSIT 208, Section 32301 Instructor: P eter C hen Introduction to Programming with QBasic to Visual Basic Lecture 8."

Similar presentations


Ads by Google