vl chap5 arrays

Post on 02-Dec-2015

70 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

DESCRIPTION

text

TRANSCRIPT

Chapter 5

A Guide to Working with Visual Logic 1

Arrays

Arrays

A Guide to Working with Visual Logic 2

�  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

Problems with Arrays

A Guide to Working with Visual Logic 3

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

Problems with Numeric Arrays

A Guide to Working with Visual Logic 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)

Problem: Average and Reverse of 5 Numbers Input

A Guide to Working with Visual Logic 5

Begin

End

Sum = 0

myCount1 to 5

Input: Number

Sum = Sum + Number

Average = Sum / 5

Output:"Average is " & Average

Begin

End

Input: First

Input: Second

Input: Third

Input: Fourth

Input: Fifth

Output:Fifth

Output:Fourth

Output:Third

Output:Second

Output:First

Problems: Input is a list of numbers

A Guide to Working with Visual Logic 6

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

More Problems: Input is a list of numbers

A Guide to Working with Visual Logic 7

�  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

�  How many Input Variables would we need?

�  Can we use a Loop? �  No? Why Not?

Problem: Reverse of 100 Numbers Input

A Guide to Working with Visual Logic 8

Reversing 5 Input Numbers

A Guide to Working with Visual Logic 9

Why? Because variable names include an integer index/position which can be changed through a LCV (loop control variable)

This works!!

Make Array in Visual Logic

A Guide to Working with Visual Logic 10

0 1 2 3 4 5 6 7 8

Scores

1.  What is the NAME of the Array? __________ 2.  What is the SIZE of the array? _____

(This is the number of memory locations created.) 3.  What are the names of the 9 variables for the

memory locations? 4.  What is the lowest index (position/subscript)

number? 5.  What is the upper bound/ highest index

(position/subscript) number? 6.  Do we have to use all the

variables/memory locations in the array?

Make Array in Visual Logic

A Guide to Working with Visual Logic 11

0 1 2 3 4 5 6 7 8

MyArray Make Array (MyArray, 8)

What does this do?

A Guide to Working with Visual Logic 12

0

0

75

86

67

0

0

70

0

0 1 2 3 4 5 6 7 8

Scores

What does this do?

A Guide to Working with Visual Logic 13

Populating Arrays Multiples of 10

Random integers

Arrays

A Guide to Working with Visual Logic 14

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

Arrays

A Guide to Working with Visual Logic 15

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

A Guide to Working with Visual Logic 16

Problem: Average and Reverse of 12 Numbers Input (Fig: 5-5)

Using Arrays – For Loops

A Guide to Working with Visual Logic 17

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

Refererence to the array

Loop variable is index

A Guide to Working with Visual Logic 18

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.

Sample Program: Evens and Odds

A Guide to Working with Visual Logic 19

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.

Design : Evens and Odds

A Guide to Working with Visual Logic 20

1.  Write a program that declares an array named List with an upper bound of 10.

2.  The program should prompt the user for 10 values and store them into an array.

3.  The program should then calculate and display the average of the 10 values.

4.  The program should then display the even values and their average.

5.  The program should display the odd values and their average.

Design Technique: Divide and Conquer

A Guide to Working with Visual Logic 21

�  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”

Design : Evens and Odds

A Guide to Working with Visual Logic 22

1.  Write a program that declares an array named List with an upper bound of 10. ç MakeArray(List, 10)

2.  The program should prompt the user for 10 values and store them into an array. ç (For loop/ Input)

3.  The program should then calculate and display the average of the 10 values. ç (Sum inside loop, Calculate Average = Sum/10)

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

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

Simulations are useful

A Guide to Working with Visual Logic 28

•  Simulations (modeling) are powerful tools for studying the behavior of certain types of systems. •  Applications: Satellite launching , automobile

design simulations.

Dice Roll Simulations

A Guide to Working with Visual Logic 29

A Guide to Working with Visual Logic 30

Sample Program: Dice Roll Simulation

A Guide to Working with Visual Logic 31

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

Design : Dice Roll Simulation

A Guide to Working with Visual Logic 32

1.  Rolling of a die - approximated/simulated by Random(6) + 1 è an integer from 1.. 6

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

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

4.  Finally, after all the values have been rolled, the program displays the totals and percentages as well as a histogram of the data.

HISTOGRAM

A Guide to Working with Visual Logic 33

�  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

Dice Roll Simulation – Part 1

A Guide to Working with Visual Logic 34

Dice Roll Simulation – Part 2

A Guide to Working with Visual Logic 35

A Guide to Working with Visual Logic 36

Dice Roll Simulation – Part 3

Problem – Username and Password

A Guide to Working with Visual Logic 37

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

Sample Runs

A Guide to Working with Visual Logic 38

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

Reading data from a text file

A Guide to Working with Visual Logic 39

File I/O in Visual Logic

A Guide to Working with Visual Logic 40

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

Usernames.txt

A Guide to Working with Visual Logic 41

"peanut butter"

"jelly"

"sunrise"

"sunset"

"light"

"dark"

"forward"

"reverse"

"water"

"oil"

"burgundy"

"gold"

"sofa"

"chair"

"boy"

"girl"

"fire"

"water"

"build"

"destroy"

Reading Usernames, Passwords into Parallel Arrays

A Guide to Working with Visual Logic 42

Figure 5-13 Pg 86

A Guide to Working with Visual Logic 43

Figure 5-13 Pg 86

A Guide to Working with Visual Logic 44

Figure 5-13 Pg 86

top related