Solutions

From BR Wiki
Jump to navigation Jump to search
Code for beginner tutorial exercises

Before looking ahead to the answers, it is best to do absolutely everything you can to figure out how to write the programs on your own. Struggling through each exercises (and I know, it can be very frustrating!) is how you will learn to program better. A lot of programming is thinking and analyzing the code, followed by a few minutes of actually typing in the solution. Do not worry if it takes you hours to write a seemingly simply program in the beginning. It’s all part of the process.

Having said that. If you have tried everything you can (including studying the BR Wiki for error explanations and help with syntax etc), then use this document as a resource to help with the programs. I would encourage you to study the differences between my examples and yours to determine what the problem is, not just copying mine.

And remember, as they say, there’s more than one way to skin a cat. There’s more than one way to write a program, and so if yours differ from the ones here, don’t worry, as long as it works for its intended purpose.

Here we go:

CHAPTER 2

1. Print 1 to 10

2. Put your name in a box:

In this example, lines 20 to 60 include lots of spaces to make up the design you see there. When you run this program, it looks like this:

CHAPTER 3

3.1 Extra Practice. Find the area:

3.3 Add CONV to AREA program above:

3.7 Extra Practice: The corrected code is highlighted in yellow:

10 Print “What is your favorite color?”
20 Input color$
30 Print “What is your favorite number?”
40 input numb
50 Print “The square root of your favorite number is”;SQR(numb)
60 Print “Your favorite color is”; color$
70 Stop

Punctuation matters!

CHAPTER 4

CHAPTER 5

5.1 Add a line label to line 20 and refer to it as the line-ref in the GOTO statement

5.2 Extra Practice

00010  ! Input Shape        ! #autonumber# 10,10
00015  L2: print "Would you like to find the area of a:"
00020     print "(1) circle"
00025     print "(2) rectangle"
00030     print "(3) triangle?"
00035     input Shape
00040     if Shape=1 then
00045        goto CIRCLE1
00050     else if Shape=2 then
00055        goto RECT1
00060     else if Shape=3 then
00065        goto TRI1
00070     end if
00075  CIRCLE1: print "To find the area of a circle, what is the radius?"
00080     input Radius conv L80
00085     let Area=Pi*Radius**2
00090     print "The area is"
00095     print Area
00100     goto again
00105  !
00107  L80: print "Please enter radius in number form"
00109     retry
00111  !
00113  RECT1: print "To find the area of a rectangle, what is the length?"
00115     input Length
00117     print "and the height?"
00119     input Height
00121     print "The area is"; Height*Length
00123     goto again
00125  !
00127  TRI1: print "To find the area of a triangle, what is the height?"
00129     input Ht
00131     print "and the width?"
00133     input Wt
00135     print "The area is";Ht*Wt/2
00137  !
00139     again: print "Would you like to calculate the area of another shape?  (Y/N)"
00141     input Again$
00143     if Uprc$(Again$)="Y" then
00145        goto L2
00147     else if Uprc$(Again$)="N" then
00149        stop
00151     end if
00153  !

5.5 If Business Rules! resumed execution with the GOSUB statement after returning from a previous GOSUB, then BR would simply go into the 2nd GOSUB, then execute code in the 2nd GOSUB until BR encounters a RETURN

5.7 Chapter exercises

1.

2. Comparing two loans. Due to the length of the program, it’s presented as a text file.

00001     print "COMPARE TWO LOANS USING THE "
00002     print "COMPOUND INTEREST INVESTMENT CALCULATOR"
00003     print "                         "
00004     print "Please state the initial amount of money you'd like to invest."
00005     input Prncpl conv CNVRSN
00006     print "Thank you, now what is the interest rate in percent?"
00007     input Rate conv CNVRSN
00008     let Rate = Rate/100
00009     print "And how many years is this investment?"
00010     input Yrs conv CNVRSN
00011     let Bal = Prncpl*(1+Rate/12)**(12*Yrs)
00012     print "investment: $";Prncpl;"rate (compounded monthly):";Rate*100;"%"
00013     print "                            "
00014     print "The value of the investment after";Yrs;"years is: $";Bal;"."
00015  !
00016     pRINT "Is the second loan amount the same as the first? (Y/N)"
00017     input equal$
00018     if uprc$(equal$) = "Y" then gosub opt3 else gosub opt2
00019    if bal2 >bal then print "The second loan yields more money." else  print "The first loan yields more money."
00020    stop
00021  !
00022   opt2: ! This compares different principal balances
00023   print "Please state the second initial amount of money you'd like to  invest."
00024     input Prncpl2 conv CNVRSN
00025     print "Thank you, now what is the second interest rate in percent?"
00026     input Rate2 conv CNVRSN
00027     let Rate2 = Rate2/100
00028     print "And how many years is this second investment?"
00029     input Yrs2 conv CNVRSN
00030     let Bal2 = Prncpl2*(1+Rate2/12)**(12*Yrs2)
00031     print "investment: $";Prncpl2;"rate (compounded  monthly):";Rate2*100;"%"
00032     print "                            "
00033     print "The value of the investment after";Yrs2;"years is: $";Bal2;"."
00034  return
00035  !
00036  opt3:         ! this compares two loans with the same starting balance
00037  print "Thank you, now what is the second interest rate in percent?"
00038     input Rate2 conv CNVRSN
00039     let Rate2 = Rate2/100
00040     print "And how many years is this second investment?"
00041     input Yrs2 conv CNVRSN
00042     let Bal2 = Prncpl*(1+Rate2/12)**(12*Yrs2)
00043     print "investment: $";Prncpl;"rate (compounded  monthly):";Rate2*100;"%"
00044     print "                            "
00045     print "The value of the investment after";Yrs2;"years is: $";Bal2;"."
00046     return
00047  !
00048  CNVRSN: print "Please use numbers only. Exclude characters and letters."
00049     retry

3. To add rangechecks, there could be several solutions. One idea is to use if/then statements and line labels, like these examples, inserted where appropriate:

00006  inputp: input Prncpl conv CNVRSN
00007    if prncpl >1000000 then print "The principal amount must be less than $1,000,000. I don’t believe you’re that rich! Please re-enter the amount." : goto inputp
00030    inputr: input rate conv CNVRSN
00031    if rate >30 then print "Rates are never that good!! Please re-enter the rate. It must be under 30%.” : goto inputr

4. This modification could be completed by the following examples, added where appropriate:

00065 if rate2=0, then rate2=rate*100
00075 if yrs2=0, then yrs2=yrs

CHAPTER 6

Exercise 6.8, NEWSAL (Remember, there may be other ways to successfully solve these sample problems, listed below is just one way, for your reference).

CHAPTER 7

Extra Practice 7.4, NEWSAL

Extra Practice 7.6, NEWSAL with TAB(x)

Extra Practice 7.8, NEWSAL with FORM statements

Extra Practice 7.9, NEWSAL with dollar symbols

Chapter 7.10 Challenge: Really try this yourself before looking at the answer!

CHAPTER 8

Mid Chapter Exercise 8.2, Mayan pyramids...!

8.3 Chapter exercises:

1. 00010 FOR X=10 TO -8 STEP -3 00020 X 00030 NEXT X

2.

3. The total interest paid will be $10,824.77 Here is the complete program:

00001  ! Loan Amortization Program
00009  ! #autonumber# 100 10
00010  !
00011     print "What is the beginning balance of the loan?"
00012     input Prin
00013     print "What is the annual interest rate?"
00014     input Rate
00015     let Rate=Rate/12
00016     print "How long will you take to repay it? (in years)"
00017     input Yr
00018     let Mo=Yr*12
00019     let Mont=Prin*(Rate*(1+Rate)**Mo)/((1+Rate)**Mo-1)
00020     print "Your monthly payment is";Mont
00021     print
00022     print "Press any key to see the breakdown of monthly payments"
00023     KSTAT$(1)
00024     let balance=prin
00025     let monthly=mont
00026     print "Month";tab(9);"Interest";tab(18);"Principal";tab(28);"Balance"
00027     print "Number";tab(9);"Paid";tab(18);"Repayment";tab(28);"Remaning"
00028     for x=1 to Mo
00029                 let INT=rate*balance
00030                 let sumint=sumint+int
00031                 let repay=monthly-int
00032                 let sumrep=sumrep+repay
00033                 let balance=balance-repay
00034          print using line : x,int,repay,balance
00035          line: form  pic(###),pic($$$,$$$.##),pic($$$,$$$.##),pic($$$,$$$.##)
00036     next x
00037     print using line2: SUMint,sumrep
00038     line2: form "TOTAL:",pic($$$,$$$.##),pic($$$,$$$.##),x 5,"$0.00" 

CHAPTER 10

10.2 The yearly average (in this case YAV) can be calculated and printed by adding/changing the following lines:

00235       LET YAV=YAV+TEMPDIF(A)
00240    NEXT A

...

00263    LET YAV=YAV/12
00265    PRINT ! serves to drop the cursor down one line to separate text
00270    PRINT SPAV,NYAV, YAV

10.2 Challenge Question (CQ): Lines that have been changed have been highlighted

00002     dim Sptemp(12), Nytemp(12), Tempdif(12), Name$(12)
00003     data 6,17,38,49,66,75,93,84,77,67,42,22
00004     data 27,37,52,69,73,84,92,83,79,78,50,43
00005     data "January","February","March","April","May","June","July",  "August", "September","October","November","December"
00006     print "Month"; Tab(15); "St. Paul ";" New York ";" Average"
00007     print
00008     for J=1 to 12
00009        read Sptemp(J)
00010     next J
00011     for K=1 to 12
00012        read Nytemp(K)
00013     next K
00014     for N=1 to 12
00015        read Name$(N)
00016     next N
00017     for C=1 to 12
00018        let Tempdif(C)=Sptemp(C)-Nytemp(C)
00019  !   If Tempdif(C) <0 Then Let Tempdif(C)=Tempdif(C)*-1
00020        print using TRY: Name$ (C),Sptemp(C),Nytemp(C),Tempdif(C)
00022  TRY:  form C 15,N 5,N 10,N 9
00023     next C
00024     for A=1 to 12
00025        let Spav=Spav+Sptemp(A)
00026        let Nyav=Nyav+Nytemp(A)
00027        let Yav=Yav+Tempdif(A)
00028     next A
00029     let Spav=Spav/12
00030     let Nyav=Nyav/12
00031     let Yav=Yav/12
00032     print
00033     print using TRY: "Average",Spav,Nyav,Yav

Chapter 10 (End of Chapter) Exercise

Multiplication table:

00010    DIM MULT(9,9)
00020    FOR ROW=1 TO 9
00030       FOR COL=1 TO 9
00040          LET MULT(ROW,COL)=ROW*COL
00050       NEXT COL
00060    NEXT ROW
00070    FOR ROW=1 TO 9
00080       FOR COL=1 TO 9
00090          PRINT MULT(ROW,COL);
00095 ! Tbl: Form N 4
00100       NEXT COL
00110       PRINT 
00120    NEXT ROW

CHAPTER 11

11.6 Midchatper practice: Debugging. Here is the corrected program, with highlights on the corrections.

00020     print Newpage
00030     dim Field$(2)*20, Words$(2)*20, Refer$(2)*50, Entries(2)
00040     data "2,2,c 15","4,2,c 15"
00050     read Mat Field$
00060     data "Product Name","Product Quantity","2, 20, c 15","4,20,c 12"
00070     read Mat Words$
00080     read Mat Refer$
00100     print fields mat Field$: mat Words$
00110     input fields mat Refer$: name$,city$

11.7 Mid-Chapter Practice

00020     print Newpage
00030     dim Field$(2)*50, Words$(2)*50, Refer$(2)*50, Entries(2)
00040     data "2,2,cr 15, r /w:w","4,2,cr 15, s/r:b"
00050     read Mat Field$
00060     data "Security Code","Current City","2, 20, c 15, ir/w:w","4,20,c 12, r/r:b"
00070     read Mat Words$
00080     read Mat Refer$
00100     print fields mat Field$: mat Words$
00110     input fields mat Refer$: name$,city$

11 Chapter Exercise: Clean up FLSCREEN !

00010 ! .! #AutoNumber# 10,10
00020    PRINT NEWPAGE
00050    PRINT FIELDS "2,6,c 35, /w:W": "Please Enter the Following:"
00060    DIM FLDEFNS$(6)*40, PROMPT$(6)*15, INPUTFLD$(6),ANSWERS$(6)*20
00070    DATA "4,6,cR 20, /b:w","6,6,cR 20, h/b:w","8,6,cR 20, /b:w","10,6,cR 20, /b:w","12,6,cR 20, /b:w","14,6,cR 20,/b:w"
00080    READ MAT FLDEFNS$
00090    DATA "Security Code:","Contact Name:","ID Number:","Company Name:","Street Address:","Website:"
00100    READ MAT PROMPT$
00110    PRINT FIELDS MAT FLDEFNS$: MAT PROMPT$
00130    DATA "4,40,c 6,i/r:w","6,40,c 20, /r:w","8,40,c 20, /r:w","10,40,c 20,  /r:w","12,40,c 20, /r:w","14,40,c 20, /r:w"
00140    READ MAT INPUTFLD$
00150    PRINT FIELDS: MAT ANSWERS$
00160    INPUT FIELDS MAT INPUTFLD$: MAT ANSWERS$

CHAPTER 14.3

MID-CHAPTER PRACTICE: Rewrite CHEKBOOK using arrays. Changes have been highlighted for reference.

00020     dim Fldef$(7)*30, Prompt$(7)*30,Inpdef$(7)*30, payee$*25,  entries$(2)*25
00030  ! .!values for fldef$
00040     print newpage
00050  data "8,4,c 20, a/w:w","10,4,c 20, a/w:w","12,4,c 20, a/w:w","14,4,c 25, a/w:w","16,4,c 25, a/w:w","18,4,c 20, a/w:w","20,4,c  20, /w:w"
00060  read mat fldef$
00070  ! .  !values for prompt$
00080     data "Check Number", "Amount", "Type of Transaction", "Date Written (ddmmyy)", "Date Cleared(ddmmyy)","Payee","Account Number"
00090  read mat prompt$
00100  ! .!values for input defs
00110   data "8,25,n 5, a/w:w","10,25,n 10.2, a/w:w","12,25,c 1, a/w:w","14,25,n 6, a/w:w","16,25,n 6, a/w:w","18,25,c 25, a/w:w","20,25,n 8, /w:w"
00120  read mat inpdef$
00130  !
00140   open #5: "name=chekbook.int,recl=63,use",internal,output,sequential
00150  begin: print !repeat entry again
00160     print newpage
00170  !
00180  print fields mat fldef$: mat prompt$
00190  input fields mat inpdef$: chnum, amount, typetran$, datew, datec,payee$,accnum
00200  !
00210  let entries(1)=chnum
00220  let entries(2)=amount
00230  let entries$(1)=typetran$
00240  let entries(3)=datew
00250  let entries(4)=datec
00260  let entries$(2)=payee$
00270  let entries(5)=accnum
00280  !
00290  write #5, using l700: entries(1), entries(2),entries$(1),entries(3),entries(4),entries$(2),entries(5)
00700 L700: FORM POS 16,C 1,POS 29,C 25,POS 1,N 5,N 10.2,POS 17,N 6,N 6,POS 54,N 8
00710  !
00720   print fields "24,4,c 44": "Would you like to add another entry?(Y/N)"
00730   input fields "24,45,c 1":rpt1$
00740  !
00750   if UPRC$(rpt1$)="Y" then goto begin
00760   close #5:

NOTE: In line 700, the Pos statements re-arrange the mat entries$ and entries$ so that BR uses two arrays, but keeps the values in the appropriate order within the data file.

Back to Index