Lecture 7. AMES 5, Tues. 26 Jan. 1999

From the course outline we see the reading assignment this week is Chapter 5 on Standard Functions and Program Design. You need to turn in modified versions of all the example programs in this chapter, and do Practice Programs 5.1 and 5.2.

Arthmetic, trigonometric, string, conversion from string to numerical and back, ascii, keyboard, and mouse functions are discussed in Chapter 5. Single-line user-defined functions are introduced. Some of the concepts are:

• Standard functions are named identifiers to which one or more values are passed; eg. sqr(9)=3.
• Arithmetic functions; abs, int, round, truncate, fp, ip, sqr, mod, max, min
• Trigonometric functions; sin, cos, tan, atn, etc. Rad and deg convert between radians and degrees. Radian is default.
• Numerical functions; log, log2, log10, exp.
• Rnd, randomize.
• String functions; len, pos, posr, ucase\$, lcase\$, repeat\$, trim\$.
• Conversion of a character to ascii and back; ord, chr\$. Conversion between numeric and string values; val, str\$.
• Print and keyboard functions; tab, clear, sound, pause, key input, get key.
• The def fn statement for user-defined functions.
• Design of a computer program with pseudocode. Program testing.

All of these concepts are illustrated by the Example Programs which you will modify in your homework.

The design of an original computer program is illustrated by Practice Program 4.9, where a Fibonacci series (eg: 1,1,2,3,5,8,13,21,34,55 etc.) for length n is computed. The use of string functions, pseudocode, subroutines, and short practice programs PP4.9.1 and PP4.9.2 to develop the longer program is illustrated. Possible practice programs PP4.1 and PP4.2 were emailed to Anissa Barnett.

Practice Program 5.7 tests a word or phrase to see if it reads the same backwards as forwards. If it does, it is called a "Palindrome". A famous example is the alleged quote from Napoleon: "Able was I ere I saw Elba".

Quiz 4a Solution

Lecture8. AMES 5, Thurs. 28 Jan. 1999

In order to test the behavior of T-BASIC functions, it is useful to write short programs using the function that can be run several times with different parameters. For example, to test the function abs(x):

The program:

PRINT abs(-1.2345)

END

Gives output:

1.2345

and the program:

! Print out the ascii codes and associated characters.

! Use functions ord(a\$) and chr\$(x), x is 0 to 255.

LET index = 0

DO

LET index = index + 1

PRINT index;chr\$(index),

LOOP while index < 255

END

Gives output:

ascii code table.

A program to test the random number function and also to explore graphics from Chapter 12 is:

Random circles and sounds:

SET MODE "graphics"

RANDOMIZE

LET i=1

DO while i <50

LET ratio=1.4

LET x=rnd

LET y=rnd

LET rad=(rnd-.5)

SET WINDOW -2*ratio,2*ratio,-2,2

SET COLOR -10*rnd

DRAW circle (rad,x,y)

SOUND 1000*rnd,2*rnd

PAUSE .2

LET i=i+1

LOOP

END

PICTURE circle (r,x,y)

BOX CIRCLE x-r,x+r,y-r,y+r

END PICTURE

Try it!

Quiz 4b Solution

Help