1 / 22

Variables and Arrays

Variables and Arrays. Data Type Data Structure. What is a Variable ?. A storage box for Values. 26. Name of Box [Variable] = Age. Sample Subroutine Dim age as integer Console.writeline(“Enter age”) Age=Console.Readline Console.Writeline(age). The value ’26’ can go into the box

melia
Télécharger la présentation

Variables and Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Variables and Arrays Data Type Data Structure

  2. What is a Variable? A storage box for Values 26 Name of Box [Variable] =Age Sample Subroutine Dim age as integer Console.writeline(“Enter age”) Age=Console.Readline Console.Writeline(age) The value ’26’ can go into the box The box is called ‘Age’

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

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

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

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

  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)

  8. Remember the 3 main program constructs are: • Sequence • Selection • Iteration • Recursion…..(a function that calls itself)

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

  10. 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?)

  11. 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?

  12. 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…etc

  13. Most 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!

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

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

  16. “cat” “dog” “pig” “rat” “bat” “fish” “fox” “seal” “cow” If you picture a variable as a box How would you visualise an Array? Variable Name=Animalname pig Variable Aray Index MyAnimalArray 0 1 2 3 4 5 6 7 8 Moose moose moose moose

  17. What 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)

  18. “cat” “dog” “pig” “rat” “bat” “fish” “fox” “seal” “cow” Aray Index MyAnimalArray 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? (How would it be represented as a formula?) Last Element of Array = Index (arraysize – 1)

  19. 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 String

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

  21. 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 String

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

More Related