Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Array - adding to array at run time Please see speaker notes for additional information!"— Presentation transcript:

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

2 000000000000 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.

3 000000000000 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 Array to add donations by drive driveDonation(1) driveDonation(2) driveDonation(3) driveDonation(4) driveDonation(5) driveDonation(6)

4 0 100 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 Array to add donations by drive driveDonation(1) driveDonation(2) driveDonation(3) driveDonation(4) driveDonation(5) driveDonation(6)

5 0 100 0 350 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 Array to add donations by drive driveDonation(1) driveDonation(2) driveDonation(3) driveDonation(4) driveDonation(5) driveDonation(6)

6 50 100 0 350 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 Array to add donations by drive driveDonation(1) driveDonation(2) driveDonation(3) driveDonation(4) driveDonation(5) driveDonation(6)

7 50 600 0 350 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)

8 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)

9 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)

10 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)

11 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 50 Total for drive 2 is 3600 Total for drive 3 is 0 Total for drive 4 is 350 Total for drive 5 is 1000 Total for drive 6 is 250

12 Mainline House keeping Wrapup Process END Housekeeping Set up variables Setup/define array - set elements to 0 End Housekeeping Process Read record or take in user input Data to process Loop Y N End Process Continue on next slide...

13 Loop Add input data to array using dept input as subscript/index/pointer Display or print data Read record or take in user input End Loop Wrapup Set up output using totals from array Display or print totals End Wrapup

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

15 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

16 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.

17 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.

18 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.

19 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.

20 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.

21 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.

22 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.

23 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.

24

25

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

27

28

29 Option Explicit Dim DonationTotals(6) Private Sub cmdClear_Click() txtUserId = "" txtUserName = "" txtDrive = "" txtAmtDonation = "" End Sub Private Sub cmdExit_Click() End End Sub Private Sub cmdProcDonation_Click() DonationTotals(txtDrive) = DonationTotals(txtDrive) + txtAmtDonation End Sub Private Sub cmdShowTotals_Click() Dim msg As String Dim ct As Integer For ct = 0 To 5 msg = "The total for " & Str(ct + 1) & " is " & Str(DonationTotals(ct + 1)) lblTotal(ct) = msg Next End Sub Private Sub Form_Load() Dim ct As Integer For ct = 1 To 6 DonationTotals(ct) = 0 Next End 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.

30 Private Sub cmdShowTotals_Click() Dim msg As String Dim ct As Integer For ct = 0 To 5 msg = "The total for " & Str(ct + 1) & " is " & Str(DonationTotals(ct + 1)) lblTotal(ct) = msg Next End 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.

31 Private Sub cmdShowTotals_Click() Dim msg As String Dim ct As Integer For ct = 0 To 5 msg = "The total for " & Str(ct + 1) & " is " & Str(DonationTotals(ct + 1)) lblTotal(ct) = msg Next End 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.


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

Similar presentations


Ads by Google