Chapter 10: Difference between revisions

From BR Wiki
Jump to navigation Jump to search
 
Line 222: Line 222:
|6  
|6  
|27  
|27  
|-21  
| -21  
|-
|-
|February  
|February  
|17  
|17  
|37  
|37  
|-20  
| -20  
|-
|-
|March  
|March  
|38  
|38  
|52  
|52  
|-14  
| -14  
|-
|-
|April  
|April  
|49  
|49  
|69  
|69  
|-20  
| -20  
|-
|-
|May  
|May  
|66  
|66  
|73  
|73  
|-7  
| -7  
|-
|-
|June  
|June  
|75  
|75  
|84  
|84  
|-9  
| -9  
|-
|-
|July  
|July  
|93  
|93  
|92  
|92  
|1  
| 1  
|-
|-
|August  
|August  
|84  
|84  
|83  
|83  
|1  
| 1  
|-
|-
|September  
|September  
|77  
|77  
|79  
|79  
|-2  
| -2  
|-
|-
|October  
|October  
|67  
|67  
|78  
|78  
|-11  
| -11  
|-
|-
|November  
|November  
|42  
|42  
|50  
|50  
|-8  
| -8  
|-
|-
|December  
|December  
|22  
|22  
|43  
|43  
|-21  
| -21  
|-
|-
|Yearly  Average  
|Yearly  Average  
|53  
|53  
|63.91667  
|63.91667  
|-10.91666  
| -10.91666  
|}   
|}   



Latest revision as of 18:30, 9 March 2017

Introduction

This chapter will teach about Arrays.  When you’re done you should be able to:

  • Describe an array, and understand the difference between a one-dimensional and a multi-dimensional array.  (matrixes)
  • Explain the difference between a subscripted and an unsubscripted variable.
  • Explain the purpose of the DIM statement.
  • Perform mathematical operations using arrays.
  • Print an array using nested FOR/NEXT loops

10.1 Introduction to one-dimensional Arrays

Imagine a situation where you would want to compare a record of the average monthly temperatures for a year in St. Paul, Minnesota, to those in New York, New York.  A table showing these figures might be as follows:

Month SP NY
January 6 27
February 17 37
March 38 52
April 49 69
May 66 73
June 75 84
July 93 92
August 84 83
September 77 79
October 67 78
November 42 50
December 22 43

Before you could compare these figures within a program, you would need some way to refer to them as variables.  St. Paul’s January temperature could for instance, be assigned to the variable SPJAN, and New York’s January temperature could be NYJAN. 

For example:

00010 LET SPJAN=6 
00020 LET SPFEB=17 
…
000150 LET NYJAN=27 
000160 LET NYFEB=37
...

When all the variables were assigned, you would have 24 separate variables and 24 different variable names to remember.  Juggling all these variables and their names would eventually become a very clumsy process.

A more convenient way to handle the numbers would be to assign each of the two lists of temperatures to its own variable.  All of the St. Paul temperatures could then be referred to as SPTEMP, and all the New York temperatures could be called NYTEMP.  You would then have two variable names instead of 24 to remember.  Business Rules! calls a list of values such as these an array. If pictured, it would be arranged like this:

SPTEMP=6 17 8 49 66 75 93 84 77 67 42 22
NYTEMP= 27 37 52 69 73 84 92 83 79 78 50  43 

Once you have assigned a list of values to an array, Business Rules! allows you to refer to each individual value within the list by its position number. For example:

SPTEMP (1) = 6 and NYTEMP(6)=84  

St. Paul’s January temperature would then become SPTEMP(1) because it is the first value in the SPTEMP array.  Likewise, New York’s June temperature would be NYTEMP(6).  The position number is called a subscript, and it is always placed in parentheses after the variable name.

The DIM statement

To assign a list of values to an array, you must first tell Business Rules! how many values will be in the list.  This is done with the DIM statement.  The DIM syntax that you would use for our lists of St. Paul and New York’s temperatures is:

line# DIM array-name (#of values)   

The “array-name” portion of the syntax is the name of the variable you wish to use. (Array variables, which are often referred to as subscripted variables, follow the same naming rules as the unsubscripted variables you have already learned about.)

The “(#of values)” portion of the syntax indicates the number of values in the list; this number must be placed within parentheses, and must be an integer number, not a variable.   You may repeat the “array-name” and “(#rows)” parameters when you wish to dimension more than one array in the same DIM statement, but you must separate each set of specifications with a comma.  As an example, the DIM statement for our St. Paul and New York temperatures example would be as follows:  

00050 DIM SPTEMP(12), NYTEMP(12)   

This statement tells Business Rules! to reserve space for twelve values in both the SPTEMP and NYTEMP “manila folders”.  Business Rules! automatically assigns a value of 0 to each element in the array until you explicitly assign another. DIM is a non-executable statement; it can be placed anywhere in a program. This is because Business Rules! automatically checks an entire program for DIM statements before every program execution.  Many programmers like to place all their DIM statements either at the beginning or end of a program for easy reference.

Assigning values to variables

Once you have used DIM to tell Business Rules! how many values will be assigned to a particular array name, you can go about assigning those values.  The same statements that you used to assign values to unsubscripted variables--LET, INPUT, READ, and DATA--can also be used to assign subscripted variables. The tricky part about assigning values to subscripted variables is that you must assign the right value to the right subscript.  One way to do this is to individually assign values to the subscripted variables with LET statements.  You could go through and assign each value in the following manner for instance:

00070 DIM SPTEMP(12) 
00080 LET SPTEMP(1)=6 
00090 LET SPTEMP(2)=17 
00100 LET SPTEMP(3)=38 
00110 LET SPTEMP(4)=49 
00120 LET SPTEMP(5)=66 
00130 LET SPTEMP(6)=75 
00140 LET SPTEMP(8)=93 
00150 LET SPTEMP(9)=84 
00160 LET SPTEMP(10)=77 
00170 LET SPTEMP(11)=42 
00180 LET SPTEMP(12)=22 

Another way that you could individually assign the values is with the DATA and READ statements:  

00070 DIM SPTEMP(12) 
00080 DATA 6,17,38,49,66,75,93,84,77,67,42,22 
00090 READ SPTEMP(1),SPTEMP(2),SPTEMP(3),SPTEMP(4),SPTEMP(5),SPTEMP(6),SPTEMP(8),SPTEMP(9),SPTEMP(10),SPTEMP(11),SPTEMP(12) 

As you can probably see, both these methods require a lot of typing and a great deal of repetition.  (And when you start to use a great deal of repetition in programming, it’s usually a sign that there’s an easier and quicker way to do whatever you’re doing!)  One easier and quicker way to assign values to an array is to use a FOR/NEXT loop along with READ and DATA statements.  The following set of program lines assigns the average monthly temperatures for both St. Paul and New York to their respective array variables:

00070 DIM SPTEMP(12), NYTEMP(12) 
00079 ! data for sptempt
00080 DATA 6,17,38,49,66,75,93,84,77,67,42,22 
00089 ! data for nytemp
00090 DATA 27,37,52,69,73,84,92,83,79,78,50,43 
00100 FOR J=1 TO 12 ! Loop to assign values to SPTEMP 
00110   READ SPTEMP(J) 
00120 NEXT J ! Loop to assign values to NYTEMP 
00130 FOR K=1 TO 12 
00140   READ NYTEMP(K) 
00150 NEXT K 
Because BR can’t handle comments in a DATA statement, they have been placed in the line above each DATA statement.

Let’s follow the execution sequence of this example.  First, Business Rules! takes note of (but does not execute) lines 70 through 90.  Next, it enters the FOR/NEXT loop at line 100.  The READ statement at line 110 instructs Business Rules! to assign a data item to SPTEMP(J). Since this is the first time through the loop, J equals one.

The first available data item, 6, will thus be assigned to the first position in the array, SPTEMP(1).

The second time through the loop, J will equal two.  Thus the next available data item, 17, will be assigned to SPTEMP(2).  This looping process will continue until J is greater than 12.  At that point, Business Rules! will jump to line 130 and right into another loop which conducts the same operation for the array variable NYTEMP.  

Each of the examples in this section has referred only to numeric array variables, but it is also possible to assign string values to string array variables.  The following example, for instance, uses an INPUT statement to assign string values to the array EMPLOYEE$. 

00060 DIM EMPLOYEE$(14) 
00070 FOR X=1 TO 14 
00080   PRINT “Enter employee’s name” e
00090   INPUT EMPLOYEE$(X) 
00100 NEXT X 

Quick Quiz 10.1

1. Which of the following definitions best describes an array?

a. A subscripted variable which follows the same naming rules as simple variables.
b. A list of values which are assigned to the same position within the list.
c. A programming statement which tells Business Rules! how many values will be assigned to a particular variable.

2. Which of the following variables are subscripted?

a. JKX(X)
b. C10
c. JKX
d. C$(10)

Answers 10.1

10.2 Performing comparisons & Arithmetic operations with Arrays

In the last lesson you learned to assign values to one-dimensional arrays. Let’s now use those arrays in some arithmetic operations.

Arithmetic operations with arrays

Our original intent in the last lesson was to compare the average monthly temperatures between St. Paul and New York; let’s do that now.  To start off, let’s have our program list the difference in average temperatures between the two cities for each month. While we’re at it, we might as well assign this list to a third program array: TEMPDIF.  You might have already guessed that this operation will employ another FOR/NEXT loop:

00070 DIM SPTEMP(12), NYTEMP(12), TEMPDIF(12) 
o 
o 
o 
00170 FOR C=1 TO 12 ! Loop to find difference in monthly temps 
00180   LET TEMPDIF(C)=SPTEMP(C)-NYTEMP(C) 
00190   PRINT SPTEMP(C),NYTEMP(C),TEMPDIF(C) 
00200 NEXT C 

In the above example, line 180 uses the loop variable to regulate which item in the list is calculated.  The first time through the loop, it calculates the difference between the first items (the January temperatures) in the St. Paul and New York temperature lists.  It then assigns the value of the difference to the first element in the TEMPDIF array.  The second time through the loop, line 180 calculates the difference between the second items in the temperature lists and assigns the difference to the second element of TEMPDIF.  This process continues until the loop variable is greater than 12.  

Notice that values were assigned to TEMPDIF in a slightly different manner here than when we assigned values to the other two arrays.  Instead of placing the values into DATA statements and instructing Business Rules! to READ them, we told Business Rules! to generate the values (from an arithmetic operation with other values) and then immediately place them into an array.  

Also notice that we went back to the DIM statement in line 70 and added the dimensions for the TEMPDIF array.

Arithmetic operations within arrays

You’ve now seen how to perform arithmetic operations from the values of one array to the values of another, but it is also possible to perform these operations within arrays.  As an example, let’s imagine that you want to find out the average yearly temperature (based on the average monthly temperatures) for both St. Paul and New York.  To find this information, you must add the twelve values in each array together, and then divide by twelve.  And guess what? You can use a FOR/NEXT loop to do this too!  

00210 FOR A=1 TO 12 ! Loop to find average yearly temperatures 
00220   LET SPAV=SPAV+SPTEMP(A) ! SPAV=St. Paul yearly average 
00230   LET NYAV=NYAV+NYTEMP(A) ! NYAV=New York yearly average 
00240 NEXT A 
00250 LET SPAV=SPAV/12 
00260 LET NYAV=NYAV/12 
00270 PRINT SPAV,NYAV 

Now try one for yourself:  What statements should you add to the above set of lines so that it also computes and prints the average yearly difference (based on the average monthly differences contained in the TEMPDIF array) in temperatures between St. Paul and New York?

Quick Quiz 10.2

1. The following section of program code should compute and print the average of the six values in the POPCAL array, but it contains a bug.  Can you discover it?

07000 FOR A=1 TO 6 
07010   LET POPAV=POPAV+POPCAL(A) 
07020   LET POPAV=POPAV/6 
07030 NEXT A 
07040 PRINT POPAV

Answer 10.2

Challenge Question

The St. Paul/New York temperature comparison program currently prints out three columns of unidentified information.  Using a fourth array (a string array) which contains a list of the months in the year, and using the information that you learned in your chapter on PRINT, make your output look as follows:

Month SP NY Dif
January 6 27 -21
February 17 37 -20
March 38 52 -14
April 49 69 -20
May 66 73 -7
June 75 84 -9
July 93 92 1
August 84 83 1
September 77 79 -2
October 67 78 -11
November 42 50 -8
December 22 43 -21
Yearly  Average 53 63.91667 -10.91666

 

CQ: Compare your program to the program TEMPID on the Solutions page.

10.3 Using MAT to perform arithmetic with arrays

So far we have shown you how to manipulate arrays only in long-hand fashion.  There is still a faster and easier method of handling many array functions however, and that is with the MAT keyword.

In conjunction with READ, the Business Rules! MAT keyword allows you to assign values to an array without using a FOR/NEXT loop.  MAT can also be used as a separate statement to quickly and easily re-dimension, perform arithmetic with, and sort arrays.

Assigning values to arrays with READ MAT

The simplest way to assign values to a variable is with the DATA and the READ MAT statements.  As an example, let’s assign the values Andrew, Rhonda, Silvia, Jack and Warren to the string array NAME$, and the values 17900, 89500, 26500, 86000 and 47000 to the numeric array SAL:

When used in the READ statement, the MAT keyword tells Business Rules! that it should assign values to an array (which is also sometimes called a matrix).  Business Rules! then checks the size (as indicated in the DIM statement) of the specified array before proceeding to assign values to it in row-by-row order.   You can also use the MAT keyword with the PRINT statement to print an array, as in the following example:

00070 PRINT MAT NAME$ 
00080 PRINT MAT SAL   

The above program lines would print the following:  

Re-dimensioning arrays

Let’s imagine that the following table represents the current yearly salaries of five people in your company.  The information in the first column is contained in the one-dimensional array NAME$, and the information in the second column is contained in the one-dimensional array SAL:

Andrew 17900
Rhonda 89500
Silvia 26500
Jack 86000
Warren 47000

Since the SAL array contains five elements, the DIM statement that originally dimensioned it also specifies five elements.  But let’s imagine that now (many program lines and operations after the original DIM statement) you want to add the names of four more employees to the list.  The MAT statement allows you to re-dimension the array so that it will now have room for nine values instead of five.  The syntax of this use of MAT is as follows:

line# MAT array-name(#rows) 

The MAT statement for our example would then be the following:

00980 MAT SAL(9) 

Using MAT to perform arithmetic with arrays

Since your company’s policy is that employees get an automatic cost-of-living pay increase in February of each year, you need to find a simple way to compute the new salaries.  This year’s pay increase is 6.4% of the current salary.   In addition to allowing you to re-dimension arrays, the MAT statement allows you to add, subtract, multiply or divide each element in an array by another value.  The syntax for MAT in this instance would be:

line# MAT array-name = (num-expr or array name) +-*/ array-name  

The first “array-name” is the name of the array to which the new array values should be assigned.
The equal sign (=) is required.
The “(num-expr)” portion of the syntax or array name indicates the value that should be applied (added, subtracted, multiplied or divided) to each element in the array.  The numeric expression must be placed within parentheses if it is not an array name.
The numeric operator, represented by “+-*/” in the above syntax statement, specifies the arithmetic function which is to occur.
The second “array-name” is the name of the array containing the elements to be manipulated.  This may be the same as the first array name.

The following example multiplies each salary in the SAL array by 1.064.  Each of the old values is replaced by the new value.  

09870 MAT SAL = (1.064)*SAL   

To print a listing of the new SAL values, you can use the immediate mode command PRINT MAT SAL after the line above has been executed.

UDim

Often when writing a program, you won’t know how many spaces will be in the array, or you will want to constantly add more lines to the array. For example, in the program above, if the user wanted to add several more employees every month as the company grew, you don’t have to constantly change the size of the array using the MAT statement. Instead, the UDim function can be used to retrieve the current size of the array, for example, to put it into a variable. As you add employee names to the array, it will serve to constantly adjust for those additions without you having to specify the size. The syntax for UDim is as follows: UDim(array-name)

Mid-Chapter Practice:

1. Use the name and salary chart above for the first five employees. 2. Modify the program to use a for/next loop to ask for new employees’ names and salaries and add them to the arrays, so that the program will list the names and salaries for all nine employees. Use the UDim function described above. 3. Then add lines to the program to calculate and list next year’s salaries for each employee.

George 35,200
Leah 68,300
Alana 43,650
Tim 29,870

The completed program should look something like this (only scroll ahead after you have tried and completed your program):

If you need help with the code itself, check your program against the one listed below:

Sorting arrays with MAT and the AIDX or DIDX functions

Business Rules!’s MAT statement allows you to sort and reorder the elements in an array.  Numeric arrays can be sorted from the lowest element to the highest element with the AIDX function, or they can be sorted from highest to lowest with the DIDX function (AIDX is ascending order; DIDX is descending order; IDX stands for index).  String elements can be sorted in alphabetical order with the AIDX function, or they can be sorted in reverse alphabetical order with the DIDX function.

When sorting array elements with AIDX or DIDX, it’s important to remember that Business Rules! does not change the order of elements in the original array.  Instead, a new array must be named to hold the subscripts of the original array in their sorted (indexed) order.

In the following example, the string array CUST$ holds the last names of five customers.  The numeric array ORDER holds the subscripts of the CUST$ values in their sorted order.  Printing out the actual names in alphabetical order requires the statements in lines 50-70.  The first time through the loop, this statement takes the first element in the ORDER array and makes it the subscript of the element in the CUST$ array to be printed.  The second time through the loop, it uses the second element in the ORDER array as the CUST$ subscript.  The process continues until all elements are printed. File:10.5.jpg

Quick Quiz 10.3

1. Which of the following statements are true about the keyword MAT?
a. It is a statement keyword.
b. It always refers to an array.
c. It allows resizing of arrays.
d. It can be used within the PRINT statement to print the contents of an array.
e. It can be used within the READ statement to assign values to array elements.
f. It provides easy ways to perform arithmetic with arrays.

2. Which of the following statements contains the proper syntax for assigning values to the TEMPLE$ array?
a. MAT READ TEMPLE$
b. MAT TEMPLE$(3)
c. READ MAT TEMPLE$
d. READ TEMPLE$

3. What do the AIDX and DIDX functions allow you to do?
a. Sort the elements of an array in either ascending or descending order.
b. Change the order of elements in an array so that they occur in either ascending or descending order.
c. Exchange the contents of one array for the contents of another.

Answers 10.3

10.4 Two-dimensional matrices

You learned in a previous lesson that you can print two or more arrays side-by-side to create a table of information.  Business Rules! also allows you to put all the same information into a single two-dimensional matrix which contains both rows and columns.  The following table for example, comes from the two-dimensional matrix POWERS:

1 1 1 1
2 4 8 16
3 9 27 81
4 16 64 256
5 25 125 625

As with one-dimensional matrices or arrays, the values in a two-dimensional matrix are referred to by their position within the matrix.  The difference is that each value in a two-dimensional matrix has both a row and a column position. 

In the above table, the value 2 is located in row 2, column 1.  In terms that Business Rules! understands, its position is POWERS(2,1).  Likewise, the value 81--which is located in row 3, column 4--is POWERS(3,4).

Rows are always specified before columns in Business Rules!.  Keeping this in mind, can you identify the value of POWERS(5,3)?

Two-dimensional matrices are often referred to by their size, or dimensions.  Our example above is a 5X4 array because it is five rows deep by four columns wide.  (Again, rows are always specified first; a 5X4 matrix is different from a 4X5 matrix.)

Two-dimensional matrices and the DIM statement

The DIM statement works in much the same way for a two-dimensional matrix as it does for a one-dimensional matrix (arrays).  The syntax is as follows:   line# DIM array-name (#rows,#columns)

The “#rows” portion of the syntax indicates the total number of horizontal rows in the table.  It must be an integer

The “#columns” portion of the syntax indicates the total number of vertical columns in the table.  It must be an integer.   The DIM statement for the POWERS matrix which contains five rows and four columns would be as follows:  

00040 DIM POWERS(5,4) 

Assigning values to two-dimensional matrix with FOR/NEXT

It can be a little trickier to assign values to a two-dimensional matrix array than it is to an array.  One way to handle it is with a nested FOR/NEXT loop.  The following example uses nested FOR/NEXT loops to assign values to the array POWERS.

Let’s follow the execution sequence of the above example from line 60, where Business Rules! enters the Row loop.  Row equals one at this point.  At the next line, Business Rules! enters the COL loop; COL also equals one at this point.  At line 80, Business Rules! is instructed to READ a data value into the (R,C) position of POWERS.  Since both ROW and COL equal one, the first data table value (1) is assigned to the first row, first column position in the array.

The second time through the COL loop, ROW still equals one but COL equals two. So the READ statement at line 80 assigns the next data item to POWERS(1,2), which is the first row, second column of the POWERS array.  Business Rules! continues to repeat the COL loop until all four columns in the first row are filled.  Since COL is then greater than four, Business Rules! has finished executing the COL loop.  It jumps to the first statement after the loop, where it is instructed to repeat the ROW loop.  ROW will now equal two while the entire COL loop process is again executed four times.  This sequence continues until ROW is greater than five.

Assigning values to matrices with READ MAT

A second, faster way to assign values to both one-dimensional and two-dimensional matrix is by using the MAT keyword in the READ statement.  This method allows you to produce the same results with one statement that it took five lines to do in our last example.

The syntax of a READ statement using MAT is:

line# READ MAT array-name 

When Business Rules! sees a READ MAT statement, it assigns DATA statement values to the matrix in row-by-row order according to the way the array was dimensioned.  The following set of program lines for example, performs the same operation that our nested FOR/NEXT loops did in the last section:

00040 DIM POWERS(5,4) 
00050 DATA 1,1E2,1E3,1E4,2,2E2,2E3,2E4,3,3E2,3E3,3E4,4,4E2,4E3,4E4,5,5E2,5E3,5E4 
00060 READ MAT POWERS 

Enter this into BR and print it to see how it appears on your screen. Is that what you expected?

Quick Quiz 10.4

1. Which of the following best describes a two-dimensional matrix?

   a. Two matrix arrays which are printed next to each other.
   b. A variable containing a list of values that can be referenced by their row and column positions.
   c. A list of values in a DATA statement.
  2. You wish to refer to the value in the seventh column of the fourth row in the matrix DCHART.  How can you refer to this position so that Business Rules! will understand?

   a. DCHART(7,4)
   b. DCHART(4,7)
   c. DHCART (4),(7)

3. What does the following statement do?

00090 DIM EIGER(5,8)

   a. Tells Business Rules! that the EIGER array will contain a maximum of 5 rows and 8 columns.
   b. Tells Business Rules! that EIGER will be an 8x5 array.
   c. Tells Business Rules! to assign a value of 40 to row 5, column 8 of the EIGER array.
   d. Tells Business Rules! to reserve space for 5 elements, each with a maximum length of 8 characters, in the EIGER array.

4. Name two ways to assign values to a two-dimensional matrix:
   _________________________   and   ________________________.

Answers 10.4

10.5 Performing arithmetic with two-dimensional matrixes

Imagine that you are a high school math instructor, and you have been working for months to interest three of your brightest -- but least interested -- students in the curriculum.  Finally you decide to hold a competition among them.  The student who averages the highest on the next four test scores will get a prize.  The catch however, is that the prize will only be awarded if all three students average at least 85% on each test.

The students’ test scores can be held in the 3x4 matrix SCORES:

Roger 89 87 94 93
Martha 85 90 92 94
Cheryl 81 83 96 100

 

In order to find out first whether or not the prize will be given, and second who it will be given to, you need to find the averages of both the column and the row figures in the array.  To try this process out for yourself as you are learning about it in the next few sections, you are going to write a program entitled MOTIVAT.

Arithmetic with matrix columns

Your first objective is to find the average for each test, or column.  To compute the column averages of the SCORES matrix, you will need to dimension a new array variable, SUMCOL.  This array will hold four values: the sums of columns one, two, three and four of the SCORES array.  Go ahead and dimension this array variable now. This is the SCORES array.   Next, type the following lines into the MOTIVAT program:  

00140  FOR C=1 TO 4 
00150  FOR R=1 TO 3 
00160  LET SUMCOL(C)=SUMCOL(C)+SCORES(R,C) 
00170  NEXT R 
00180  PRINT SUMCOL(C)/3 
00190  NEXT C 

The above code uses nested FOR/NEXT loops to determine the average score for each test.  After Business Rules! has entered both the C and the R loops, the LET statement at line 160 tells it to add the current values of SUMCOL(C) to the value in row R column C of the SCORES array.

Because this is the first time through the C loop, SUMCOL(C) equals SUMCOL(1)  and, because SUMCOL(1) has not yet been assigned a value, its value is currently 0.  Since this is also the first time through the R loop, SCORES(R,C) is equal to the value of SCORES(1,1), which is 89.  Thus the value 0+89 (89) is assigned to the SUMCOL(1) variable.

C remains the same, but R changes to 2 the second time Business Rules! executes the R loop.  This causes the value of SUMCOL(1) to be added to the value at row 2, column 1 of the SCORES array.  Thus SUMCOL(1) is now assigned the value 89+85 (174).

When Business Rules! completes the R loop for the third time, it assigns 174+81 (255) to SUMCOL(1).  It then goes on to execute the PRINT statement at line 180. The printed value is the average of the three test scores for test 1.

Business Rules! then returns to line 140 and executes the C loop three additional times to determine the average scores for tests 2, 3, and 4.

Arithmetic with matrix rows

Since the overall average on each test was at least 85%, your next objective is to figure out who won the contest.  To do this, you need to average the row totals in the SCORES array. The row arithmetic for this example is similar to the column arithmetic you used in the last section. Before you read ahead, try to write the next lines of the program yourself.

The biggest difference is that the variables in the inner and outer loops are switched.  Also, you will need to dimension another array variable, SUMROW, to the following lines to the MOTOIVAT program:

00200  FOR R=1 TO 3 
00210  FOR C=1 TO 4 
00220  LET SUMROW(R)=SUMROW(R)+SCORES(R,C) 
00230  NEXT C 
00240  PRINT SUMROW(R)/4 
00250  NEXT R 

Quick Quiz 10.5

1. Who was the winner of the math contest?
   a. Roger
   b. Martha
   c. Cheryl

Answer:
1-Run the program to see!

10.6 More about DIM

Business Rules! is flexible in that it does not require you to use DIM statements for (one-dimensional) arrays holding 10 values or less.  You can check this out for yourself by typing the following lines, which assign four values to the TOWEL$ array, into Business Rules!.  Business Rules! will RUN the program without sending an error. (If you wish to print the array, use PRINT MAT TOWEL as a command after you run the program.)

00010 LET MAT$(1)=”Canon” 
00020 LET MAT$(2)=”Martex” 
00030 LET MAT$(3)=”Perry Ellis” 
00040 LET MAT$(4)=”JC Penney” 
00050 READ MAT TOWEL$ 

When you assign values to an array without first dimensioning it, Business Rules! automatically gives it a size of 10.  In the above example, the TOWEL$ array has room for 10 values--even though only 4 are assigned.

The ability to assign values to arrays without dimensioning them is a very useful timesaver, but this is poor programming style. Failure to dimension arrays in long and complex programs can make those programs difficult to understand, edit, and debug.

Quick Quiz 10.6

1. What is the maximum number of values that can be assigned to a two-dimensional array before Business Rules! requires you to dimension the array variable?
a. 4
b. 10 rows, 10 columns (100 values)
c. 10

2. True or False: When you assign 6 values to an array without dimensioning it, Business Rules! automatically dimensions the array to six values for you.

Answers 10.6

Chapter Exercise

Write a program to assign the 9x9 multiplication table to a two-dimensional matrix and print it out as follows:

x 1 2 3 4 5 6 7 8 9
1 1 2 3 4 5 6 7 8 9
2 4 6 8 10 12 14 16 18
3 3 6 9 12 15 18 21 24 27
4 4 8 12 16 20 24 28 32 36
5 5 10 15 20 25 30 35 40 45
6 6 12 18 24 30 36 42 48 54
7 7 14 21 28 35 42 49 56 63
8 8 16 24 32 40 48 56 64 72
9 9 18 27 36 45 54 63 72 81

Solution

NEXT:Full Screen Processing, Using Print Fields and Input Fields

Back to Index