logging in or signing up Variables and Arrays MissBenjamin Download Post to : URL : Related Presentations : Share Add to Flag Embed Email Send to Blogs and Networks Add to Channel Uploaded from authorPOINT lite Insert YouTube videos in PowerPont slides with aS Desktop Copy embed code: (To copy code, click on the text box) Embed: URL: Thumbnail: WordPress Embed Customize Embed The presentation is successfully added In Your Favorites. Views: 10 Category: Entertainment License: All Rights Reserved Like it (0) Dislike it (0) Added: March 15, 2011 This Presentation is Public Favorites: 0 Presentation Description No description available. Comments Posting comment... Premium member Presentation Transcript Variables and Arrays: Variables and Arrays Data Type Data StructureWhat is a Variable?: What is a Variable ? A storage box for Values The value ’26’ can go into the box The box is called ‘Age’ Name of Box [Variable] = Age 26 Sample Subroutine Dim age as integer Console.writeline(“Enter age”) Age=Console.Readline Console.Writeline(age)What is a Variable?: All variables must be declared before they are used in a program Dim number1 as Integer Dim number2 as Real Dim number3 as Double Dim answer as Boolean Dim result as char etc What is a Variable ?Flow Control: Flow Control A program –typically –is just a Sequence of Statements. (a statement is just one step or instruction in a high level language) Statements are usually executed in a sequence. For instance Statement 1 Statement 2 Statement 3 Statement 4 Statement 5List of statements in sequence: List of statements in sequence Is fine when all you need is a list of steps executed in that sequence But In most real problems….solutions cannot just be broken down into ONE set of sequential steps.For instance: For instance You might need TWO sequences to be executed depending on if the CONDITION is TRUE or FALSE. If condition=true (or met) then Do Sequence 1 else Do Sequence 2Slide 7: What we need is some way to control the flow of statements in a program We do this by the use of SELECTION and ITERATION (REPETITION)Remember the 3 main program constructs are:: Remember the 3 main program constructs are: Sequence Selection Iteration Recursion…..(a function that calls itself)What is a Variable….: It’s a storage box for a value It sets aside some place in computer memory or storage to keep a value –so it can be used in programming What is a Variable….What is the problem with Variables?: What is the problem with Variables? Dim age as integer Suppose you needed to store the ages of everyone in this class (in like a database type thing) and access it. How would you do it? (How would you declare it?)Option 1 –Declare all the variables: Option 1 –Declare all the variables Dim mattsage as integer Dim atleesage as integer Dim nancysage as integer Dim charliesage as integer Dim Davidsage as integer Etc Etc etc What is the problem with doing it like this?Option 2 –Use An Array!: Option 2 –Use An Array! A variable can only be used to store a single value There are situations in which you may need to store a number of values of the same type. Storing each value in a Variable could be idiotic….time consuming…etcMost high level programming languages: Provide an inbuilt data structure called an array so this enables a set of values to be stored conveniently and easily! Most high level programming languagesSo what is an Array?: So what is an Array? Think of it as a muchhhh cooler way of storing elements, as opposed to declaring single variables. Official definition : A Data structure used in programming to store and manipulate a collection of elements of the same data type.Definition of Array: Definition of Array An array is a linear sequence of one or more variables with the same variable name. They are distinguished by a positional number called an index. The term used to describe a variable of an array is element.If you picture a variable as a box: If you picture a variable as a box How would you visualise an Array? pig Variable Name= Animalname Variable “cat” “dog” “pig” “rat” “bat” “fish” “fox” “seal” “cow” MyAnimalArray Aray Index 0 1 2 3 4 5 6 7 8 Moose moose moose mooseWhat facts/feature of an Array can we state?: An Array has a Name An array stores data items of the same type Number of elements in an array determine the length or size of an array Array elements are identified by an index. First element is always index 0 and last element is always at index (array size-1) What facts/feature of an Array can we state?Slide 18: “cat” “dog” “pig” “rat” “bat” “fish” “fox” “seal” “cow” MyAnimalArray Aray Index 0 1 2 3 4 5 6 7 8 Array Name? Array Size? First Index of Array? First Element of Array? Last Element of Array is represented as Index…? This Array stores data items of the same data type –which data type? Index (arraysize – 1) Last Element of Array = (How would it be represented as a formula?)Lets look at another example… (this time you draw it): Lets look at another example… (this time you draw it) Consider A Program that keeps track of the names of the people per seat in the rows of a movie theatre. To identify each person you need to store the name of the person associated with the seat number. You could create a variable for each seat, but that would be stupid! (data handling becomes huge) i.e Dim personinseat1 as String Dim personinseat2 as String Dim in personinseat3 as StringInstead: Instead We create an array to store the names of the people in one row. You could later make an array for each row of the theatre. For now, lets just focus on ONE ROWTask: Task Row number 7 10 seats in this row Each seat occupied by one person Call the array row7 It will have a length of 10 Index starting at 0 and ending at ….. The elements will be of type StringWhats the point of an array?: Whats the point of an array? Storage Accessing elements Say we want to tell person in Row 7, seat 6 (who happens to be John) to get out of the cinema at once. Or we wanted to access all array elements. Change one of the elements stored… Search for a specific item in the array You do not have the permission to view this presentation. In order to view it, please contact the author of the presentation.
Variables and Arrays MissBenjamin Download Post to : URL : Related Presentations : Share Add to Flag Embed Email Send to Blogs and Networks Add to Channel Uploaded from authorPOINT lite Insert YouTube videos in PowerPont slides with aS Desktop Copy embed code: (To copy code, click on the text box) Embed: URL: Thumbnail: WordPress Embed Customize Embed The presentation is successfully added In Your Favorites. Views: 10 Category: Entertainment License: All Rights Reserved Like it (0) Dislike it (0) Added: March 15, 2011 This Presentation is Public Favorites: 0 Presentation Description No description available. Comments Posting comment... Premium member Presentation Transcript Variables and Arrays: Variables and Arrays Data Type Data StructureWhat is a Variable?: What is a Variable ? A storage box for Values The value ’26’ can go into the box The box is called ‘Age’ Name of Box [Variable] = Age 26 Sample Subroutine Dim age as integer Console.writeline(“Enter age”) Age=Console.Readline Console.Writeline(age)What is a Variable?: All variables must be declared before they are used in a program Dim number1 as Integer Dim number2 as Real Dim number3 as Double Dim answer as Boolean Dim result as char etc What is a Variable ?Flow Control: Flow Control A program –typically –is just a Sequence of Statements. (a statement is just one step or instruction in a high level language) Statements are usually executed in a sequence. For instance Statement 1 Statement 2 Statement 3 Statement 4 Statement 5List of statements in sequence: List of statements in sequence Is fine when all you need is a list of steps executed in that sequence But In most real problems….solutions cannot just be broken down into ONE set of sequential steps.For instance: For instance You might need TWO sequences to be executed depending on if the CONDITION is TRUE or FALSE. If condition=true (or met) then Do Sequence 1 else Do Sequence 2Slide 7: What we need is some way to control the flow of statements in a program We do this by the use of SELECTION and ITERATION (REPETITION)Remember the 3 main program constructs are:: Remember the 3 main program constructs are: Sequence Selection Iteration Recursion…..(a function that calls itself)What is a Variable….: It’s a storage box for a value It sets aside some place in computer memory or storage to keep a value –so it can be used in programming What is a Variable….What is the problem with Variables?: What is the problem with Variables? Dim age as integer Suppose you needed to store the ages of everyone in this class (in like a database type thing) and access it. How would you do it? (How would you declare it?)Option 1 –Declare all the variables: Option 1 –Declare all the variables Dim mattsage as integer Dim atleesage as integer Dim nancysage as integer Dim charliesage as integer Dim Davidsage as integer Etc Etc etc What is the problem with doing it like this?Option 2 –Use An Array!: Option 2 –Use An Array! A variable can only be used to store a single value There are situations in which you may need to store a number of values of the same type. Storing each value in a Variable could be idiotic….time consuming…etcMost high level programming languages: Provide an inbuilt data structure called an array so this enables a set of values to be stored conveniently and easily! Most high level programming languagesSo what is an Array?: So what is an Array? Think of it as a muchhhh cooler way of storing elements, as opposed to declaring single variables. Official definition : A Data structure used in programming to store and manipulate a collection of elements of the same data type.Definition of Array: Definition of Array An array is a linear sequence of one or more variables with the same variable name. They are distinguished by a positional number called an index. The term used to describe a variable of an array is element.If you picture a variable as a box: If you picture a variable as a box How would you visualise an Array? pig Variable Name= Animalname Variable “cat” “dog” “pig” “rat” “bat” “fish” “fox” “seal” “cow” MyAnimalArray Aray Index 0 1 2 3 4 5 6 7 8 Moose moose moose mooseWhat facts/feature of an Array can we state?: An Array has a Name An array stores data items of the same type Number of elements in an array determine the length or size of an array Array elements are identified by an index. First element is always index 0 and last element is always at index (array size-1) What facts/feature of an Array can we state?Slide 18: “cat” “dog” “pig” “rat” “bat” “fish” “fox” “seal” “cow” MyAnimalArray Aray Index 0 1 2 3 4 5 6 7 8 Array Name? Array Size? First Index of Array? First Element of Array? Last Element of Array is represented as Index…? This Array stores data items of the same data type –which data type? Index (arraysize – 1) Last Element of Array = (How would it be represented as a formula?)Lets look at another example… (this time you draw it): Lets look at another example… (this time you draw it) Consider A Program that keeps track of the names of the people per seat in the rows of a movie theatre. To identify each person you need to store the name of the person associated with the seat number. You could create a variable for each seat, but that would be stupid! (data handling becomes huge) i.e Dim personinseat1 as String Dim personinseat2 as String Dim in personinseat3 as StringInstead: Instead We create an array to store the names of the people in one row. You could later make an array for each row of the theatre. For now, lets just focus on ONE ROWTask: Task Row number 7 10 seats in this row Each seat occupied by one person Call the array row7 It will have a length of 10 Index starting at 0 and ending at ….. The elements will be of type StringWhats the point of an array?: Whats the point of an array? Storage Accessing elements Say we want to tell person in Row 7, seat 6 (who happens to be John) to get out of the cinema at once. Or we wanted to access all array elements. Change one of the elements stored… Search for a specific item in the array