31
Array - adding to array at run time Please see speaker notes for additional information!

Array - adding to array at run time Please see speaker notes for additional information!

Embed Size (px)

Citation preview

Page 1: Array - adding to array at run time Please see speaker notes for additional information!

Array - adding to array at run time

Please see speaker notes for additional information!

Page 2: Array - adding to array at run time Please see speaker notes for additional information!

0

0

0

0

0

0

driveIn

1

2

3

4

5

6

Array to add donations by drive

driveDonation(1)

driveDonation(2)

driveDonation(3)

driveDonation(4)

driveDonation(5)

driveDonation(6)

The driveIn which will be either keyed in by the user or a field on the records in the file will be used to determine which driveDonation to add to. In other words, driveIn will be the subscript/index/pointer that tells me which element of the array to add to. If the driveIn is 1, then I will add to driveDonation(1), if the driveIn is 2, then I want to add to Drive Donation(2). However, I am defeating the value of the array if I hard code a number as the subscript/index/pointer, so instead I will use driveIn as the subscript/index/pointer.

The following pseudocode illustrates:

add amtDonation to driveDonation(driveIn)

This will take the amount of the contribution coming in from the user or the record on the file and add it to driveDonation element that driveIn points to.

Page 3: Array - adding to array at run time Please see speaker notes for additional information!

0

0

0

0

0

0

Array to add donations by drive

driveDonation(1)

driveDonation(2)

driveDonation(3)

driveDonation(4)

driveDonation(5)

driveDonation(6)

User Input:

driveIn = 2

amtDonation = 100

add amtDonation to driveDonation(driveIn)

0

100

0

0

0

0

Array to add donations by drive

driveDonation(1)

driveDonation(2)

driveDonation(3)

driveDonation(4)

driveDonation(5)

driveDonation(6)

Page 4: Array - adding to array at run time Please see speaker notes for additional information!

0

100

0

0

0

0

Array to add donations by drive

driveDonation(1)

driveDonation(2)

driveDonation(3)

driveDonation(4)

driveDonation(5)

driveDonation(6)

User Input:

driveIn = 4

amtDonation = 350

add amtDonation to driveDonation(driveIn)

0

100

0

350

0

0

Array to add donations by drive

driveDonation(1)

driveDonation(2)

driveDonation(3)

driveDonation(4)

driveDonation(5)

driveDonation(6)

Page 5: Array - adding to array at run time Please see speaker notes for additional information!

0

100

0

350

0

0

Array to add donations by drive

driveDonation(1)

driveDonation(2)

driveDonation(3)

driveDonation(4)

driveDonation(5)

driveDonation(6)

User Input:

driveIn = 1

amtDonation = 50

add amtDonation to driveDonation(driveIn)

50

100

0

350

0

0

Array to add donations by drive

driveDonation(1)

driveDonation(2)

driveDonation(3)

driveDonation(4)

driveDonation(5)

driveDonation(6)

Page 6: Array - adding to array at run time Please see speaker notes for additional information!

50

100

0

350

0

0

Array to add donations by drive

driveDonation(1)

driveDonation(2)

driveDonation(3)

driveDonation(4)

driveDonation(5)

driveDonation(6)

User Input:

driveIn = 2

amtDonation = 500

add amtDonation to driveDonation(driveIn)

50

600

0

350

0

0

Array to add donations by drive

driveDonation(1)

driveDonation(2)

driveDonation(3)

driveDonation(4)

driveDonation(5)

driveDonation(6)

Page 7: Array - adding to array at run time Please see speaker notes for additional information!

50

600

0

350

0

0

Array to add donations by drive

driveDonation(1)

driveDonation(2)

driveDonation(3)

driveDonation(4)

driveDonation(5)

driveDonation(6)

User Input:

driveIn =6

amtDonation = 250

add amtDonation to driveDonation(driveIn)

50

600

0

350

0

250

Array to add donations by drive

driveDonation(1)

driveDonation(2)

driveDonation(3)

driveDonation(4)

driveDonation(5)

driveDonation(6)

Page 8: Array - adding to array at run time Please see speaker notes for additional information!

50

600

0

350

0

250

Array to add donations by drive

driveDonation(1)

driveDonation(2)

driveDonation(3)

driveDonation(4)

driveDonation(5)

driveDonation(6)

User Input:

driveIn =2

amtDonation = 1000

add amtDonation to driveDonation(driveIn)

50

1600

0

350

0

250

Array to add donations by drive

driveDonation(1)

driveDonation(2)

driveDonation(3)

driveDonation(4)

driveDonation(5)

driveDonation(6)

Page 9: Array - adding to array at run time Please see speaker notes for additional information!

50

1600

0

350

0

250

Array to add donations by drive

driveDonation(1)

driveDonation(2)

driveDonation(3)

driveDonation(4)

driveDonation(5)

driveDonation(6)

User Input:

driveIn =5

amtDonation = 1000

add amtDonation to driveDonation(driveIn)

50

1600

0

350

1000

250

Array to add donations by drive

driveDonation(1)

driveDonation(2)

driveDonation(3)

driveDonation(4)

driveDonation(5)

driveDonation(6)

Page 10: Array - adding to array at run time Please see speaker notes for additional information!

50

1600

0

350

1000

250

Array to add donations by drive

driveDonation(1)

driveDonation(2)

driveDonation(3)

driveDonation(4)

driveDonation(5)

driveDonation(6)

User Input:

driveIn =2

amtDonation = 2000

add amtDonation to driveDonation(driveIn)

50

3600

0

350

1000

250

Array to add donations by drive

driveDonation(1)

driveDonation(2)

driveDonation(3)

driveDonation(4)

driveDonation(5)

driveDonation(6)

Page 11: Array - adding to array at run time Please see speaker notes for additional information!

50

3600

0

350

1000

250

Array to add donations by drive

driveDonation(1)

driveDonation(2)

driveDonation(3)

driveDonation(4)

driveDonation(5)

driveDonation(6)

If at the end of processing I want to print out totals, I can easily take them from the array and display or print them.

Total for drive 1 is 50Total for drive 2 is 3600Total for drive 3 is 0Total for drive 4 is 350Total for drive 5 is 1000Total for drive 6 is 250

Page 12: Array - adding to array at run time Please see speaker notes for additional information!

Mainline

Housekeeping

Wrapup

Process

END

Housekeeping

Set up variables

Setup/definearray - set elements to 0

EndHousekeeping

Process

Read record or take in user input

Data to process

LoopY

N

End Process

Continue on next slide...

Page 13: Array - adding to array at run time Please see speaker notes for additional information!

Loop

Add input data to array using dept input as subscript/index/pointer

Display or printdata

Read record or take in user input

End Loop

Wrapup

Set up output using totals from array

Display or printtotals

End Wrapup

Page 14: Array - adding to array at run time Please see speaker notes for additional information!

Note that two contributions have been made to drive 3 and it is now $600.

First contribution to drive 3.

Page 15: Array - adding to array at run time Please see speaker notes for additional information!

Another contribution has been made to drive 2 and it is now up to $1,500.

Another contribution has been made to drive 2 and it is now up to $1,750

Page 16: Array - adding to array at run time Please see speaker notes for additional information!

Another contribution was made to drive 3 which brought it up to $700

Additional donations were made and finally the user entered N. The totals were then printed.

Page 17: Array - adding to array at run time Please see speaker notes for additional information!

IDENTIFICATION DIVISION. PROGRAM-ID. ACCARRAY. DATA DIVISION. WORKING-STORAGE SECTION. 01 USER-INPUT. 05 USER-ID PIC 99999. 05 USER-NAME PIC X(20). 05 USER-DRIVE PIC 9. 05 USER-DONATION PIC 9(5)V99. 05 USER-RESPONSE PIC X VALUE " ". 01 DATA-FOR-SCREEN. 05 USER-DONATION-SCR PIC $$$,$$$.99. 05 ARRAY-DATA-SCR PIC $$$$,$$$.99. *NOTE: MY COMPANY CURRENTLY HAS 6 DRIVES THAT PEOPLE CAN *CONTRIBUTE TO. THE DRIVES ARE NUMBERED 1, 2, 3, 4, 5, 6. 01 DONATION-ARRAY. 05 DRIVE-DONATION PIC 9(6)V99 OCCURS 6 TIMES. PROCEDURE DIVISION. PERFORM A-100-HOUSEKEEPING. PERFORM B-100-PROCESS. PERFORM C-100-TERMINATE. STOP RUN. A-100-HOUSEKEEPING. DISPLAY " " WITH BLANK SCREEN. DISPLAY " ". MOVE 0 TO DRIVE-DONATION(1). MOVE 0 TO DRIVE-DONATION(2). MOVE 0 TO DRIVE-DONATION(3). MOVE 0 TO DRIVE-DONATION(4). MOVE 0 TO DRIVE-DONATION(5). MOVE 0 TO DRIVE-DONATION(6).

Occurs 6 times says to set up 6 elements and to call each element by the name DRIVE-DONATION.

Here I am making sure that each element in the array starts out at 0. There are easier ways of doing this, but this makes the point better than most.

Page 18: Array - adding to array at run time Please see speaker notes for additional information!

B-100-PROCESS. DISPLAY "ENTER USER ID# ". ACCEPT USER-ID. DISPLAY "ENTER USER NAME ". ACCEPT USER-NAME. DISPLAY "ENTER DRIVE DONATION IS MADE TO 1,2,3,4,5 OR 6". ACCEPT USER-DRIVE. DISPLAY "ENTER AMOUNT OF DONATION - MAXIMUM IS 99999.99". ACCEPT USER-DONATION. PERFORM B-200-LOOP UNTIL USER-RESPONSE = "N". B-200-LOOP. ADD USER-DONATION TO DRIVE-DONATION(USER-DRIVE). MOVE USER-DONATION TO USER-DONATION-SCR DISPLAY "YOUR CONTRIBUTION HAS INCREASED " USER-DRIVE " BY " USER-DONATION-SCR. MOVE DRIVE-DONATION(USER-DRIVE) TO ARRAY-DATA-SCR. DISPLAY "THE CURRENT TOTAL FOR " USER-DRIVE " IS " ARRAY-DATA-SCR. DISPLAY "ENTER USER ID# ". ACCEPT USER-ID. DISPLAY "ENTER USER NAME ". ACCEPT USER-NAME. DISPLAY "ENTER DRIVE DONATION IS MADE TO 1,2,3,4,5 OR 6". ACCEPT USER-DRIVE. DISPLAY "ENTER AMOUNT OF DONATION - MAXIMUM IS 99999.99". ACCEPT USER-DONATION. DISPLAY "DO YOU WANT TO CONTINUE AND PROCESS THIS DATA?". DISPLAY "ENTER Y IF YOU WANT TO CONTINUE, ENTER N TO STOP". ACCEPT USER-RESPONSE.

Here I take in the initializing data and give the command to repeat the loop until the user signals they are done by responding with a N.

Here I am using USER-DRIVE as the subscript to add to the element in the array called DRIVE-DONATION.

Here I am moving the element in DRIVE-DONATION that USER-DRIVE is pointing to.

Page 19: Array - adding to array at run time Please see speaker notes for additional information!

C-100-TERMINATE. MOVE DRIVE-DONATION(1) TO ARRAY-DATA-SCR. DISPLAY "TOTAL FOR DRIVE 1 IS " ARRAY-DATA-SCR. MOVE DRIVE-DONATION(2) TO ARRAY-DATA-SCR. DISPLAY "TOTAL FOR DRIVE 2 IS " ARRAY-DATA-SCR. MOVE DRIVE-DONATION(3) TO ARRAY-DATA-SCR. DISPLAY "TOTAL FOR DRIVE 3 IS " ARRAY-DATA-SCR. MOVE DRIVE-DONATION(4) TO ARRAY-DATA-SCR. DISPLAY "TOTAL FOR DRIVE 4 IS " ARRAY-DATA-SCR. MOVE DRIVE-DONATION(5) TO ARRAY-DATA-SCR. DISPLAY "TOTAL FOR DRIVE 5 IS " ARRAY-DATA-SCR. MOVE DRIVE-DONATION(6) TO ARRAY-DATA-SCR. DISPLAY "TOTAL FOR DRIVE 6 IS " ARRAY-DATA-SCR.

In this code, I move the element from the array one at a time to another area in memory which is edited. I then display the edited version.

Note that I am using the actual numbers here as I did in the housekeeping when I initialized at 0.

Page 20: Array - adding to array at run time Please see speaker notes for additional information!

IDENTIFICATION DIVISION. PROGRAM-ID. ACCARRAY. DATA DIVISION. WORKING-STORAGE SECTION. 01 USER-INPUT. 05 USER-ID PIC 99999. 05 USER-NAME PIC X(20). 05 USER-DRIVE PIC 9. 05 USER-DONATION PIC 9(5)V99. 05 USER-RESPONSE PIC X VALUE " ". 01 DATA-FOR-SCREEN. 05 USER-DONATION-SCR PIC $$$,$$$.99. 05 ARRAY-DATA-SCR PIC $$$$,$$$.99. 01 PROCESSING-SUBSCRIPT. 05 SUBZ PIC 9 VALUE 0. *NOTE: MY COMPANY CURRENTLY HAS 6 DRIVES THAT PEOPLE CAN *CONTRIBUTE TO. THE DRIVES ARE NUMBERED 1, 2, 3, 4, 5, 6. 01 DONATION-ARRAY. 05 DRIVE-DONATION PIC 9(6)V99 OCCURS 6 TIMES. PROCEDURE DIVISION. PERFORM A-100-HOUSEKEEPING. PERFORM B-100-PROCESS. PERFORM C-100-TERMINATE. STOP RUN. A-100-HOUSEKEEPING. DISPLAY " " WITH BLANK SCREEN. DISPLAY " ". MOVE 1 TO SUBZ. PERFORM UNTIL SUBZ > 6 MOVE 0 TO DRIVE-DONATION(SUBZ) ADD 1 TO SUBZ END-PERFORM.

Here I am defining a memory variable called SUBZ that I will use as my subscript/index/pointer in parts of this program.

Here I initialize SUBZ at 1. I then go through a loop until SUBZ is greater than 6. Inside the loop I move 0 to the DRIVE-DONATION element that SUBZ points to and then I add 1 to SUBZ so that the next time I do the move it will move to the next element. When SUBZ is greater than 6 it has initialized all of the elements so I end the loop.

Page 21: Array - adding to array at run time Please see speaker notes for additional information!

B-100-PROCESS. DISPLAY "ENTER USER ID# ". ACCEPT USER-ID. DISPLAY "ENTER USER NAME ". ACCEPT USER-NAME. DISPLAY "ENTER DRIVE DONATION IS MADE TO 1,2,3,4,5 OR 6". ACCEPT USER-DRIVE. DISPLAY "ENTER AMOUNT OF DONATION - MAXIMUM IS 99999.99". ACCEPT USER-DONATION. PERFORM B-200-LOOP UNTIL USER-RESPONSE = "N". B-200-LOOP. ADD USER-DONATION TO DRIVE-DONATION(USER-DRIVE). MOVE USER-DONATION TO USER-DONATION-SCR DISPLAY "YOUR CONTRIBUTION HAS INCREASED " USER-DRIVE " BY " USER-DONATION-SCR. MOVE DRIVE-DONATION(USER-DRIVE) TO ARRAY-DATA-SCR. DISPLAY "THE CURRENT TOTAL FOR " USER-DRIVE " IS " ARRAY-DATA-SCR. DISPLAY "ENTER USER ID# ". ACCEPT USER-ID. DISPLAY "ENTER USER NAME ". ACCEPT USER-NAME. DISPLAY "ENTER DRIVE DONATION IS MADE TO 1,2,3,4,5 OR 6". ACCEPT USER-DRIVE. DISPLAY "ENTER AMOUNT OF DONATION - MAXIMUM IS 99999.99". ACCEPT USER-DONATION. DISPLAY "DO YOU WANT TO CONTINUE AND PROCESS THIS DATA?". DISPLAY "ENTER Y IF YOU WANT TO CONTINUE, ENTER N TO STOP". ACCEPT USER-RESPONSE.

Page 22: Array - adding to array at run time Please see speaker notes for additional information!

C-100-TERMINATE. MOVE 1 TO SUBZ. PERFORM UNTIL SUBZ > 6 MOVE DRIVE-DONATION(SUBZ) TO ARRAY-DATA-SCR DISPLAY "TOTAL FOR DRIVE " SUBZ " IS " ARRAY-DATA-SCR ADD 1 TO SUBZ END-PERFORM.

I initialize SUBS to 1.

Then I perform or execute the loop until SUBZ > 6. Note that inside the loop I increment SUBZ so eventually it will be > 6.

There are three important things to do when using a loop that is controlled by a counter. In this case the counter is the subscript/index/pointer. You should initialize the subscript outside the loop. You should test the subscript to see when it has reached its limit. You should increment the subscript inside the loop.

Inside the loop, I move the DRIVE-DONATION element that the SUBZ is pointing to to a memory variable and then I display the memory variable. Note that I also display the value of SUBZ so I know which element I am viewing.

Page 23: Array - adding to array at run time Please see speaker notes for additional information!

I entered the data and clicked Process Donation. The data is being added to the array using the drive which is 5 to determine which element.

Page 24: Array - adding to array at run time Please see speaker notes for additional information!
Page 25: Array - adding to array at run time Please see speaker notes for additional information!
Page 26: Array - adding to array at run time Please see speaker notes for additional information!

This shows the total data for the data entry shown on the last few slides.

Page 27: Array - adding to array at run time Please see speaker notes for additional information!
Page 28: Array - adding to array at run time Please see speaker notes for additional information!
Page 29: Array - adding to array at run time Please see speaker notes for additional information!

Option ExplicitDim DonationTotals(6)Private Sub cmdClear_Click() txtUserId = "" txtUserName = "" txtDrive = "" txtAmtDonation = ""End Sub

Private Sub cmdExit_Click() EndEnd Sub

Private Sub cmdProcDonation_Click() DonationTotals(txtDrive) = DonationTotals(txtDrive) + txtAmtDonationEnd Sub

Private Sub cmdShowTotals_Click()Dim msg As StringDim ct As IntegerFor ct = 0 To 5 msg = "The total for " & Str(ct + 1) & " is " & Str(DonationTotals(ct + 1)) lblTotal(ct) = msgNextEnd SubPrivate Sub Form_Load() Dim ct As Integer For ct = 1 To 6 DonationTotals(ct) = 0 NextEnd Sub

This established by array - it actually can be used with indexes 0 - 6 but I am going to use 1 - 6.

This code uses the txtDrive that the user entered as a index for the array DonationTotals. Note that I add the txtAmtDonation that the user entered to the DonationTotals that txtDrive is pointing to and then store the result back in DonationTotals.

Here I am using a FOR loop to loop through the DonationTotals with ct starting at 1 and ending at 6. I am initializing each of the elements in the table at 0..

See next slide.

Page 30: Array - adding to array at run time Please see speaker notes for additional information!

Private Sub cmdShowTotals_Click()Dim msg As StringDim ct As IntegerFor ct = 0 To 5 msg = "The total for " & Str(ct + 1) & " is " & Str(DonationTotals(ct + 1)) lblTotal(ct) = msgNextEnd Sub

The labels to hold the totals are set up in an array when I created the form. The first one is lblTotal(0) and the second is lblTotal(1) etc.

Page 31: Array - adding to array at run time Please see speaker notes for additional information!

Private Sub cmdShowTotals_Click()Dim msg As StringDim ct As IntegerFor ct = 0 To 5 msg = "The total for " & Str(ct + 1) & " is " & Str(DonationTotals(ct + 1)) lblTotal(ct) = msgNextEnd Sub

First I define msg as String and ct as Integer for local use within this subroutine.

Then I have a loop to print out the totals which will use the FOR and will run from 0 to 5 since the labels on the form are lblTotal(0) through lblTotal(5).

When I go into the loop I set up the message I want to display as made up of a literal concatenated with the string version of ct + 1 which will give me the drive no concatenated with another literal concatenated with the string version of the element in the table that ct+1 is pointing to. Remember the numbers in the array run from 1 to 6 and on the labels on the form they run from 0 to 5. This is why I am using ct+1 to reference the array in memory.

Next I put the msg in lblTotal(ct) where ct will move from 0 to 5 taking what is in the array in 1 through 6. First goes to zeroth, second goes to first, third goes to second etc.