Variables and Arrays

Preview:

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

Citation preview

Variables and Arrays

Data TypeData Structure

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)

• 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

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

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

• 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

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

• Sequence

• Selection

• Iteration

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

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

• 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

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

• 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

• 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

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

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

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

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

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

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

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

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

Recommended