Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "08/09/20151 7 Arrays Defining, Declaring & Processing."— Presentation transcript:

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

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

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

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

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

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

7 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

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

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

10 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

11 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

12 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

13 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

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

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

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

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

19 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…)?

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

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

22 Extension “Election” Program 2

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

24 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

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

26 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

27 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!

28 Extension “Letter Tally” Program 3

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

30 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: 1. 1. Make sure you include a reset button. 2. 2. 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.

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

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

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

34 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 1991. Sales was €56800 in the 3 rd quarter of 1991. SalesFigures(2,0) = 56800 Sales was €96400 in the 4 th quarter of 1999. Sales was €96400 in the 4 th quarter of 1999. SalesFigures(4,9) = 96400 56800 96400 1234 12 3 4 5 6 7 8 9 10

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

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

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

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

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

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

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

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

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

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

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

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

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


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

Similar presentations


Ads by Google