|
The American Programmer | |
| Home | Programming | Books for Computer Professionals | Privacy | Terms |
| Home > Programming > REXX Programming > REXX Language > REXX Practice Problems |
REXX Practice Problems. (Answers below)
This is to be used with the book "REXX Programming on TSO"
See this web page for ordering information: http://www.theamericanprogrammer.com/programming/rexx.programming.book.shtml
This document is at: http://www.theamericanprogrammer.com/programming/rexx2.prob.shtml
Revised 2006.
You'll find the answers about halfway through this document.
look for
******Suggested answers for workshops.******
******Suggested answers for workshops.******
1. In Chapter 3, after say.
Create and run the following REXX program using your name instead of Sue
Say 1 + 1
Say "Today is" Date()
Name = "Sue"
Say "Hello, " Name
2. In Chapter 3, after literal.
Say
Display the literal "hello"
display 1 + 1
display the length of the literal "hello" Say length(“hello”)
set the variable NUM to 5
display NUM + 5
3. In Chapter 3, after literal.
Type in this program without the line numbers.
Run this program.
Discuss the results.
1 /* rexx */
2 Say 1 + 1
3 Say "1 + 1"
4 Say "1" + "1"
5 Say one + 1
6 one = 1
7 Say one + 1
8 Say "one" + 1
4. In Chapter 3, after literal.
Type in this program without the line numbers.
Run this program.
Discuss the results.
1 /* rexx */
2 one = 1
3 Say one + 1
4 Drop one
5 Say one + 1
5. Variables. In Chapter 3, after concatenate data.
Set a variable to "Good morning"
Set a variable to your name. (Joe in the example)
Display Good morning Joe
Display Good morningJoe
Display Good morning (10 spaces here) Joe
6. In Chapter 3, after Passing Commands to TSO.
Run this program. Discuss the results.
say "enter a message"
pull message
say "enter userid to send to"
pull userid
"send '"message"'user("userid")"
7. In Chapter 3, after Passing Commands to TSO.
Execute the TSO command LISTDS 'your-userid.REXX.EXEC' MEMBERS
in a REXX program.
8. In Chapter 3, after Passing Commands to TSO.
Variables in TSO Command. Use of quotes.
Like the previous, but MEMBERS is now a variable.
Set it to nothing, in other words MEMBERS = ""
Now run the program. It should not display any members.
9. After Chapter 3.
What does this program do?
/*REXX SAMPLE*/
X = 10
Y = 20
Z = Y - X
SAY "THE ANSWER IS " Z
10. After Chapter 3.
Write an exec to execute the TSO commands:
TIME
SEND 'SAMPLE EXEC ' USER(*)
Include the comment: This is a sample exec for REXX class
Expected results will be approximately like this:
09:05:02 03/08/42 CPU 4.3 SVC 888.323
+ SAMPLE EXEC TSOU01 11. After Chapter 3.
What does this program do?
/*REXX TRYME*/
SAY GREETING
12. After Chapter 3.
What does this program do?
/*REXX TRY ME TOO*/
GREETING = "HELLO"
SAY GREETING
13. After Chapter 3.
Can you tell what this does? Try it out if you don't know.
/*REXX TRY ME TOO*/
GREETING = "HAPPY HALLOWEEN"
SAY GREETING
DROP GREETING
SAY GREETING
14. After Chapter 3.
What does this display on the screen?
SAY 'F2F3F4'X
15. After Chapter 3.
Write an exec that displays these lines exactly as shown:
3 + 1 is 4
'3 + 1 is 4'
O'brien
16. After Chapter 3.
Write an exec that executes this TSO command:
LISTCAT ENTRY('SYS1.PROCLIB')
17. After Chapter 3.
What does this exec print out?
/*REXX SHORT EXEC*/
MESSAGE = MESSAGE
SAY MESSAGE
EXIT
18. After Chapter 3.
In an exec, assign the number 10 to a variable.
Assign the number 20 to another variable.
In one instruction display the total of the two.
19. After Chapter 3.
First assign the variable USERID the value of your neighbor's TSO userid. Then execute the TSO command:
LISTCAT LEVEL(your neighbor's userid)
using the variable USERID in place of your neighbor's userid.
20. After Chapter 4, IF.
Write a program that will show you what happens when you create a number with more than 9 digits. Type this in. Run it with a trace I.
NUM1 = 9876543211
NUM2 = 9876543212
If Num1 = Num2 then say "they are equal"
else say "not equal"
21. After Chapter 4, IF.
What does this exec display?
A = 5
B = 4
SAY A = B
22. After Chapter 4, IF.
What does this exec display? Try it to find out.
A = 2 + 2 = 2
SAY A
These are displaying the truth value of the comparison
true is 1
false is 0
23. After Chapter 5. Looping, DO END
DO Type this in a program. Notice the results
DO
Say "hi"
Say "lo"
END
24. After Chapter 5. Looping, DO END
DO Type this in a program. Notice the results
DO 5
Say "hi"
Say "lo"
END
25. After Chapter 5. Looping, DO END
DO Type this in a program.
the first time, replace XXXXX with EXIT
2nd time replace with ITERATE
3rd time replace with LEAVE
4th time replace with SIGNAL BYE
Notice the difference
DO 10
Say "loop or stop now?"
Say "type STOP, or press enter to keep looping"
Pull Reply
If Reply = "STOP" then XXXXX
END
Bye: Exit
26. After Chapter 5. Looping, DO END
Nesting. Run this
DO Outer = 1 to 10
Do Inner = 1 to 5
Say Outer Inner
End Inner
End Outer
27. After Chapter 5. Looping, DO END
Nesting
Same as the previous, but change the last END OUTER to END INNER.
What is the result?
28. After Chapter 5. Looping, DO END
Write an exec that asks the user to enter a series of numbers, but never the same one twice in a row. Limit it to 10 numbers.
29. After Chapter 5. Looping.
Set secret number equal to 13. Write an exec to keep asking the user to enter a number from 1 to 20 until the secret number is guessed. Limit the loop to 10 guesses. Expected results:
===> %myexec
PLEASE ENTER YOUR GUESS, FROM 1 TO 20
===> 2
WRONG
===> 7
WRONG
===> 13
RIGHT
30. After Chapter 5. Looping, DO END
Loop 10 times. In the loop ask for a dataset name, then issue the TSO command LISTDS on the dataset. If the RC (return code) is 0, get out of the loop; otherwise display the message "not found" and then automatically repeat the loop.
31. After Chapter 6. Pull, If, Select
Select. Write a program that asks you for your name.
If you reply "JOHN" it displays "PAUL JONES"
If you reply "GEORGE" it displays "Washington"
NELSON -> Mandela
JULIUS -> Caesar
a blank, or just hitting Enter -> Please try again
any thing else, Bad reply.-> Please enter a name
32. After Chapter 7.
Parse Pull.
Set this up so that the only valid reply is Yes - Upper case Y, lower case e, s.
Ask for a reply. Get the reply.
If the reply is Yes display "correct answer" otherwise display "wrong answer"
33. After Chapter 7.
Write an exec that breaks up the information contained in a variable into three variables. Place this information in the first variable VAR1:
ABCDEFGHIJKLMNOP
Use the proper PARSE instruction to break up the first variable this way:
VAR2 gets columns 3 and 4
VAR3 gets columns 6 through 9
VAR4 gets columns 9 through 14
Display VAR2
Display VAR3
Display VAR4
Expected results:
CD
FGHI
IJKLMN
34. After Chapter 7.
What would this say?
/*REXX program TESTING */
ARG N1 N2 N3
SAY N2 N3 N1
if it is executed this way:
==> %TESTING MARYELLEN SUE KAREN
35. After Chapter 7.
Write an exec that will execute this TSO command:
LISTCAT ENTRY(dataset-name)
The dataset name is to be entered through an ARG. Make sure it works on datasets belonging to you as well as those belonging to someone else. Try executing it with these two dataset names:
REXXPRGS.EXEC
'SYS1.PROCLIB'
==> %myexec REXXPRGS.EXEC
==> %myexec 'SYS1.PROCLIB'
36. After Chapter 7.
This ARG, and this manner of execution, produces what display?
/*REXX addemup exec */
ARG NUM1 NUM2 NUM3
TOTAL = NUM1 + NUM2 + NUM3
SAY TOTAL
===> %ADDEMUP 10 20 30 40 50 60
37. After Chapter 7.
Write an exec that accepts three pieces of information and displays them in reverse order. If more than three are entered, display an error message. Expected results:
==> %myexec peter paul mary
MARY PAUL PETER
==> %myexec peter paul mary ringo
I said enter only three items please!
38. After Chapter 7.
You want the user to type in three and only three items (words) of information, separated by spaces. What does your PULL look like?
39. After Chapter 7.
Write an exec that asks for three words (only) and displays them in reverse order. Expected results:
===> %myexec a b c
C B A
===> %myexec a b c d
C B A
40. After Chapter 7.
Write a front-end exec for the DELETE command. Have it ask "Are you sure?" If the dataset name is "junk.data," don't ask. Expected results:
===> %erase abc.data
ARE YOU SURE? Y/N
===> n
NOT DELETED
===> %erase junk.data
DELETED
41. After Chapter 7.
Write an exec that computes the percent of increase of one number over another. Ask for the two amounts from the terminal. The formula is:
% increase = 100 * ((new - old) / old)
Imagine your old salary was $21,203 and your new is $80,000. You would figure the % increase as:
% increase = 100 * ((80000 - 21203) / 21203)
Expected results:
===> %myexec 100 120
20
42. After Chapter 9, traps.
Write an exec that asks for a dataset name and then issues the TSO command LISTDS on it. Set up an ERROR trap to intercept the command that is not working. In the ERROR trap, display the line of the program in error and the error code from TSO. Ask for the dataset name again, reexecute the command, and exit. Expected results:
===> %myexec
PLEASE ENTER DATASET NAME
===> abc.nonesuch.data
CANNOT EXECUTE TSO COMMAND
TSO ERROR CODE IS 12
LINE IN ERROR IS "LISTDS dsn"
PLEASE REENTER
===> abc.real.data
ABC.REAL.DATA VOLUME SERIAL D12345
LRECL 80 BLKSIZE 3120 RECFM FB
43. After Chapter 11, Math.
Write an exec that will multiply two numbers. Prompt the user for the two numbers, and set up a trap to intercept any error, such as invalid numbers.
44. After Chapter 11, Math.
Run this exec to determine the effect of parentheses on arithmetic operations. What is displayed?
SAY (1 + 2) * 3
SAY 1 + (2 * 3)
SAY 1 + 2 * 3
45. After Chapter 11, Math.
Write an exec that asks for a number (record length) and another number (block size). It determines if block size is an exact multiple of record length, that is, if record length was multiplied by a whole number to obtain block size. Expected results:
===> %myexec
PLEASE ENTER RECORD LENGTH
===> 80
PLEASE ENTER BLOCK SIZE
===> 3121
BLOCK SIZE IS NOT A MULTIPLE OF RECORD LENGTH
46. After Chapter 11, Math.
Create a simple exec to calculate the effect of compound interest. Ask for the following items from the terminal:
Principal
Percent of interest (yearly)
Number of years money kept in account
Compounding frequency (how many times per year is interest compounded?
1 = yearly; 12 = monthly; 52 = weekly; 365 = daily)
Perform the following calculations:
Compute the yearly rate of interest as yearly percent of interest / 100.
Compute r (the rate) as yearly rate of interest / compounding frequency.
Compute n (number of times compounded) as how many years X compounding frequency.
Compute final amount as principal times ((1 + r)n).
Display final amount.
Keep this program, since you will be improving on it later.
47. In Chapter 12. RC.
put these into a REXX program. Notice the results.
"LISTCAT LEVEL(your-userid) "
Say RC
"LISTCAT LEVEL(JUNK) "
Say RC
"LISTDOG LEVEL(your-userid ) "
Say RC
48. After Chapter 13. datatype, arg, math, look up the function
Accept two numbers from the command line, and check to be sure they are valid. Subtract one from the other. Drop the sign of the answer and display the answer. Expected results:
===> %myexec 100 20
80
===> %myexec 20 100
80
49. After Chapter 13.
Use SYSDSN to see if there exists a dataset named WORKSHOP.TEMP. If so, delete it. If not, allocate a new one like your Exec library. The TSO command is:
ALLOC DSN(WORKSHOP.TEMP) LIKE(REXXPRGS.EXEC)
50. After Chapter 13.
Redo the previous problem, supressing any display message from the DELETE command, using OUTTRAP.
51. After Chapter 13.
Redo the previous problem, supressing any display message from the DELETE command with the MSG function.
52. After Chapter 13.
Using the TRANSLATE function, create an exec that will convert one character string to another. Set it up so that if you type in:
GREAT
it will print out: BUENO.
53. After Chapter 13. arg, hex conversion functions
Write an exec that will accept a hex number, a plus or minus sign, and another hex number; then add or subtract based on the sign. Display the answer in hex. Expected results:
===> %hexmath e - 2
C
===> %hexmath e + 2
10
54. After Chapter 13.
Here is a working example of a program that will convert all the letters in a string of characters to lower case. Modify it so that it converts from lower case to upper case.
/*REXX LOWER EXEC converts upper to lower*/
parse arg instring
upper_alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
lower_alpha = "abcdefghijklmnopqrstuvwxyz"
outstring = translate(instring,lower_alpha,upper_alpha)
say outstring
55. In Chapter 14. writing your own function..
put these into a REXX program.
SAY Mysub(1,2)
Exit
Mysub:
ARG A, B
C = A + B
Return C
56. After Chapter 15.
Write an internal function that will concatenate two items passed to it (without spaces). Execute the internal function from within the same program. Expected results:
===> %myexec cat dog
CATDOG
57. After Chapter 15.
Write an internal subroutine will concatenate two items passed to it (without spaces). Execute the internal subroutine from within the same program. Expected results:
===> %myexec cat dog
CATDOG
58. After Chapter 15.
Redo the percent increase problem (problem 33) as an external function, naming it PERCENTI
Have it accept two parameters (that is, arguments): first, Old salary; second, New salary.
Have it return the percent increase on the RETURN statement.
Check both numbers for numeric.
If either is not valid, display a message and return a zero.
Note that there will be two members in your library: the function
and the main program that invokes the function.
59. After Chapter 15.
Using the solution to the CATDOG problem (problem 48), write your concatenate function as an external function
60. After Chapter 15. user written external functions, math, arg
Create a metric conversion external function named Metric. It will accept two arguments: UNIT and QUANTITY.
UNIT has these possible values: LITER QUART MILE KILOMETER.
It returns a number equivalent to QUANTITY in the other measuring system, based on this table:
1 liter = 1.057 quarts
1 quart = .946 liters
1 mile = 8/5 kilometers
1 kilometer = .625 miles
Also, write a main program that invokes this as a function. Expected results:
==> %myexec kilometer 1000
625 MILES
61. After Chapter 5. Looping, DO END
Write an external subroutine to compute the square root of a number passed to it. Name it SQRT. It will accept one ARG: the number. Validate the number. If negative, or not numeric, display a message and return a zero. Make your guess one-half of the number.
Repeat this loop 50 times.
Make new guess = (guess + (number / guess)) / 2
Make guess = new guess
End-repeat
Return the new guess to the caller
62. After Chapter 17 Compound variables
Write an exec with a loop that sets NUMBER.1 through NUMBER.10 to the numbers 101 through 110; then loop through the array, displaying the contents of NUMBER.1 through NUMBER.11 (yes, NUMBER.11 is what I said) to see what happens.
63. After Chapter 17 Compound variables. Outtrap. Loop.
Write a program that captures the display from TSO commands in an array and then displays the lines of output one at a time.
Display "Please enter a command."
Accept the command into a variable.
Turn on OUTTRAP using the array Display_line.
Execute the command. Turn off OUTTRAP.
Display the message: "About to display command's output."
Loop through the array, displaying each line of the command's output.
64. Chapter 18. Execio.
Choose a file that you currently have under your userid. Read it into an array. Display every other record, starting with record 1.
65. Chapter 18. Execio.
Read in a file you currently have under your userid into an array, and write it out to another file. Get both file names from the terminal by asking. To allocate the new file, use an allocate command with LIKE.
66. Chapter 18. Execio.
Read any file of yours into an array. Count each record, and keep track of the length of the longest one. At the end of the program, display the record count and the length of the longest record.
67. Chapter 18. Execio.
Modify the command output capture exec (Problem 64) to store the lines of the display in the dataset:
TEMP.CAPTURE.DATA
Use SYSDSN to see if that dataset exists. If it does, delete it. Then allocate it (create it) with the following TSO command:
ALLOC DDN(CAPTURE) DSN(TEMP.CAPTURE.DATA) NEW REUSE
SPACE(1 1) TRACKS LRECL(80) BLKSIZE(4000) RECFM(F B)
(Normally typed in on one line, not two.)
OUTTRAP loads an array. Use that array to write out the lines of the command display. The WRITE command should close the file at the same time.
68. Chapter 19.
Create a quick calculator exec. Use INTERPRET to execute an expression that is typed in, then display the result and name it CALC. It will be used this way:
==> %CALC 1 + 1
It will display "2"
Extra practice problem. Loop. Pull. Numeric datatype function.
Improve on your compound interest program written previously (problem 46) These are the added requirements
Put the terminal dialogue, calculations, and display into a loop that will repeat until you type in "NO." After getting the amounts from the terminal, verify the following:
If principal is blank, end the loop.
If percent blank, or years blank, or years over 100, or principal not numeric, or percent not numeric, or years not numeric, then start the loop over again.
*****************************Suggested answers for workshops.**********************************************
*****************************Suggested answers for workshops.**********************************************
*****************************Suggested answers for workshops.**********************************************
*****************************Suggested answers for workshops.**********************************************
Suggested answers for workshops.
1.
Just do it.
2.
Say "hello"
Say 1 + 1
Say length("hello")
num = 5
say num + 5
3.
Quotes around a valid number don’t stop REXX from doing math. Quotes around a variable prevent REXX from seeing the variable’s contents.
4.
An undefined variable (or one that was DROPped) can’t be used in math.
5.
greeting = "Good morning"
name = "Joe"
say greeting name
say greeting || name
or
say greeting""name
say greeting " "name
6.
It uses the TSO command SEND to send a message to another TSO user.
The variables message and userid must be out of quotes.
7.
"LISTDS 'userid.REXX.EXEC' MEMBERS"
8.
members = ""
"LISTDS 'userid.REXX.EXEC' " MEMBERS
9.
This program displays THE ANSWER IS 10
10.
/* REXX */
/* This is a sample exec for REXX class */
"TIME"
"SEND 'SAMPLE EXEC ' USER(*)"
11.
Displays GREETING
12.
Displays HELLO
13.
Displays HAPPY HALLOWEEN
GREETING
14.
Displays 234
15.
say "3 + 1 is 4"
say "'3 + 1 is 4'"
say "O'brien"
16.
"LISTCAT ENTRY('SYS1.PROCLIB')"
17.
Displays MESSAGE
18.
v1 = 10
v2 = 20
say v1 + v2
19.
userid = "TSOU02" /*for example*/
"LISTCAT LEVEL("userid")"
20.
They will be found equal, because a number with more than 9 digits is rounded.
21.
Displays 0
0 means the comparison is false.
22.
displays 0
the comparison of "2 + 2" and "2" is false.
23 – 27.
Just do it.
28.
previous = ""
do 10
say "please enter a number (not same as previous)"
pull number
if number = previous then say "that is same as previous"
previous = number
End
29.
secret = 13
do 10
say "PLEASE ENTER YOUR GUESS, FROM 1 TO 20"
pull number
if number = secret then do
say "RIGHT"
leave
end
else do
say "WRONG"
end
end
30.
do 10
say "PLEASE ENTER A DATASET NAME"
pull dsn
"LISTDS" dsn
if rc = 0 then do
leave
end
else do
say "not found"
end
end
-------------
31.
Say “Please enter name”
Pull name
select
when name = "JOHN" then say "PAUL JONES"
when name = "GEORGE" then say "Washington"
when name = "NELSON" then say "Mandela"
when name = "JULIUS" then say "Caesar"
when name = "" then say "Please try again"
otherwise
say "bad reply .> Please enter a name"
end
32.
Say "Please reply Yes! "
parse pull reply
if reply = "Yes" then say "correct answer"
else say "wrong answer"
33.
VAR1 = "ABCDEFGHIJKLMNOP"
Parse var VAR1 3 var2 5 6 var3 10 9 var4 15
say var2
say var3
say var4
34.
Displays SUE KAREN MARYELLEN
35.
arg dataset
"LISTCAT ENTRY("dataset")"
36.
Syntax error.
37.
arg v1 v2 v3 v4
if v4 <> "" then say "I said enter only three items please!"
say v3 v2 v1
38.
Say "Please type in three words (only)"
Pull word1 word2 word3 . /* the period throws away words after the third*/
39.
Say "Please type in three words (only)"
Pull word1 word2 word3 . /* the period throws away words after the third*/
Say word3 word2 word1
40.
arg dataset
if dataset = "JUNK.DATA" then "DELETE JUNK.DATA"
else do
Say "ARE YOU SURE? Y/N"
pull reply
if reply = "Y" then do
"DELETE " dataset
say "DELETED"
end
else do
say "NOT DELETED"
end
end
41.
Say "Please type in old salary"
Pull old
Say "Please type in new salary"
Pull new
increase = 100 * ((new - old) / old)
Say "the % of increase was" increase
42.
say "PLEASE ENTER DATASET NAME"
pull dsn
signal on error
"LISTDS" dsn
Exit
error:
say "CANNOT EXECUTE TSO COMMAND"
say "TSO ERROR CODE IS " rc
say "LINE IN ERROR IS " sourceline(sigl)
say "PLEASE REENTER"
pull dsn
"LISTDS" dsn
exit
43.
say "Please type in a number"
pull number1
say "Please type in another number"
pull number2
signal on syntax
say number1 * number2
exit
syntax:
say "a syntax error has occured. "
exit
44.
displays
9
7
7
45.
Say "PLEASE ENTER RECORD LENGTH"
pull lrecl
say "PLEASE ENTER BLOCK SIZE"
pull blksize
if (blksize // lrecl) = 0 then say "OK"
else say "BLOCK SIZE IS NOT A MULTIPLE OF RECORD LENGTH"
46.
say "Please enter principal"
pull principal
say "please enter yearly interest rate in percent"
pull percent
say "how many years is money kept in account?"
pull years
say "please enter how many times per year interest is compounded"
say "1 = yearly; 12 = monthly; 52 = weekly; 365 = daily"
pull frequency
yearly_rate = percent / 100
r = yearly_rate / frequency
n = years * frequency
final = principal * ((1 + r)**n)
say final
47.
Just do it.
48.
arg number1 number2
if datatype(number1) = "CHAR" then do
say number1 "is not a valid number"
exit
end
if datatype(number2) = "CHAR" then do
say number2 "is not a valid number"
exit
end
answer = number1 - number2
answer = abs(answer)
say answer
49.
if sysdsn(WORKSHOP.TEMP) = "OK" then do
delete "WORKSHOP.TEMP"
end
else do
"ALLOC DSN(WORKSHOP.TEMP) LIKE(REXXPRGS.EXEC)" end
50.
if sysdsn(WORKSHOP.TEMP) = "OK" then do
call outtrap "line.", "*" /* or x = outtrap("line.","*") */
delete "WORKSHOP.TEMP"
end
else do
"ALLOC DSN(WORKSHOP.TEMP) LIKE(REXXPRGS.EXEC)"
end
51.
if sysdsn(WORKSHOP.TEMP) = "OK" then do
call msg "OFF"
delete "WORKSHOP.TEMP"
end
else do
"ALLOC DSN(WORKSHOP.TEMP) LIKE(REXXPRGS.EXEC)"
end
52.
say "Please type in GREAT"
pull word
say translate(word,"BUENO","GREAT")
- or -
say translate(word,"ONEUB","TAERG")
53.
arg hex1 sign hex2
dec1 = x2d(hex1)
dec2 = x2d(hex2)
if sign = "+" then do
dec_answer = dec1 + dec2
end
if sign = "-" then do
dec_answer = dec1 - dec2
end
say d2x(dec_answer)
54.
parse arg instring
upper_alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
lower_alpha = "abcdefghijklmnopqrstuvwxyz"
outstring = translate(instring,upper_alpha,lower_alpha)
say outstring
55.
Just do it.
56.
Say concat_nate("cat" "dog")
exit
concat_nate:
arg var1 var2
return var1 || var2
/* or return var1""var2 */
57.
call concat_nate "cat" "dog"
exit
concat_nate:
arg var1 var2
return var1 || var2
/* or return var1""var2 */
58.
/*REXX main program to execute PERCENTI*/
say percenti(100000, 150000)
/*REXX external function PERCENTI*/
arg old, new
if datatype(old) = "CHAR" then do
say "invalid number" old
return 0
end
if datatype(new) = "CHAR" then do
say "invalid number" new
return 0
end
increase = 100 * ((new - old) / old)
return increase
59.
/*REXX main program*/
call concatn "cat" "dog"
exit
/*REXX CONCATN external function*/
arg var1 var2
return var1 || var2
/* or return var1""var2 */
60.
/*REXX main program*/
say metric("KILOMETER" 1000)
/*REXX external function metric*/
arg unit quantity
if unit = "LITER" then do
return quantity * 1.057 "quarts"
end
if unit = "QUART" then do
return quantity * .946 "liters"
end
if unit = "MILE" then do
return quantity * (8/5) "kilometers"
end
if unit = "KILOMETER" then do
return quantity * .625 "miles"
end
61.
/*REXX main program*/
call sqrt 25
/*REXX sqrt external function or subroutine*/
arg number
if number < 0 then do
say "number must be positive"
return 0
end
if datatype(number) = "CHAR" then do
say "number must be numeric"
return 0
end
guess = number / 2
do 50
new_guess = (guess + (number / guess)) / 2
guess = new_guess
end
return new_guess
62.
do i = 1 to 10
number.i = i + 100
end i
do i = 1 to 11
say number.i
end i
63.
say "please enter a command"
pull command
call outtrap "Display_line.", "*"
command
call outtrap "OFF"
say "About to display command's output"
do i = 1 to Display_line.0
say Display_line.i
end i
64.
sample_file = "my.sample.file"
"ALLOC DDN(INFILE) SHR REUSE DSN("sample_file")"
"EXECIO * DISKR INFILE (STEM INFILE. FINIS)"
"FREE DDN(INFILE)"
Do i = 1 to infile.0 by 2
say infile.i
end i
65.
sample_file = "my.sample.file"
sample_output = "my.sample.output"
"ALLOC DDN(INFILE) SHR REUSE DSN("sample_file")"
"EXECIO * DISKR INFILE (STEM INFILE. FINIS)"
"FREE DDN(INFILE)"
"ALLOC DDN(OUTFILE) NEW REUSE DSN("sample_output")",
"LIKE("sample_file")"
"EXECIO " infile.0 "DISKW OUTFILE (STEM INFILE. FINIS)"
"FREE DDN(OUTFILE)"
66.
sample_file = "my.sample.file"
"ALLOC DDN(INFILE) SHR REUSE DSN("sample_file")"
"EXECIO * DISKR INFILE (STEM INFILE. FINIS)"
"FREE DDN(INFILE)"
record_count = 0
max_length = 0
Do i = 1 to infile.0
record_count = record_count + 1
say infile.i
if length(infile.i) > max_length
then max_length = length(infile.i)
end i
Say "record count was " record_count
/* or infile.0 instead of record_count */
Say "length of longest record was " max_length
67.
say "please enter a command"
pull command
call outtrap "Display_line.", "*"
command
call outtrap "OFF"
if sysdsn("temp.capture.data") = "OK"
then "DELETE TEMP.CAPTURE.DATA"
"ALLOC DDN(CAPTURE) DSN(TEMP.CAPTURE.DATA) NEW REUSE",
"SPACE(1 1) TRACKS LRECL(80) BLKSIZE(4000) RECFM(F B)"
"EXECIO " Display_line.0 "DISKW CAPTURE (STEM Display_line. FINIS)"
"FREE DDN(CAPTURE)"
68.
arg expression
interpret "say" expression
Extra practice problem.
do until reply = "NO"
say "Please enter principal"
pull principal
if principal = "" then leave
if datatype(principal) = "CHAR" then iterate
say "please enter yearly interest rate in percent"
pull percent
if percent = "" then iterate
if datatype(principal) = "CHAR" then iterate
say "how many years is money kept in account?"
pull years
if years = "" then iterate
if years > 100 then iterate
if datatype(principal) = "CHAR" then iterate
say "please enter how many times per year interest is compounded"
say "1 = yearly; 12 = monthly; 52 = weekly; 365 = daily"
pull frequency
yearly_rate = percent / 100
r = yearly_rate / frequency
n = years * frequency
final = principal * ((1 + r)**n)
say final
say "do another calculation? YES/NO"
pull reply
end
For information on REXX:
http://www.theamericanprogrammer.com
![[Books Computer]](http://www.theamericanprogrammer.com/pix/rwb2_line.gif)
|
Home
|
Programming
|
Books for Computer Professionals
|
Privacy
|
Terms
|
Contact
|
|
Site Map and Site Search
|
Programming Manuals and Tutorials
|
The REXX Files
| Top of Page
|