Arrays Chapter 5 1 A Guide to Working with Visual Logic Arrays An Array is a list or collection of similar or related items Examples: 1. Grocery List: Milk, Eggs, Bread, ….., Ice-Cream 2. Friends (on FB): John C, Jose F, Matt D, Dan I, Katie O, Sophana T, Allysa R, ……, Namita S 3. CS_MidtermScores: 95, 100, 88, 76, 0, … 103 4. Prices of Items (in your Shopping Cart): 10.95, 5.95, 1,99, 3.49, 1.99, 15.75, 1.99, 69.99, 3.49 2 A Guide to Working with Visual Logic Problems with Arrays Lists: 1. Grocery List: Milk, Eggs, Bread, ….., Ice-Cream 2. Friends (on FB): Peter K, Joe A, Matt D, Katie O, Sophana T, Kim B, ……, Namita S Problems: Grocery List: No List - You can put items in your cart as you remember them List - You can have a list and check off an item as you put it in your cart Friends List: Add a friend not already on your list Delete a friend 3 A Guide to Working with Visual Logic Problems with Numeric Arrays 4 Lists: 1. CS_MidtermScores : 95, 100, 88, 76, 0, … 103 2. Prices of Items (in your Shopping Cart): 10.95, 5.95, 1,99, 3.49, 1.99, 15.75, 1.99, 69.99, 3.49 Problems: Scores: Find: Highest Score, Average, Lowest Score, Median, Scale All Scores Up by 10 points (you wish J) Prices: Total Bill, Find the most Expensive Item, Find the price that appears the most on the List (3 items cost 1.99) A Guide to Working with Visual Logic Problem: Average and Reverse of 5 Numbers Input Begin Input: First Input: Second Begin Input: Third Sum = 0 Input: Fourth myCount 1 to 5 Input: Fifth Input: Number Sum = Sum + Number Average = Sum / 5 Output: "Average is " & Average End Output: Fifth Output: Fourth Output: Third Output: Second Output: First 5 A Guide to Working with Visual Logic End Problems: Input is a list of numbers Examples Seen before: SUM Problem: Find the sum/average of numbers (e.g. Input- 3, 7, 8, 4 Output- Sum: 22, Average: 5.5) LARGEST Problem : Find the largest number (e.g. Input- 3, 7, 8, 4 Output- Largest: 8) Do we have to keep/remember the entire list of the input numbers while we solve these problems? Yes/No What do we need to keep/remember at any point in the solution? SUM: SumSoFar, NextNumber LARGEST: LargestSoFar, NextNumber 6 A Guide to Working with Visual Logic More Problems: Input is a list of numbers REVERSE Problem: Displaying input numbers in reverse order (e.g. Input- 3, 7, 8, 4, 1 Output- 1, 4, 8, 7, 3) INCREASING ORDER/SORTING Problem: Displaying input numbers in increasing order (e.g. Input- 3, 7, 8, 4,1 Output- 1,3, 4, 7, 8) MEDIAN (“middle” value) Problem: Finding the “middle” number (e.g. Input- 3, 7, 8, 4,1 Output- 4) Do we have to remember the entire list of the input numbers while we solve these problems? Yes/No 7 A Guide to Working with Visual Logic Problem: Reverse of 100 Numbers Input How many Input Variables would we need? Can we use a Loop? No? Why Not? 8 A Guide to Working with Visual Logic Reversing 5 Input Numbers Why? Because variable names include an integer index/position which can be changed through a LCV (loop control variable) 9 A Guide to Working with Visual Logic This works!! Make Array in Visual Logic Scores 0 1 2 3 4 5 6 7 10 A Guide to Working with Visual Logic 8 Make Array in Visual Logic Make Array (MyArray, 8) 0 1. What is the NAME of the Array? __________ 1 2. What is the SIZE of the array? _____ (This is the number of memory locations created.) 2 3. What are the names of the 9 variables for the 3 memory locations? 4. What is the lowest index (position/subscript) 4 5 number? 5. What is the upper bound/ highest index 6 (position/subscript) number? 7 6. Do we have to use all the 8 variables/memory locations in the array? 11 A Guide to Working with Visual Logic MyArray What does this do? Scores 0 1 2 3 4 5 12 A Guide to Working with Visual Logic 0 0 75 86 67 0 6 0 7 70 8 0 What does this do? Populating Arrays Multiples of 10 Random integers 13 A Guide to Working with Visual Logic Arrays An array is a variable that is a collection of related data. (An array has a name, just like a variable does). Each of the values in the array/collection is called an element. Each element is like a separate variable (can store a value, access the value, modify the value). Each element is uniquely identified by an integer value called its index (subscript), which indicates its position (i.e. how far it is from the “first” element) in the array. Index values are integers. The lowest index in an array is 0 (zero). The largest index is called the upper bound. To refer to an element you must specify the array name and its index (e.g. MyArray(2) ; Nums(Position)). 14 A Guide to Working with Visual Logic Arrays In Visual Logic, when referencing an element in an array, start with the array name and then specify the desired index in parentheses. {In Java use [ ] instead of ( )} The index (that references an array) can be provided by an integer constant, an integer variable, or an arithmetic expression which evaluates to an integer. This gives a great deal of power to developers/programmers when using arrays. In Visual Logic you create an array using the Make Array command. 15 A Guide to Working with Visual Logic Problem: Average and Reverse of 12 Numbers Input (Fig: 5-5) 16 A Guide to Working with Visual Logic Using Arrays – For Loops An array can be used to store multiple values in a single storage location. Arrays get their full power when used in conjunction with loops. The body of the loop contains a reference to the array and the loop variable is used as the index value for the array. Loop variable is index Refererence to the array 17 A Guide to Working with Visual Logic Benefits of Using an Array An array can be used to store multiple values in a single storage location. This makes the code easy to read and easy to work with. Arrays get their full power when used in conjunction with loops. The body of the Loop contains a reference to the array. LCV is used as the index value of the array array (0 .. Upperbound) A Loop can be used to store values in an array. A Loop can be used to read/output values from an array. 18 A Guide to Working with Visual Logic Sample Program: Evens and Odds Write a program that declares an array named List with an upper bound of 10. The program should prompt the user for 10 values and store them into an array. The program should then calculate and display the average of the 10 values. The program should then display the even values and their average, and the program should display the odd values and their average. 19 A Guide to Working with Visual Logic Design : Evens and Odds 1. 2. 3. 4. 5. 20 Write a program that declares an array named List with an upper bound of 10. The program should prompt the user for 10 values and store them into an array. The program should then calculate and display the average of the 10 values. The program should then display the even values and their average. The program should display the odd values and their average. A Guide to Working with Visual Logic Design Technique: Divide and Conquer Break the program into separate steps. The smaller pieces can be solved individually Solving the smaller pieces solves the larger problem This technique is called “Divide and Conquer” 21 A Guide to Working with Visual Logic Design : Evens and Odds 1. 2. 3. 4. 5. 22 Write a program that declares an array named List with an upper bound of 10. ç MakeArray(List, 10) The program should prompt the user for 10 values and store them into an array. ç (For loop/ Input) The program should then calculate and display the average of the 10 values. ç (Sum inside loop, Calculate Average = Sum/10) The program should then display the even values and their average. ç Process each item in the Array. Display, Count and Sum only the even values. Calculate the average The program should display the odd values and their average. (similar to previous step) A Guide to Working with Visual Logic 23 A Guide to Working with Visual Logic 24 A Guide to Working with Visual Logic 25 A Guide to Working with Visual Logic 26 A Guide to Working with Visual Logic 27 A Guide to Working with Visual Logic Simulations are useful • Simulations (modeling) are powerful tools for studying the behavior of certain types of systems. • Applications: Satellite launching , automobile design simulations. 28 A Guide to Working with Visual Logic Dice Roll Simulations 29 A Guide to Working with Visual Logic 30 A Guide to Working with Visual Logic Sample Program: Dice Roll Simulation A Dice Roll: A single die is equally likely to roll a 1, 2, 3, 4, 5 or 6. While no single die can be predicted, if the die is rolled many times, the roll values should move toward an even distribution. Specifically, as the number of the rolls increases, the distributions should become closer to one-sixth, or 16.67%. Write a program that simulates rolling a single die many times. The program should maintain a count of how many times each of the 6 values was rolled. After all the rolls have been made the program should display the totals of how many times each value was rolled. 31 A Guide to Working with Visual Logic Design : Dice Roll Simulation 1. 2. 3. 4. 32 Rolling of a die - approximated/simulated by Random (6) + 1 è an integer from 1.. 6 6 variables to count how many times each value is rolled. Use an array to hold these 6 counters. The die roll 1.. 6 IS the index of the array. Counter(1) holds a count of how many 1s were rolled, etc. Each time the value I is rolled Counter(I) is incremented by 1 Finally, after all the values have been rolled, the program displays the totals and percentages as well as a histogram of the data. A Guide to Working with Visual Logic HISTOGRAM For each value rolled it displays an horizontal line (of circles) whose length is determined by the counter value. Use nested loops: For each rollvalue= 1 to 6 do Output: rollvalue + : + For count = 1 to Counter(rollvalue) do Output: “ 0” EndFor Output: newline EndFor 33 A Guide to Working with Visual Logic Dice Roll Simulation – Part 1 34 A Guide to Working with Visual Logic Dice Roll Simulation – Part 2 35 A Guide to Working with Visual Logic Dice Roll Simulation – Part 3 36 A Guide to Working with Visual Logic Problem – Username and Password Write a program that reads 10 username and password values into parallel arrays. After the arrays have been loaded, the program should behave like a login screen, prompting for a username and a password. Based on the data read and stored in the arrays, the program should respond appropriately with one of three output messages: “Username not found.”, “Username and password does not match.” or “Access granted.” 37 A Guide to Working with Visual Logic Sample Runs 38 A Guide to Working with Visual Logic Reading data from a text file In addition to Dialog and Console input, there is a third option called File Input, which allows the input data to come from a text file rather than being manually typed in each time the program is executed. Similarly, there is a third option for Output called File Output, which writes all the output in a specified text file (instead of in a Dialog box, or Console). In Visual Logic to perform file I/O (Input/Output): click “More,” choose the 3rd option, and specify the appropriate text file name. 39 A Guide to Working with Visual Logic File I/O in Visual Logic In the input text file DO (you MUST) use quotes around string data inside the text file. (Note: File input works the same as Console Input) DO NOT use quotes around the text filename in the I/O dialog. If the full path name is not used to specify a file name, then the file must be in the same folder as the Visual Logic.exe file (which may not be in the same folder as the .vlsig) If you have both FileInput and FileOuput in the same program you must use different file names for the Input and Output files. 40 A Guide to Working with Visual Logic Usernames.txt 41 "peanut butter" "burgundy" "jelly" "gold" "sunrise" "sofa" "sunset" "chair" "light" "boy" "dark" "girl" "forward" "fire" "reverse" "water" "water" "build" "oil" "destroy" A Guide to Working with Visual Logic Reading Usernames, Passwords into Parallel Arrays Figure 5-13 Pg 86 42 A Guide to Working with Visual Logic 43 A Guide to Working with Visual Logic Figure 5-13 Pg 86 Figure 5-13 Pg 86 44 A Guide to Working with Visual Logic