08/09/20151 7 Arrays Defining, Declaring & Processing.

Slides:



Advertisements
Similar presentations
30/04/ Selection Nested If structures & Complex Multiple Conditions.
Advertisements

MIS 3200 – Unit 4 Complex Conditional Statements – else if – switch.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Microsoft Visual Basic: Reloaded Chapter Six Repeating Program Instructions.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Standard Algorithms. Many algorithms appear over and over again, in program after program. These are called standard algorithms You are required to know.
by Chris Brown under Prof. Susan Rodger Duke University June 2012
IE 212: Computational Methods for Industrial Engineering
1 Visual Basic for Applications (VBA) for Excel Prof. Yitzchak Rosenthal.
PMS /134/182 HEX 0886B6 PMS /39/80 HEX 5E2750 PMS /168/180 HEX 00A8B4 PMS /190/40 HEX 66CC33 By Adrian Gardener Date 9 July 2012.
Chapter 17: Arrays Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
06/10/ Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and.
07/10/ Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)
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.
Chapter 12: How Long Can This Go On?
1 2.2 Selection Logical Operators. 2 Learning Objectives Explain how the logical operator AND Boolean statements works.
VBA Lab 2 I ns.Samia Al-blwi. Visual Basic Grammar Object: Visual Basic is an object-oriented language. This means that all the items in Excel are thought.
19/10/20151 Data Structures Arrays. 219/10/2015 Learning Objectives Explain initialising arrays and reading data into arrays. Design and write routine/s.
© 1999, by Que Education and Training, Chapter 8, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
27/05/ Iteration Loops Nested Loops & The Step Parameter.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 17 – Flag Quiz Application Introducing One-Dimensional.
30/10/ Iteration Loops Do While (condition is true) … Loop.
CS285 Visual Basic 2 Department of Computing UniS 1 Statements in Visual Basic A statement is the fundamental syntactical element of a program smallest.
04/11/ Arrays 1D Arrays Defining, Declaring & Processing.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Class Average Application Introducing the Do...Loop While and Do...Loop Until.
22/11/ Selection If selection construct.
Advanced Work with Embedded and Summative Assessment Dr. Steve Broskoske Misericordia University EDU 533 Computer-based Education.
Dr. Soha S. Zaghloul2 Let arr be an array of 20 integers. Write a complete program that first fills the array with up to 20 input values. Then, the program.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 18 – Student Grades Application Introducing.
Lab 6 (2) Arrays ► Lab 5 (1) Exercise Review ► Array Concept ► Why Arrays? ► Array Declaration ► An Example of Array ► Exercise.
18/12/ Arrays 2D Arrays Defining, Declaring & Processing.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
Top-down approach / Stepwise Refinement & Procedures & Functions.
MIS 3200 – Unit 5.1 Iteration (looping) – while loops – for loops Working with List Items.
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.
04/02/ Procedures Top-down approach / Stepwise Refinement & Sub Procedures.
05/02/ Records. 205/02/2016 Learning Objectives State: The difference between records and arrays. The difference between records and arrays. How.
CHAPTER EIGHT ARRAYS © Prepared By: Razif Razali1.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
02/03/ Strings Left, Right and Trim. 202/03/2016 Learning Objectives Explain what the Left, Right and Trim functions do.
Visual Basic CDA College Paphos Campus COM123 Visual Programming 1 Lecture: Charalambous Sotiris Week 8: COM123 Visual Programming 1 Lecture: Charalambous.
1 4.2 Selection Logical Operators. 2 Learning Objectives Explain how the logical operator AND Boolean statements works. Directly testing if text boxes.
National Diploma Unit 4 Introduction to Software Development Procedures and Functions.
21/03/ Working with Controls Text and List Boxes.
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
Lab 5 Arrays ► Lab 4 Exercise Review ► Array Concept ► Why Arrays? ► Array Declaration ► An Example of Array ► Exercise.
13/06/ Strings Left, Right and Trim. 213/06/2016 Learning Objectives Explain what the Left, Right and Trim functions do.
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
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.
One-Dimensional Array Introduction Lesson xx
If selection construct
Do While (condition is true) … Loop
4.1 Strings ASCII & Processing Strings with the Functions
If selection construct
1D Arrays Defining, Declaring & Processing
Text / Serial / Sequential Files
3.1 Iteration Loops For … To … Next 18/01/2019.
2D Arrays Defining, Declaring & Processing
Random Access Files / Direct Access Files
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
To understand what arrays are and how to use them
Review of Previous Lesson
Text / Serial / Sequential Files
3.2 Working with Data Scope of variables 29/07/2019.
8 Records 25/07/2019.
Introduction to Computer Programming IT-104
10.2 Procedures Passing Parameters 30/08/2019.
Presentation transcript:

08/09/ Arrays Defining, Declaring & Processing

208/09/2015 Learning Objectives Define an array and an element. Explain why arrays are useful and so when they should be used. State how to declare an array. State how to process an array i.e. use it (store or retrieve data). Explain the concept of 2D arrays and how to declare them.

308/09/2015 What is an Array? A data structure that stores as many items as required using a single variable. All items must be of the same data type. All items must be of the same data type.e.g. An array of integers. An array of integers. An array of strings. An array of strings. … A list box is an example of an array. A list box is an example of an array. A storage ‘slot’ in an array is called an element e.g. A storage ‘slot’ in an array is called an element e.g

408/09/2015 Why use arrays? To store a single number you would declare one integer variable. To store 3 numbers you would need 3 variables. Clearly declaring tens, hundreds, etc… of variables is difficult and this why arrays are used.

508/09/2015 How to declare an array? Similar to variables. Dim …(…) As … Array name The number of elements required. Data Type

608/09/2015 Processing an Array …(…) = Array Name A number or a variable (with a stored number) representing the required element. e.g. ExamMarks(3) = Mark e.g. ExamMarks(3) = Mark Will store the contents of the Mark variable in the 3 rd element of the ExamMarks array. Will store the contents of the Mark variable in the 3 rd element of the ExamMarks array.

708/09/2015 Initialising an array ‘All programs assume that the start values are 0 ‘and arrays may contain values from previous ‘processing. If they do then programs will use ‘this data and give incorrect results. Loop through each array element from element 1 to the last element. FOR i = 1 TO 26 ‘ LastElement e.g. 26 letters of the alphabet. Letters(i) = 0 ‘Place a zero in all array elements Letters(i) = 0 ‘Place a zero in all array elementsNEXT

808/09/2015 Program 7 Number Array Specification: Allow the user to enter up to 5 numbers and store them in an array. Allow the user to enter up to 5 numbers and store them in an array. Output an appropriate message if the user attempts to store a 6 th number. Output an appropriate message if the user attempts to store a 6 th number. Allow the user to display the contents of the array at any time, and to enter a number to be searched for in the array. Allow the user to display the contents of the array at any time, and to enter a number to be searched for in the array. Display the result of this search. Display the result of this search.

Program 7 Number Array Create a new project named ‘Number Array’. txtNumber butAddToArray butDisplayArray txtNumberSearch lstArray butNumberSearchlblNumberSearch

1008/09/2015 Program 7 Number Array Declare a global array and a variable for the element of the array. ‘Declare array. ‘Declare array. Dim Numbers(5) As Integer Dim Numbers(5) As Integer ‘Will keep a count of the number of numbers currently held ‘Will keep a count of the number of numbers currently held ‘in the Numbers array. ‘in the Numbers array. Dim HowManyNumbers As Integer Dim HowManyNumbers As Integer Go back to Election Program Go back to Election Program

1108/09/2015 Program 7 Number Array butAddToArray code: ‘Will be used to add the number to be stored in the array. ‘Will be used to add the number to be stored in the array. Dim Number As Integer Dim Number As Integer Number = txtNumber.Text ‘Store the number entered. Number = txtNumber.Text ‘Store the number entered. If HowManyNumbers = 5 Then ‘Is array full? If HowManyNumbers = 5 Then ‘Is array full? MsgBox(“The array is FULL!”) ‘Inform user array is full! Else ‘Array not full. Else ‘Array not full. ‘Increment How Many Numbers to move to next element of the Numbers array. HowManyNumbers = HowManyNumbers + 1 ‘Store the number in the next element of the array. Numbers(HowManyNumbers) = Number 'Clear the list box which may be displaying previously entered numbers. lstArray.Items.Clear txtNumber. Text = “” ‘Clear txtNumber. txtNumber.Focus ‘Place cursor in txtNumber. End If End If Go back to Election Program Go back to Election Program

Program 7 Number Array butDisplayArray code: ‘Clear the list box from last time. ‘Clear the list box from last time. lstArray.Items.Clear() lstArray.Items.Clear() ‘Will be used to represent each element of the array. ‘Will be used to represent each element of the array. Dim Element As Integer Dim Element As Integer 'Go through each used element in the array 'Go through each used element in the array 'and add each to the items of lstArray. 'and add each to the items of lstArray. For Element = 1 To 5 For Element = 1 To 5lstArray.Items.Add(Numbers(Element)) Next Element Next Element

1308/09/2015 Program 7.1 Number Array butNumberSearch code: Dim Element As Integer Dim Element As Integer Dim SearchNumber As Integer Dim SearchNumber As Integer SearchNumber = txtNumberSearch.Text SearchNumber = txtNumberSearch.Text 'Go through each used element in the array to search for the number required. 'Go through each used element in the array to search for the number required. For Element = 1 To 5 For Element = 1 To 5 ‘Has the number been found? If Numbers(Element) = SearchNumber Then lblNumberSearch.Text = "This number is in element " & Element & " of the array.“ lblNumberSearch.Text = "This number is in element " & Element & " of the array.“ Exit Sub Exit Sub End If ‘Keep looking until number is found or all 5 elements have been searched. Next Element Next Element ‘If loop has finished then the number was not found so display a suitable message. ‘If loop has finished then the number was not found so display a suitable message. lblNumberSearch.Text = “This number is not in the array.” lblNumberSearch.Text = “This number is not in the array.” Go back to Election Program Go back to Election Program

1408/09/2015 Program 7 Number Array Run the program and test it.

1508/09/2015 Program 7 Number Array Add a reset button with the following code: ‘Set HowManyNumbers back to 1 for a new set of numbers. HowManyNumbers = 0 ‘Clear all text boxes, labels and the list box. lblNumberSearch.Text = “” txtNumberSearch.Text = “” txtNumber.Text = “” lstArray.Items.Clear txtNumber.Focus ‘Place cursor in txtNumber.

1608/09/2015 Program 7 Number Array Now: 1.Enter 5 numbers. 2.Click the “Display Array” button. 3.Click the reset button. 4.Enter 2 new numbers. 5.Click the “Display Array” button. What happens? What should happen? Why does this happen?

17 08/09/2015 Initialising an array Add the following code to the Reset button. Add the following code to the Reset button. ‘All programs assume that the start values are 0 ‘and arrays may contain values from previous ‘processing. If they do then programs will use this data ‘and give incorrect results. 'Loop through the array and clear each number. Dim Element As Integer For Element = 1 To 5 Numbers(Element) = 0 Numbers(Element) = 0 Next Element Go back to Election Program Go back to Election Program

1808/09/2015 Program 7 Number Array Run the program and test it.

Commenting on Arrays In presentation 7 I will only ask for comments to arrays.7 Your comments MUST explain: What does the array hold? And if it is important: How many elements and why this number? And when it is being used: What are you storing in/retrieving from the array and why? When (after and before what) are you doing this and why does it have to be done there? When in the procedure code or, if it is on its own, in which procedure (button, checkbox, textbox, etc…)?

2008/09/2015 Extension “Add 10 Numbers” Program 1 Write a program to allow a user to enter ten numbers and display the sum. Use an array NOT FirstNumber, SecondNumber, ThirdNumber, etc…. Use an array NOT FirstNumber, SecondNumber, ThirdNumber, etc…. You can adapt the “Add Two Numbers” in 3.1 Working with Data but note that this program did not use an array. 3.1 Working with Data3.1 Working with DataExtension: Use a global “Numbers” array and “HowManyNumbers” variable as before, but do NOT use a global “RunningTotal” variable, use a loop to add up all the elements in the array when the “Sum” button is clicked. Use a global “Numbers” array and “HowManyNumbers” variable as before, but do NOT use a global “RunningTotal” variable, use a loop to add up all the elements in the array when the “Sum” button is clicked. This is not because a global running total variable would not work, as it would, but because exams may want you to do it this way and I want to make sure you can. Add a reset button. Add a reset button. Adapt the program so that user can see a list box with all the numbers entered. Adapt the program so that user can see a list box with all the numbers entered.

2108/09/2015 Extension “Election” Program 2 A town election is held to elect a new mayor. The people in the town can vote for whoever they prefer from the three candidates A, B, C. The voting is done by each voter typing A, B and C in a text box in a voting booth and then clicking a Vote button. This acts as input to a computer program. The software should assume that there are a maximum number of 1000 people who will be voting (don’t worry you will not be asked to test 1000 votes but set it up so that it would accept 1000 votes). It uses an array, Votes() to store the votes that are cast. Votes() is an array of 1000 characters: A, B or C. Votes() is an array of 1000 characters: A, B or C. A second array, CandidateTotals(), contains 3 integers and is used with the array, Votes(). Make sure you initialise each element of each array. Write this program and when a “Finish” button is clicked the winner should be displayed. Make sure the program can deal correctly with two or three of the candidates receiving equal votes. See next slides for some hints!

Extension “Election” Program 2

& Declare two arrays Votes(….) & CandidateTotals(….). See the introduction slide of this extension program for the numbers to use in (….) – in red - slide 20 of this presentation.20 Declare a variable to count how many votes are cast – HowMany…... As the Votes(….) array and the HowMany….. Variable will be needed by both the Vote and Result buttons you will need to declare them …..? Compare with the Program 7.1 Number Array (slide 10 of this presentation).10

Extension “Election” Program 2 In the Vote button add each vote to the Votes(….) array. Compare with the “Add” button of the Program 7.1 Number Array (slide 11 of this presentation).11

Extension “Election” Program 2 In the Result button search the Votes(….) array for each vote (A, B or C). Place totals in the CandidateTotals(….) array. CandidateTotals(1) for how many A’s, CandidateTotals(2) for how many B’s and ….. Compare with the “Search” button of the Program 7.1 Number Array (slide 13 of this presentation).13 However, you are no longer searching for a number entered by the user but first for “A”, then for “B” and then for “C”. Use three separate If’s for “A”, “B” and “C”. For example if a “A” is found then increment CandidateTotals(1): CandidateTotals(1) = CandidateTotals(1) + 1 Remember you are searching the Votes(….) array and incrementing the appropriate element of the CandidatesTotals array(….) when a “A”, “B” or “C” is found.

Extension “Election” Program 2 Compare the totals and declare the winner. CandidateTotals(1) for how many A’s, CandidateTotals(2) for how many B’s and ….. Make sure you add a “Reset” button. Compare with the “Reset” button of the Program 7.1 Number Array (slide 17 of this presentation).17

2708/09/2015 Extension “Letter Tally” Program 3 Write a program which will keep a tally of the number of times each letter appears in a given text (can copied and pasted in). Use an array of size 26 to store the totals for each letter. Make sure you initialise each element of the array. See next slides for some hints!

Extension “Letter Tally” Program 3

2908/09/2015 Obviously use a loop with a Mid function to extract letter by letter from the text entered. Then the long manual way would be to: If Character = “a” Then If Character = “a” Then Letters (1) = Letters (1) + 1 ElseIf Character = “b” Then ElseIf Character = “b” Then Letters (2) = Letters (2) + 1 and so on…. and so on…. However, this is not efficient and is not the approach exams are really expecting. See the next slide. See the next slide.

Extension “Letter Tally” Program 3 Use the Asc function to subtract the ASCII code of “a” from the ASCII code of each letter. The difference + 1 will tell you which element of the Letters(….) array to use. CharacterIndex = ASC(Character)-ASC(“a”) + 1 Letters(CharacterIndex) = Letters(CharacterIndex) + 1 Notes: Make sure you include a reset button This does not deal with capitals, if you have time try to see if you can get the program to do so. Continued on the next slide.

Extension “Letter Tally” Program 3 To display, the long manual way would be to: lblLettera.Text = Letters(1) lblLetterb.Text = Letters(1) lblLetterc.Text = Letters(1) … However, a quicker way (if you have placed 26 labels in a group box as shown in the picture on slide 28, named something like grpLetters) is: Index = 26 For Each Ctrl in grpLetters.Controls Ctrl.Text = Letters(Index) Index = Index - 1 Next Ctrl Note if you not have placed 26 labels in a group box as shown in the picture on slide 28 then either do so or use the long manual way above.

3208/09/2015 Declaring 2D Arrays Dim …(…, …) As … 1D Size 2D Size

3308/09/2015 2D Arrays e.g. a firm’s quarterly sales figures for the years 1990 – (quarters) x 10 (years) = 40 items of data 4 (quarters) x 10 (years) = 40 items of data Dim SalesFigures(4, 10) As Decimal

3408/09/2015 2D Arrays Each row is a year. Each column is a quarter. e.g. Sales was €56800 in the 3 rd quarter of Sales was €56800 in the 3 rd quarter of SalesFigures(2,0) = Sales was €96400 in the 4 th quarter of Sales was €96400 in the 4 th quarter of SalesFigures(4,9) =

3508/09/2015 Uses for 2D Arrays Useful for storing data for some mathematical problems. Limited use for ‘business’ type problems because all data items have to be of the same type.

Extension “Store BIKE IDs” Program 4 Super Bikes owns a rectangular parking area with 30 rows; each row has 4 bike spaces. Each bike is always parked in the same space. The array BikeSpace[30,4] stores the bike registrations. Soni uses a flowchart to help him design a module to populate the array with the bike registrations. Input is terminated using the rogue value “BK000”. Write this program and allow the user to see the contents of the array when a button is clicked. Also include a “Reset” button.

Extension “Store BIKE IDs” Program 4 Hints: Use an input box for each input in your loops: InputBox(“message”, “title”, defaultValue) You don’t have to have a title or a default value but you must have a message. Display the array in a list box with one loop and each row as a single item e.g.: For Row = 1 To 30 lstDisplay.Items.Add(BikeSpace(Row, 1) & “ “ & BikeSpace(Row, 2) & “ “ & BikeSpace(Row, 3) & “ “& BikeSpace(Row, 4) & “ “) Next Row You can use any loop you wish but note that exams may force you to use a specified type. Also note that the “Cancel” in the InputBox will not work as it returns “” and as it is in a loop, it just enters “” into the array and then asks for the next BikeID.

Extension “Chess Board” Program 5a Liliane wants to write a program to play chess. She will represent the board of 4 x 4 squares, using a 2-dimensional array. If a chess piece is on a square, it will take a value of 1. Write a program to accept row and column numbers and place a 1 at this position and re- display the board. See next slide for some hints.

Extension “Chess Board” Program 5a Use the form’s load event (double click the form) and create a loop like: For Row = … To … lstBoard.Items.Add(ChessBoard(Row, 1) & “ “ & ChessBoard(Row, 2) & “ “ & ChessBoard(Row, 3) & “ “& ChessBoard(Row, 4) & “ “) Next Row To display the initially empty board in a label. Also see the next slide.

Extension “Chess Board” Program 5a Update the appropriate row and column position in the array to 1 when a chess piece is placed on a square. Note that there is no need for an input box and nested loops here as the row and column entered will indicate where the 1 is to go in the ChessBoard array. Then use lstBoard.Items.Clear to clear the label before re-displaying the chess board, otherwise it will appear as though nothing has happened. Re-display the array with the same loop described on the previous slide. Don’t forget the “Reset” button.

Extension “Chess Board” Program 5b Liliane's next task is to indicate that there are pieces occupying the first two rows of the 4 x 4 board. Each square in rows 1 and 2 will be given the value 1. Add a “Start” button that occupies the first two rows of the 4 x 4 board with 1’s and clears the other rows.

Extension “Chess Board” Program 5b Rewrite the program using a DO While Loops (as you probably used For To Next Loops originally).

4308/09/2015 Plenary What is an array and an element? Array: Array: A data structure that stores as many items as required using a single variable. Element: Element: A storage ‘slot’ in an array. Why arrays are useful and so when they should be used? Useful because they avoid the need to declare a variable for each item of data. Useful because they avoid the need to declare a variable for each item of data. Should be used when you want to store more than a few items of data of the same type. Should be used when you want to store more than a few items of data of the same type.

4408/09/2015 Plenary How do we declare an array?

4508/09/2015 How to declare an array? Similar to variables. Dim …(…) As … Array name The number of elements required. Data Type

4608/09/2015 Plenary How do we process an array i.e. use it (store or retrieve data)? How are array elements numbered?

4708/09/2015 Processing an Array …(…) = Array Name A number or a variable (with a stored number) representing the required element. e.g. ExamMarks(3) = Mark e.g. ExamMarks(3) = Mark Will store the contents of the Mark variable in the 3 rd element of the ExamMarks array. Will store the contents of the Mark variable in the 3 rd element of the ExamMarks array.