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

Slides:



Advertisements
Similar presentations
Check Digit - Mod 11 Please use speaker notes for additional information!
Advertisements

ABNIAC The following slide presentation is to acquaint the student with ABNIAC. The version used for presentation is the Java version, which can be found.
Programming Logic and Design, Third Edition Comprehensive
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Screen Section Please use speaker notes for additional information!
To type the VB code behind the command button (named cmdPush), Double-Click on the Push Me (caption) command button As a result the Visual Basic Code Window.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Creating Embedded Formative Assessment Dr. Steve Broskoske Misericordia University EDU 533 Computer-based Education.
12-1 Structured COBOL Programming Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout John.
MySQL + PHP.  Introduction Before you actually start building your database scripts, you must have a database to place information into and read it from.
InvEasy (Project1) Please use speaker notes for additional information!
Modifications to program Addda.cbl Please use speaker notes for additional information!
Break Processing Please use speaker notes for additional information!
Iteration. Adding CDs to Vic Stack In many of the programs you write, you would like to have a CD on the stack before the program runs. To do this, you.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Programming Examples to Accompany Structure Topic Please use speaker notes for additional information!
Totals on the Screen Please use speaker notes for additional information!
For Loops (ProjFor1, ProjFor2, ProjFor3, ProjFor4, textbox, textbox1) Please use speaker notes for additional information!
Logic Structure - focus on looping Please use speaker notes for additional information!
VB Games: Preparing for Memory Brainstorm controls & events Parallel structures (again), Visibility, LoadPicture, User-defined procedures, Do While/Loop,busy.
Working with the VB IDE. Running a Program u Clicking the”start” tool begins the program u The “break” tool pauses a program in mid-execution u The “end”
1 Week 6 The Repetition Structure. 2 The Repetition Structure (Looping) Lesson A Objectives After completing this lesson, you will be able to:  Code.
Tutorial 6 The Repetition Structure
30/10/ Iteration Loops Do While (condition is true) … Loop.
Random Files Please see speaker notes for additional information!
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
9-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)
Explanation of SAMPLEIF (if88in1.cbl or if88in1.html) Please use speaker notes for additional information!
HELLO WORLD program in COBOL - CIS12 Please use speaker notes for additional information!
22/11/ Selection If selection construct.
When I want to work with SQL, I start off as if I am doing a regular query.
COBOL Screens Please use speaker notes for additional information!
Two Forms Please use speaker notes for additional information!
1 Chapter 9. To familiarize you with  Simple PERFORM  How PERFORM statements are used for iteration  Options available with PERFORM 2.
Select (drop-down list) Inputs. Insert/Form/List Menu.
Lab 6 (2) Arrays ► Lab 5 (1) Exercise Review ► Array Concept ► Why Arrays? ► Array Declaration ► An Example of Array ► Exercise.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Visual Basic I/O Programs (ProjRead1, ProjRead2, ProjWrite1, ProjPay) Please use speaker notes for additional information!
Maximum Profit Please use speaker notes for additional information!
Lesson 1. Security At the menu bar at the top you will see the word Tools. Click your mouse on Tools scroll down to Macro. Move the Mouse over and down.
31/01/ Selection If selection construct.
Controlling Program Flow with Looping Structures
Analysis of SAMPLE1.CBL Please check speaker notes for additional information!
Using a Database Access97 Please use speaker notes for additional information!
Random update Please use speaker notes for additional information!
Sequential Processing to Update a File Please use speaker notes for additional information!
General Introduction Algorithms. Let’s write a program  A program is a collection of statements written in a language the computer understands.  A computer.
Subroutines (PrArith, Math,projCP1, PrAdrProc, PrAdrProcFunc) Please use speaker notes for additional information!
Multiple forms - SDI & MDI Please use speaker notes for additional information!
26/06/ Iteration Loops For … To … Next. 226/06/2016 Learning Objectives Define a program loop. State when a loop will end. State when the For.
This is a while loop. The code is done while the condition is true. The code that is done is enclosed in { }. Note that this program has three parts: Housekeeping.
Visual Basic - Break Processing
Processing multiple files
Please use speaker notes for additional information!
Access Database for CIS17
Department Array in Visual Basic
Use proper case (ie Caps for the beginnings of words)
Please use speaker notes for additional information!
Chapter (3) - Looping Questions.
Iteration: Beyond the Basic PERFORM
Introduction to TouchDevelop
Access Database for CIT12
3.1 Iteration Loops For … To … Next 18/01/2019.
Searching an Array or Table
Please use speaker notes for additional information!
Using screens and adding two numbers - addda.cbl
Wrapup which is the document write that says the end.
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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.

IDENTIFICATION DIVISION. PROGRAM-ID. ACCARRAY. DATA DIVISION. WORKING-STORAGE SECTION. 01 USER-INPUT. 05 USER-ID PIC USER-NAME PIC X(20). 05 USER-DRIVE PIC USER-DONATION PIC 9(5)V USER-RESPONSE PIC X VALUE " ". 01 DATA-FOR-SCREEN. 05 USER-DONATION-SCR PIC $$$,$$$ 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, 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.

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

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.

IDENTIFICATION DIVISION. PROGRAM-ID. ACCARRAY. DATA DIVISION. WORKING-STORAGE SECTION. 01 USER-INPUT. 05 USER-ID PIC USER-NAME PIC X(20). 05 USER-DRIVE PIC USER-DONATION PIC 9(5)V USER-RESPONSE PIC X VALUE " ". 01 DATA-FOR-SCREEN. 05 USER-DONATION-SCR PIC $$$,$$$ ARRAY-DATA-SCR PIC $$$$,$$$ 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, 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.

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

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.

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.

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

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 but I am going to use 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.

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.

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.