05/02/20161 6 records. 205/02/2016 learning objectives state: the difference between records and...

Download 05/02/20161 6 Records. 205/02/2016 Learning Objectives State: The difference between records and arrays. The difference between records and arrays. How

If you can't read please download the document

Upload: marilynn-stafford

Post on 18-Jan-2018

221 views

Category:

Documents


0 download

DESCRIPTION

305/02/2016 A Record Can store many items of data regardless of data type. An array: Can only store data of one type. Can only store data of one type.

TRANSCRIPT

05/02/ Records 205/02/2016 Learning Objectives State: The difference between records and arrays. The difference between records and arrays. How to set up a record structure and declare an array of records. How to set up a record structure and declare an array of records. How to store and retrieve the contents of an array of records. How to store and retrieve the contents of an array of records. How to declare a variable that is available on other forms. How to declare a variable that is available on other forms. How to refer to a variable declared as above another form. How to refer to a variable declared as above another form. 305/02/2016 A Record Can store many items of data regardless of data type. An array: Can only store data of one type. Can only store data of one type. 405/02/2016 An array of Records Each column is a field. Each row is a record. Account Number SurnameForenameBalance 45278SmithSally JonesJohn CainShazad WhitePeter 250.00 505/02/2016 Declaring an array of records Declare a single record globally. Structure AccountStructure Structure AccountStructure Dim AccountNumber As Integer Dim Surname As String Dim Forename As String Dim Balance As Decimal End Structure End Structure AccountNumberSurnameForenameBalance 605/02/2016 Declaring an array of records Declare an array of this record. Dim Accounts(4) As AccountStructure Dim Accounts(4) As AccountStructure AccountNumberSurnameForenameBalance 705/02/2016 Declaring an array of records Declare a single record globally. Structure AccountStructure Structure AccountStructure Dim AccountNumber As Integer Dim Surname As String Dim Forename As String Dim Balance As Decimal End Structure End Structure Declare an array of this record. Dim Accounts(4) As AccountStructure Dim Accounts(4) As AccountStructure 805/02/2016 Processing Records Account Number SurnameForenameBalance Sally Accounts(1).Forename = Sally Accounts(3).Balance = 100 Console.WriteLine(Accounts(1).AccountNumber) Declaring an array of records Declare a single record globally. Structure Structure Dim As End Structure End Structure Declare an array of this record. Dim () As Dim () As Record Name Field Names & Data Types Record Array Name Record Name (used above) No. of records req. 1005/02/2016 Declaring an array of records This is like declaring your own data type. The data type is a 1x1 array of field names of various data types. Then you declare a variable which is a 1D array of the 1x1 data type. Which will result in a 2D array of records. 1105/02/2016 Processing records (Index).FieldName Array of Records Name 6a Student Test Marks Specification: Allow the user to enter up to 10 students and their test marks into an array. Allow the user to enter up to 10 students and their test marks into an array. Allow the user to search for student and see his/her test mark. Allow the user to search for student and see his/her test mark. Allow the user to see all students and their marks. Allow the user to see all students and their marks. Allow the user to reset and enter a new set of students and their marks. Allow the user to reset and enter a new set of students and their marks. Set up an record structure NameMark Structure StudentStructure Dim Name As String Dim Mark As Integer End Structure Set up an array of records for 10 students NameMark 'Set up the Students Array with space 10 students. 'It will be used to store each student's name and mark. Dim Students(10) As StudentStructure All code from this point is written (as normal) in the main procedure (i.e. in between Sub Main () & End Sub). 'Will be used to hold the number of students currently in the StudentsArray. Dim NumberOfStudents As Integer 'Will be used to store the required student name. Dim SearchName As String 'Will be used to represent each element of the array. Dim Index As Integer 'Will be used to represent if a name was found. Dim NameFound As Boolean = False 'Will be used to represent if the user wishes to enter another student. Dim EnterAnotherStudent As Boolean = False 'Will be used to represent if the user wishes to search for another student. Dim SearchAgain As Boolean = False 'Will be used to represent if the user wishes to reset. Dim Reset As Boolean = False 'Will be used to represent the users request. Dim Request As String = False Do Console.WriteLine(Do you wish to Add, Display, Search, Reset or Exit?) Request = Console.ReadLine If Request = Add Then Do 'Increment the Number Of Students to move to next element in the Students Array. NumberOfStudents = NumberOfStudents + 1 'Store the name entered into the Name field of the Students Array. Console.WriteLine(Enter the students name.) Students(NumberOfStudents).Name = Console.ReadLine 'Store the mark entered into the Mark field of the Students Array. Console.WriteLine(Enter the students mark.) Students(NumberOfStudents).Mark = Console.ReadLine Console.WriteLine(Do you want to add another student (True/False)?) EnterAnotherStudent = Console.ReadLine Loop Until EnterAnotherStudent = False Enter Another student? ElseIf Request = Display Then 'Loop through the Students array and display each student's name and mark (with a space between). For Index = 1 To 10 Console.WriteLine(Students(Index).Name & " " & Students(Index).Mark) Next Index ElseIf Request = Search Then Do 'Store the required student. Console.WriteLine(Enter a students name to search for.) SearchName = Console.ReadLine 'Loop through each element of the Students array and look for the student name entered. For Index = 1 To 10 'Has the student name been found? If Students(Index).Name = SearchName Then 'If so, show the student's mark. Console.WriteLine(SearchName & s mark is: & Students( Index ).Mark) NameFound = True Exit For End If Next Index Inform the user if the name was not found. If NameFound = False Then Console.WriteLine("The name was not found!") End if Console.WriteLine(Do you want to search for another student (True/False)?) SearchAgain = Console.ReadLine Loop Until SearchAgain = False Search for another student? ElseIf Request = Reset Then 'Set the number of students back to 0. NumberOfStudents = 0 End If Loop Until Request = Exit Exit? Now: 1.Enter 5 student names and marks. 2.Search for a student you have entered and check the correct mark is returned. 3.Search for a student you have not entered and check you get a Not found message. 4.Check all students and their marks are as you entered them. 5.Reset. 6.Enter 2 new student names and marks. 7.Search for a student you entered previously but not this time. 8.Check all students and their marks. What happens? What should happen? Why does this happen? 20 Reset Working? Reset the array of records Add the following code in the black rectangle: Add the following code in the black rectangle: ElseIf Request = Reset Then 'Set the number of students back to 0. NumberOfStudents = 0 Arrays may contain values from previous Arrays may contain values from previous processing. If they do then programs will use this processing. If they do then programs will use this data and give incorrect results. data and give incorrect results. 'Loop through the array and clear each name and mark. 'Loop through the array and clear each name and mark. For Index = 1 To 10 For Index = 1 To 10 Students(Index).Name = "" Students(Index).Mark = 0 Next Index Next Index End If Loop Until Request = Exit Exit? 6a Student Test Marks Run the program and test it. Commenting on Records For the presentation 6 I will only ask for comments to Records. Your comments MUST explain: What is the record for? And if it is important: How many elements and why this number? And when it is being used: What are you storing or retrieving? 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)? 2405/02/2016 Extension Estate Agency Records Program 6b An Estate Agency has a program which stores details of the last 10 queries made. These are held in an array which contains, for each query made, the following: The name of the customer The name of the customer The number of bedrooms requested The number of bedrooms requested The date on which the query was made The date on which the query was made Whether the customer asked for details of a property or not (As Boolean Yes/No). Whether the customer asked for details of a property or not (As Boolean Yes/No). Write a program which allows: 10 queries to be stored. 10 queries to be stored. When a name is entered it displays all their querys details. When a name is entered it displays all their querys details. Displays all queries stored. Displays all queries stored. A reset option. A reset option. 2505/02/2016 Extension Health Club Program 6c Store the following details of members of a health club in an array of records: Name Name Date of Birth (DOB) Date of Birth (DOB) Weight (kg) Weight (kg) Height (m) Height (m) Write a program which allows: Member details (as above) to be stored. Member details (as above) to be stored. When a name is entered it displays all their querys details. When a name is entered it displays all their querys details. Displays all member details stored. Displays all member details stored. A reset option. A reset option. 2605/02/2016 Plenary What is the difference between records and arrays? 2705/02/2016 A Record Can store many items of data regardless of data type. An array: Can only store data of one type. Can only store data of one type. 2805/02/2016 Plenary How can we set up a record structure and declare an array of records? 2905/02/2016 Declaring an array of records Declare a single record globally. Structure AccountStructure Structure AccountStructure Dim AccountNumber As Integer Dim Surname As String Dim Forename As String Dim Balance As Decimal End Structure End Structure Declare an array of this record. Dim Accounts(4) As AccountStructure Dim Accounts(4) As AccountStructure 3005/02/2016 Plenary How do we store and retrieve the contents of an array of records? 3105/02/2016 Processing records (Index).FieldName Array of Records Name