# Solutions 2

SOLUTIONS for the Intermediate Tutorial (Tutorial 2).

These solutions are meant to provide a guideline as you work through the tutorial. However, there are often several ways to approach a programming challenge and solve each challenge. You may find that your solutions are different.

### Exercise 1.1

The following code demonstrates this as the user chooses what kind of order will be processed:

00150 let X\$="Overnight"
00160 let Y\$="Regular"
00170 print fields "12,5, c 30": "Shipping preference: "
00190 if FKEY=8, then print fields "15,5,c 30": "It will be there tomorrow!"
00200 if FKEY=9, then print fields "16,5,c 50": “It will be there with 3 to 7 business days!"
00220 if x\$(1:1)="^" then shipping\$="O"
00230 if y\$(1:1)="^" then shipping\$="R"

### Exercise 1.2

The following code augments our sample program with the ability to select which items to ship:

00002 dim boxnameform\$(3)*20,boxname\$(3)*20,box\$(3)*20
00004 data "22,15,c 10","23,15,c 10","24,15,c 10"
00005 data "Item 1","Item 2","Item 3"
00008 print fields "20,5,c 35": "Please select items to order:"
00009 print fields mat boxnameform\$:mat boxname\$
00010  !
00011 let X\$="one" : let Y\$="two"
00012 print fields "24,30,C 20": "Press Esc to quit"
00013 do
00014 input fields "22,14,CHECK 8,,10;23,14,CHECK 8,,11;24,14,CHECK 8,,12": box\$(1),box\$(2),box\$(3)
00015 print fields "24,1,N 2": Fkey
00016 loop While Fkey~=99

In this example, the DO LOOP structure allows the user to choose more than one checkbox, while the Escape key allows the user to exit the program when complete.

### Exercise 1.3 A

00260 dim Boxnameform\$(3)*20,Boxname\$(3)*20,Box\$(3)*20
00270 data "22,15,c 10","23,15,c 10","24,15,c 10"
00280 data "Item 1","Item 2","Item 3"
00310 print fields "20,5,c 35": "Please select items to order:"
00320 print fields Mat Boxnameform\$: Mat Boxname\$
00340 print fields "23,30,CC 8,,b99" : "Done"
00350 do
00360 input fields "22,14,CHECK 8,,10;23,14,CHECK 8,,11;24,14,CHECK 8,,12": Box\$(1),Box\$(2),Box\$(3)
00370 print fields "24,1,N 2": Fkey
00380 loop While Fkey~=99
00390 For I=1 to UDIM(box\$)
00400 if box\$(I)(1:1)="^" then ordered(I)=1 else ordered(I)=0
00410 next I

### Exercise 1.3B

00001  ! .! This is a sample for use of radio buttons, checkboxes, and buttons.
00010  ! .! #autonumber# 10,10
00020 print Newpage
00040 data "5,3,C 20","6,3,C 20","7,3,C 20","8,3,C 10","8,36,C 10","9,3,C 10"
00060 data "First Name: ","Last Name: ","Address: ","City: ","State: ","Zip Code: "
00080 data "5,20,C 30","6,20,C 30","7,20,C 30","8,20,C 15","8,43,C 2","9,20,C 7"
00110 print fields Mat Clientform\$: Mat Clientwords\$
00120  !
00140  !
00160 let X\$="^Overnight"
00170 let Y\$="Regular"
00180 print fields "12,5, c 30": "Shipping preference: "
00200 if Fkey=8, then print fields "15,5,c 20": "Ok then!"
00210 if Fkey=9, then print fields "16,5,c 20": "We'll do it!"
00220 if x\$(1:1)="^" then shipping\$="O"
00230 if y\$(1:1)="^" then shipping\$="R"
00240  !
00250  ! Check Boxes
00260 dim Boxnameform\$(3)*20,Boxname\$(3)*20,Box\$(3)*20
00270 data "22,15,c 10","23,15,c 10","24,15,c 10"
00280 data "Item 1","Item 2","Item 3"
00310 print fields "20,5,c 35": "Please select items to order:"
00320 print fields Mat Boxnameform\$: Mat Boxname\$
00340 print fields "23,30,CC 8,,b99" : "Done"
00350 do
00360 input fields "22,14,CHECK 8,,10;23,14,CHECK 8,,11;24,14,CHECK 8,,12": Box\$(1),Box\$(2),Box\$(3)
00370 print fields "24,1,N 2": Fkey
00380 loop While Fkey~=99
00390 For I=1 to UDIM(box\$)
00400 if box\$(I)(1:1)="^" then ordered(I)=1 else ordered(I)=0
00410 next I
00420  !
00430  ! Open File And Record Answers
00440 Open #1: "name=orders.int, recl=118,USE,kfname=lastfirst.int,kps=31/1,kln=30/30", internal, OUTIN, keyed
00450 write #1, using recform:mat answers\$,shipping\$,mat ordered
00460 recform: form c 30,c 30,c 30,c 15,c 2,c 7,c 1,n 1,n 1,n 1
00470 close #1:
00480  !

### 1.4 SAMPLIST.BR

00001  ! Print "this is a sample list then a grid"
00010  ! .! #autonumber# 10,10
00020 print newpage
00040 data "First Name”,”Last Name”,”Address”,”City”,”State”,”Zip Code”,”Shipping”,”Item 1”,”Item 2”,”Item 3"
00050 data 10,10,10,10,4,9,3,3,3,3
00070 f\$="cc 30,cc 30,cc 30,cc 15,cc 2,cc 7,cc 1,n 1,n 1,n 1"
00080 str2mat(f\$,mat forms\$,",")
00090  !
00110  !
00120 open #1:"name=orders.int, recl=118,USE", internal, outin
00130  !
00140 mat FirstName\$(0)
00150 mat LastName\$(0)
00170 mat City\$(0)
00180 mat state\$(0)
00190 mat zipcodes\$(0)
00200 mat shipmethod\$(0)
00210 mat item1(0)
00220 mat item2(0)
00230 mat item3(0)
00240  !
00270 recform: form c 30,c 30,c 30,c 15,c 2,c 7,c 1,n 1,n 1,n 1
00280  !
00290 let NewSize=udim(FirstName\$)+1
00300  !
00310 mat FirstName\$(NewSize)
00320 mat LastName\$(NewSize)
00340 mat City\$(NewSize)
00350 mat state\$(newsize)
00360 mat zipcodes\$(NewSize)
00370 mat shipmethod\$(NewSize)
00380 mat item1(NewSize)
00390 mat item2(NewSize)
00400 mat item3(NewSize)
00410  !
00480 shipmethod\$(NewSize)=shipping\$
00490 item1(NewSize)=ordered(1)
00500 item2(NewSize)=ordered(2)
00510 item3(NewSize)=ordered(3)
00520  !
00540  !
00550 DoneReading: ! We're done reading, go to the next part, print them on the list
00560  !
00570  !
00590 print fields "2,2,list 10/80,=r": (mat FirstName\$, mat LastName\$,mat Address\$,mat City\$,mat state\$,mat zipcodes\$,mat shipmethod\$,mat item1,mat item2,mat item3)
00600  !
00610 print fields "23,30,CC 8,,b99" : "Done"
00620 do
00630 input fields "2,2,list 10/80,rowsub,selone": selection
00640 loop while FKEY~=99
00650 print selection
00660  !
00670 close #1:
00680  !

### 1.5 GRID1

GRID1, part one:

00001  ! Print "this is a sample list then a grid"
00002  ! This Is A Sample Sort And Print File.
00010  ! .! #autonumber# 10,10
00020 print Newpage
00040 data "First Name","Last Name","Address","City","State","Zip Code","Shipping","Item 1","Item 2","Item 3"
00060 data 10,10,10,10,4,9,3,3,3,3
00080 data "cc 30","cc 30","cc 30","cc 15","cc 2","cc 7","cc 1","n 1","n 1","n 1"
00100  !
00120  !
00130 open #1: "name=orders.INT", internal, outin, relative
00140  !
00150 mat Firstname\$(0)
00160 mat Lastname\$(0)
00180 mat City\$(0)
00190 mat State\$(0)
00200 mat Zipcodes\$(0)
00210 mat Shipmethod\$(0)
00220 mat Item1(0)
00230 mat Item2(0)
00240 mat Item3(0)
00250 mat Recordnum(0)
00260  !
00270  !
00300 RECFORM: form C 30,C 30,C 30,C 15,C 2,C 7,C 1,N 1,N 1,N 1
00310  !
00320 let Newsize=Udim(Firstname\$)+1
00330  !
00340 mat Firstname\$(Newsize)
00350 mat Lastname\$(Newsize)
00370 mat City\$(Newsize)
00380 mat State\$(Newsize)
00390 mat Zipcodes\$(Newsize)
00400 mat Shipmethod\$(Newsize)
00410 mat Item1(Newsize)
00420 mat Item2(Newsize)
00430 mat Item3(Newsize)
00440 mat Recordnum(Newsize)
00450  !
00520 let Shipmethod\$(Newsize)=Shipping\$
00530 let Item1(Newsize)=Ordered(1)
00540 let Item2(Newsize)=Ordered(2)
00550 let Item3(Newsize)=Ordered(3)
00560 let Recordnum(Newsize)=Rec(1)
00570  !
00590  !
00600 DONEREADING: ! We're done reading, go to the nexst part, print them on the list
00610  !
00630 print fields "2,2,grid 10/80,=r": (Mat Firstname\$, Mat Lastname\$,Mat Address\$,Mat City\$,Mat State\$,Mat Zipcodes\$,Mat Shipmethod\$,Mat Item1,Mat Item2,Mat Item3)
00640  !
00650 print fields "23,30,CC 8,,b99" : "Done" ! Done Button
00660  !
00670 do
00680  ! .  ! writing changed selections to the file
00690 input fields "2,2,GRID 10/80,ROWCNT,CHG": Rows ! # Of Changed Rows
00700 mat Subscr(Rows) ! Redimension
00710 input fields "2,2,GRID 10/80,ROWSUB,CHG,NOWAIT": Mat Subscr ! Read Subscripts
00720  !
00730  ! .  ! redimension
00740 mat Firstname\$(Rows)
00750 mat Lastname\$(Rows)
00770 mat City\$(Rows)
00780 mat State\$(Rows)
00790 mat Zipcodes\$(Rows)
00800 mat Shipmethod\$(Rows)
00810 mat Item1(Rows)
00820 mat Item2(Rows)
00830 mat Item3(Rows)
00840  !
00850 input fields "2,2,GRID 10/80,ROW,CHG,NOWAIT": (Mat Firstname\$, Mat Lastname\$, Mat Address\$, Mat City\$, Mat State\$, Mat Zipcodes\$, Mat Shipmethod\$, Mat Item1, Mat Item2, Mat Item3) ! Read Changed Rows
00860  !
00870  ! Write To File
00880 for Index=1 to Udim(Mat Firstname\$)
00890 let Thesubscriptnumber=Subscr(Index)
00900 let Therecordnumber=Recordnum(Thesubscriptnumber)
00910 rewrite #1, using RECFORM, rec=Therecordnumber: Firstname\$(Index), Lastname\$(Index), Address\$(Index), City\$(Index), State\$(Index), Zipcodes\$(Index), Shipmethod\$(Index), Item1(Index), Item2(Index), Item3(Index)
00920 next Index
00930 loop Until Fkey=99
00940  !
00950 dim Newfirstname\$(1)*30, Newlastname\$(1)*30, Newaddress\$(1)*30, Newcity\$(1)*15, Newstate\$(1)*2, Newzipcodes\$(1)*7, Newshipmethod\$(1)*1, Newitem1(1), Newitem2(1), Newitem3(1)
00960  !
00970 print Newpage
00980 let Newfirstname\$(1)=" "
00990 let Newlastname\$(1)=" "
01010 let Newcity\$(1)=" "
01020 let Newstate\$(1)=" "
01030 let Newzipcodes\$(1)=" "
01040 let Newshipmethod\$(1)=" "
01050 let Newitem1(1)=0
01060 let Newitem2(1)=0
01070 let Newitem3(1)=0
01080  !

### GRID1 Part 2

01090 print fields "2,2,c 42": "Would you like to add a new record?"
01100 print fields "2,46,cc 8,,b98;2,55,cc 8,,b9": "Yes","No"
01110 let Kstat\$(1)
01120 if Fkey=9 then
01130 goto ENDALL
01140 end if
01150  !
01170 print fields "4,2,grid 10/80,=": (Mat Newfirstname\$, Mat Newlastname\$, Mat Newaddress\$, Mat Newcity\$, Mat Newstate\$, Mat Newzipcodes\$, Mat Newshipmethod\$, Mat Newitem1, Mat Newitem2, Mat Newitem3)
01180 print fields "23,30,CC 8,,b99" : "Done"
01190  !
01200 do While Fkey=98
01210 input fields "4,2,grid 10/80,row,chg": (Mat Newfirstname\$, Mat Newlastname\$, Mat Newaddress\$, Mat Newcity\$, Mat Newstate\$, Mat Newzipcodes\$, Mat Newshipmethod\$, Mat Newitem1, Mat Newitem2, Mat Newitem3)
01220 write #1, using RECFORM: Newfirstname\$(1), Newlastname\$(1), Newaddress\$(1), Newcity\$(1), Newstate\$(1), Newzipcodes\$(1), Newshipmethod\$(1), Newitem1(1), Newitem2(1), Newitem3(1)
01230 loop
01240  !
01250 ENDALL: ! Close File And End Program
01260 close #1:<br

### 2.3 NAMEFIND

00002 dim prinform\$(12)*12,prinform2\$(8)*12,lastname\$*30, first\$*30, last\$*30, address\$*30
00003 data "8,2,c 6","8,9,c 30","8,39,c 1","8,40,c 30","9,2,c 9","9,12,c 30","9,43,c 6","9,50,c 15","9,65,c 7","9,73,c 2","10,2,c 9","10,12,c 7"
00005 data "11,2,c 22","11,25,c 1","13,2,c 8","13, 12,n 1","14,2,c 8","14,12,n 1","15,2,c 8","15,12,n 1"
00007 print fields "2,2,c 50":"Which last name do you wish to search for?"
00008 input fields "4,2,c 30":lastname\$
00009  !
00010 open #1: "name=orders.INT, kfname=firstlast.int,kps=1,kln=60", internal, input, keyed
00012 readform: form c 30,c 30,c 30,c 15,c 2,c 7,c 1,n 1,n 1,n 1
00013  !
00014 print fields mat prinform\$: "Name: ",first\$," ",last\$,"Address: ",address\$,"City: ",city\$,"State: ",state\$,"Zipcode: ",zipcode\$
00015 print fields mat prinform2\$: "Overnight or Regular? ", shipmethod\$,"Item 1? ",item1,"Item 2? ",item2,"Item 3? ",item3

### Namefind Version two

This part accounts for two of the same last names.

00001  ! Print "this program will find specific records based on names"
00002 dim Prinform\$(12)*12,Prinform2\$(8)*12,Lastname\$*30, First\$*30, Last\$*30, Address\$*30
00003 data "8,2,c 6","8,9,c 30","8,39,c 1","8,40,c 30","9,2,c 9","9,12,c 30","9,43,c 6","9,50,c 15","9,65,c 7","9,73,c 2","10,2,c 9","10,12,c 7"
00005 data "11,2,c 22","11,25,c 1","13,2,c 8","13, 12,n 1","14,2,c 8","14,12,n 1","15,2,c 8","15,12,n 1"
00007 print fields "2,2,c 50": "Which last name do you wish to search for?"
00008 input fields "4,2,c 30": Lastname\$
00009  ! .! let lastname\$=UPRC\$(lastname\$)
00010  ! Open #1: "name=orders.INT, RECL=118, USE, kfname=firstlast.int,kps=31/1,kln=30/30", Internal, Input, Keyed  ! For Two Keys
00011 open #1: "name=orders.INT, kfname=firstlast.int,kps=1,kln=60", internal, input, keyed
00012 print fields "17,2,c 15":"Next record?"
00013 print fields "18,2,cc 10,,b8":"Yes"
00014 print fields "18,14,cc 10,,b9":"Quit"
00015  !
00017 READFORM: form C 30,C 30,C 30,C 15,C 2,C 7,C 1,N 1,N 1,N 1
00018 print fields Mat Prinform\$: "Name: ",First\$," ",Last\$,"Address: ",Address\$,"City: ",City\$,"State: ",State\$,"Zipcode: ",Zipcode\$
00019 print fields Mat Prinform2\$: "Overnight or Regular? ", Shipmethod\$,"Item 1? ",Item1,"Item 2? ",Item2,"Item 3? ",Item3
00020  !
00021  ! Prinform2: Form "11,2,c 22","11,25,c 1","13,2,c 8","13, 12,n 1","14,2,c 8","14,12,n 1","15,2,c 8","15,12,n 1"
00022 again: !
00023 do
00024 input fields "24,1,c 1": nomatter\$
00025 loop until fkey=8 or fkey=9
00026  !
00027 if FKEY=8 then
00029 print fields Mat Prinform\$: "Name: ",First\$," ",Last\$,"Address: ",Address\$,"City: ",City\$,"State: ",State\$,"Zipcode: ",Zipcode\$
00030 print fields Mat Prinform2\$: "Overnight or Regular? ", Shipmethod\$,"Item 1? ",Item1,"Item 2? ",Item2,"Item 3? ",Item3
00031 goto again
00032 else end

### 3.1 KSTAT

00001 dim words\$(5)*40,form\$(5)*25
00002 print Newpage
00003 data "5,5,c 30","6,5,c 30","7,5,c 30","8,5,c 30","9,5,c 30"
00007 print fields mat form\$:mat words\$
00008 print fields "12,5,c 40":"Enter menu number"
00009 print kstat\$(1)

### 3.2 UPRCS

Answers may vary. One solution is to change the following line:

### 3.3 LREC

00655 print fields "21,30,n 4":lrec(1)

### 3.4 TIME\$

Answers will vary. When writing the record, you will have to add TIME\$ to the WRITE line and n 8 to the form line, also update the record length in the OPEN statement.

### 5.1 STR2MAT

Two different possibilities are shown below, for mat headings\$ and mat forms\$

Lines 30 to 70 of our example above could be replaced by the following:

data 10,10,10,10,4,9,3,3,3,3
f\$="cc 30,cc 30,cc 30,cc 15,cc 2,cc 7,cc 1,n 1,n 1,n 1"
str2mat(f\$,mat forms\$,",")

### 5.2 SRCH

Note, your program might be different, but this is one successful option. With a short file, this task could be accomplished with FOR/NEXT and not SRCH, but in a very long list of clients, SRCH will speed it up considerably. The relevant code begins at line 380

00010 ! #autonumber# 10,10
00020 print Newpage
00040 open #1: "name=orders.INT", internal, input, relative
00060  !
00080  !
00100 openform: form c 30,c 30,c 30,c 15,c 2,c 7,c 1,n 1,n 1,n 1
00110  !
00120 let NewSize=udim(FirstName\$)+1
00130  !
00140 mat FirstName\$(NewSize)
00150 mat LastName\$(NewSize)
00170 mat City\$(NewSize)
00180 mat state\$(newsize)
00190 mat zipcodes\$(NewSize)
00200 mat shipmethod\$(NewSize)
00210 mat item1(NewSize)
00220 mat item2(NewSize)
00230 mat item3(NewSize)
00240  !
00310 shipmethod\$(NewSize)=shipping\$
00320 item1(NewSize)=ordered(1)
00330 item2(NewSize)=ordered(2)
00340 item3(NewSize)=ordered(3)
00350  !
00370  !
00380 DoneReading: ! We're done reading the file into arrays, next search for items.
00390  !
00400 print "The following people ordered all three items:"
00410 print "Row ";"Name"
00420  !
00430 do while SRCH(item1,1,y)>0
00440 let x=SRCH(item1,1,y)
00450 if item2(x)=1 and item3(x)=1 then
00460 print x;" ";firstname\$(x);" ";lastname\$(x)
00470 end if
00480 let y=x+1
00490 loop

### 5.3 DIDX

nexttry:  ! this
restore nexttry
dim salestotal(12)
data 300,500,66,789,1023,24,56,72,800,945,15,7