Variables and Arrays

22
Variables and Arrays Data Type Data Structure

description

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 - PowerPoint PPT Presentation

Transcript of Variables and Arrays

Page 1: Variables and Arrays

Variables and Arrays

Data TypeData Structure

Page 2: Variables and Arrays

What is a Variable?

A storage box for Values

The value ’26’ can go into the boxThe box is called ‘Age’

Name of Box [Variable] =Age 26

Sample Subroutine

Dim age as integerConsole.writeline(“Enter age”)Age=Console.ReadlineConsole.Writeline(age)

Page 3: Variables and Arrays

• 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?

Page 4: Variables and Arrays

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

Page 5: Variables and Arrays

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.

Page 6: Variables and Arrays

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

Page 7: Variables and Arrays

• 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)

Page 8: Variables and Arrays

Remember the 3 main program constructs are:

• Sequence

• Selection

• Iteration

• Recursion…..(a function that calls itself)

Page 9: Variables and Arrays

• 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….

Page 10: Variables and Arrays

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

Page 11: Variables and Arrays

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?

Page 12: Variables and Arrays

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

Page 13: Variables and Arrays

• 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 languages

Page 14: Variables and Arrays

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.

Page 15: Variables and Arrays

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.

Page 16: Variables and Arrays

If you picture a variable as a boxHow 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 moose

Page 17: Variables and Arrays

• 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?

Page 18: Variables and Arrays

“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?)

Page 19: Variables and Arrays

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

Page 20: Variables and Arrays

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

Page 21: Variables and Arrays

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

Page 22: Variables and Arrays

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